You have a few problems:
You are using Knockout 1.2.1
The foreach binding was not added until the 2.0 knockout was released.
You are not using observableArray
You need to change the categories property as ko.observableArray() instead of a simple array. Otherwise, the knockout will not be able to observe when you push to it, and the remove method will not exist.
The binding of this is incorrect.
When called from event handlers, this will be set incorrectly. You can fix this in many ways, discussed in the Knockout documentation , but one simple fix is ββto change the references to the viewModel instead of before this .
To fix all this, you must go to "Knockout 2.0" and change your view model ad to
var viewModel = { name: ko.observable(''), description: ko.observable(''), categories: ko.observableArray(), categoryToAdd: ko.observable(''), removeCategory: function(category) { viewModel.categories.remove(category); }, addCategory: function() { viewModel.categories.push(new Category(viewModel.categoryToAdd())); viewModel.categoryToAdd(''); } };
Here is the adjusted JSFiddle: http://jsfiddle.net/ueNg7/19/
source share