I’m using the knockout mapping plugin, previously I would have set a subscribe like so
self.selectedProduct.subscribe(function (name) {
}
but I'm unsure of how to do this in this format below. The message I get is
“missing : after property id”
var viewModel = {
products: ko.mapping.fromJS([]),
productOptions: ko.mapping.fromJS([]),
productOptions.subscribe = function (name) {
alert('somthing change');
},
loadInitialData: function () {
ko.mapping.fromJS(serverData, viewModel.productOptions);
}
}
If you are defining your view model as an object literal, then you can’t make a function call like you are doing inline. You would have to do it after your view model is created or create your view model in a different way.
If you did it afterwards, then it would look something like:
If you created your view model using a constructor function, then it would look like: