Saturday, 14 September 2013

KnockoutJS arrayFilter doesn't update

KnockoutJS arrayFilter doesn't update

A Knockout newbie here. I've tried to use ko.utils.arrayFilter but it
doesn't seem to update when I use it. I'm using the same method I used
with arrayForEach so I'm not sure what's wrong here. How can I get this
the list to update when using arrayFilter?
JS:
function entry(name, category) {
this.name = ko.observable(name);
this.category = ko.observable(category);
}
function entriesModel() {
this.entries = ko.observableArray([]);
this.filter = function () {
ko.utils.arrayFilter(this.entries(), function (item) {
return item.category == 'SciFi';
});
};
this.sort = function () {
this.entries.sort(function (a, b) {
return a.category < b.category ? -1 : 1;
});
};
}
$(document).ready(function () {
$.getJSON("entries.php", function (data) {
entries(data);
});
ko.applyBindings(entriesModel());
});
HTML:
<ul data-bind="foreach: entries">
<li>
<p data-bind="text: name"></p>
<p data-bind="text: category"></p>
</li>
<button data-bind="click: filter">Filter</button>
<button data-bind="click: sort">Sort</button>

No comments:

Post a Comment