I've been going crazy trying to figure this one out. I've seen plenty of obfuscated and tough-to-implement examples of filling a computed observable in Knockout when an async call has finished, but I can't seem to make it work using the module-reveal pattern.
I'm trying to create a read-only observable, as that's all I need. I wouldn't even make an observable if I didn't need an anonymous function to run to make the request. Here is my view-model:
eh.vm.skills = function () {
//#region Public Variables
var skills = ko.computed({
read: function () {
$.get("http://horodyski.me/api/skills", function (data) {
return data;
});
},
deferEvaluation: true
}),
//#endregion
//#region Public Interface
return {
skills: skills
}
//#endregion
};
And this is the HTML binding:
<ul class="skills skills-top" id="skills" data-bind="foreach: skills">
<li>
<i data-bind="text: $index"> </i>
<span data-bind="text: $data.Title"></span>
</li>
</ul>
<script src="//cdn.horodyski.me/js/vm.js"></script>
<script>
ko.applyBindings(eh.vm.skills, $("#skills")[0]);
</script>
What I'd like to do is on creation of the skills
variable, fetch the data and return it. The data already comes back in an array (ex: [{Title: "ABC"}]
) but it doesn't seem to bind. I tried using $.when().then()
instead (as I prefer it), but even when the computed value is deferred it still won't update.
The really complex part for me is variable scope. The module-reveal pattern sucks when it comes to scope. I've been racking my brains over this for 3 hours...if anyone can guide me in the right direction it would be appreciated.
Edit Using Knockout 3.1 (if it helps)
I'm trying to make a .jsp where, by clicking a button, the page opens a modal dialog with some stuff. I'm also using jQuery. I'm new to this, so, to test if jQuery was working, I first tried a kind of ...
I'm trying to make a .jsp where, by clicking a button, the page opens a modal dialog with some stuff. I'm also using jQuery. I'm new to this, so, to test if jQuery was working, I first tried a kind of ...
I'm learning Angular but having troubles to understand a point. I want to get data from my server. I used this code (from angular's doc) : this.myFunction = new function() { var uri = this....
I'm learning Angular but having troubles to understand a point. I want to get data from my server. I used this code (from angular's doc) : this.myFunction = new function() { var uri = this....
I am using Jquery to making drag and drop. My short javascript to make an item to drag is: $(".draggable").draggable(); My short javascript to make a dropable area is: $("#droppable").droppable(); ...
I am using Jquery to making drag and drop. My short javascript to make an item to drag is: $(".draggable").draggable(); My short javascript to make a dropable area is: $("#droppable").droppable(); ...
I'm trying to wrap my head around Meteor's reactivity. I understand it re-renders a page when a reactive data source that's being referenced in a template changes. I also understand what constitutes a ...
I'm trying to wrap my head around Meteor's reactivity. I understand it re-renders a page when a reactive data source that's being referenced in a template changes. I also understand what constitutes a ...