Can I watch an object, and change a link if not null??

Is it possible for me to watch the values of an object, and if not null, change the HREF of a link on the page?

For instance, my object which is stored in SessionStorage, looks like this when created:

var deviceRepair = {

     manufacturer: null,
     deviceModel: null,
     serviceProvider: null,
     repair: null   
 
}

Open in new window


and I have some links that correspond to the object:

          <div class="stepWrapper">
              <a id="link-manufacturer" href="" class="step current">Manufacturer</a>
              <a id="link-deviceModel" href="" class="step">Model</a>
              <a id="link-serviceProvider" href="" class="step">Service Provider</a>
              <a id="link-repair" href="" class="step">Repair</a>
          </div>

Open in new window


Now I need to change the HREF in the links IF the key in my object is not NULL (meaning they have already completed that section).  I will add a value to each key when they complete each section. So if the key has a value, I need to add the appropriate HREF which I stored in this object:

    var links = {
        manufacturer: "RapidRepair/Manufacturer",
        model: "RapidRepair/DeviceModel",
        serviceProvider: "RapidRepair/ServiceProvider",
        repair: "RapidRepair/Repair"
    }

Open in new window


Is there a way in Knockout to do this? This client is using KO , and I am very new to it.

Thank you.
John S.Web DeveloperAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

leakim971PluritechnicianCommented:
why don't you update the links at the same time you update the localStorage ?
You don't need jQuery or KO :
<div class="stepWrapper">
    <a id="link-manufacturer" href="" class="step current">Manufacturer</a>
    <a id="link-deviceModel" href="" class="step">Model</a>
    <a id="link-serviceProvider" href="" class="step">Service Provider</a>
    <a id="link-repair" href="" class="step">Repair</a>
</div>
<script>
    if(!localStorage.deviceRepair)
        localStorage.setItem('deviceRepair', JSON.stringify({
            manufacturer: null,
            deviceModel: null,
            serviceProvider: null,
            repair: null
        }));
    var updateLinks = function() {
        var deviceRepair = JSON.parse(localStorage.deviceRepair);
        for(var _attribute in deviceRepair) {
            var id = "link-" + _attribute;
            document.getElementById(id).setAttribute("href", deviceRepair[_attribute]);
        }
    }
    updateLinks();
</script>

Open in new window


or to watch using a timer :
    updateLinks();
to :
    setInterval(updateLinks, 100); // RESOURCES CONSUMING EVERY 100ms
</script>
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
John S.Web DeveloperAuthor Commented:
That would be great, but the value of the key is not a url.

For instance, the object will look something like this:

var deviceRepair = {

     manufacturer: "Apple",
     deviceModel: "iPhone 5",
     serviceProvider: "Verizon",
     repair: "Broken Screen"
 
}

Open in new window


Each key is a page they visited. I need to make sure the links in the header are only active if they have already visited that page, thus, the object key will hold a value. If it has a value, then the link can be hot.

Is there a way using your method to do this?
0
leakim971PluritechnicianCommented:
That would be great, but the value of the key is not a url.

It's not an issue for the provided code.
you may use the first version without the timer as you want to update the link only when the user come back to your page
the code loop over all the key and update the link according there's a not null value for the key
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
KnockoutJS

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.