Using resources in the view-model
Now that we have created our product resource, we will use it in our view-model to get our data back by following these steps:
- First of all, link the
ProductResource.js
file in theindex.html
file, as follows:<script type='text/javascript' src='js/resources/ProductResource.js'></script>
Since the resource works asynchronously, you can't apply bindings at the end of the file because the data may not be ready yet. Therefore, bindings should be applied when the data has arrived.
To do this, create a method called
activate
. This method will be fired at the end of the file, on the same line we calledko.applyBindings
earlier, in the following manner:- Take this line of code:
ko.applyBindings(vm);
- Replace it with this one: ...
- Take this line of code:
Get KnockoutJS Essentials now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.