What’s the difference between ko.utils.unwrapObservable and ko.toJS?

There are differences between them. The ko.toJS takes an object and “opens up” and cleans the object from all observables. It does this for the entire object graph. I behaves much like a serializer which means that you’ll run into problems if you have circular references for instance. It uses mapJsObjectGraph internally which is way … Read more

Knockout Subscribe to any change in observable complex object

You can use the following trick: ko.computed(function() { return ko.toJSON(complexObject); }).subscribe(function() { // called whenever any of the properties of complexObject changes }); See http://jsfiddle.net/xcajt4qn/3/ The reason why this works is ko.toJSON will recursively read all the properties in the object, therefore making the computed depend on all the properties.

knockout viewmodel property undefined

So, there are a few options that you have: KO will have an issue when you try to bind against undefined properties, unless they are off of an object. So, you can prefix your various bindings with $data. and KO will be able to parse your bindings. Sample: http://jsfiddle.net/rniemeyer/dLCL8/ If you know that several properties … Read more

Knockout.js: time input format and value restriction

Here is a thread with a few different techniques to keep the value numeric: https://groups.google.com/d/topic/knockoutjs/SPrzcgddoY4/discussion One option is to push this concern into your view model and create a numericObservable to use instead of a normal observable. It might look like: ko.numericObservable = function(initialValue) { var _actual = ko.observable(initialValue); var result = ko.dependentObservable({ read: function() … Read more

Observable array push multiple Objects in knockout.js

We do have ko.utils.arrayPushAll(array, valuesToPush) as a utility function that you can use. It is not available directly off of observableArrays though. If you add your pushAll to observableArrays, you would want to operate on the underlying array (this() in your case) and then call valueHasMutated() on the observableArray at the end. This will ensure … Read more

Adding properties to the view model created by using the Knockout JS mapping plugin

You need to use a create method on the mapping object itself like: var mapping = { //customize at the root level. create: function(options) { //first map the vm like normal var vm = ko.mapping.fromJS(options.data); //now manipulate the returned vm in any way that you like vm.someProperty = “test”; vm.someComputed = ko.computed(function() { return vm.first() … Read more

Access viewModel in javascript function outside viewModel’s scope

You can always access it just by storing your viewmodel in a variable you can access, the module and revealing module patterns are nice, but you can just store it in an object that won’t conflict with other names (‘my’ here): my = { viewModel: new EmployeeViewModel() }; ko.applyBindings(my.viewModel); You had the right idea, you … Read more

File not found.