Sure this is a very easy question to answer but is there an easy way to determine if any property of a knockout view model has changed?
相关问题
- Is there a limit to how many levels you can nest i
- How to toggle on Order in ReactJS
- void before promise syntax
- Keeping track of variable instances
- Can php detect if javascript is on or not?
Consider a view model as follows
After you Bind your Data you can store the state using ko.toJS(myViewModel) function.
You can declare a variable inside your view model as a computed observable like
This computed function will be entered when there is change to any of the other observables inside the view model.
Then you can compare the two view model states as:
});
**Note: This computed observable function is also executed the first time when the view model is initialized. **
Hope this helps ! Cheers!!
Use extenders:
Then:
Now you can inspect like this:
You can also write some generic viewModel traversing to see if anything's changed:
... and then just check at the viewModel level
You might use the plugin below for this:
https://github.com/ZiadJ/knockoutjs-reactor
The code for example will allow you to keep track of all changes within any viewModel:
PS: As of now this will not work with subscribables nested within an array but a new version that supports it is on the way.
Update: The sample code was upgraded to work with v1.2b which adds support for array items and subscribable-in-subscribable properties.
You can subscribe to the properties that you want to monitor:
This will alert when personName changes.
Ok, so you want to know when anything changes in your model...
(haven't tested it - but you should get the idea)
Consider using Knockout-Validation plug-in
It implements the following:
Along with other validation stuff which comes in handy!
Cheers
I had the same problem, i needed to observe any change on the viewModel, in order to send the data back to the server, If anyone still intersted, i did some research and this is the best solution iv'e managed to assemble:
in order to use this 'global observer':
Note that the callback given (in this case 'Ajax_Submit') will be fired on ANY change that occurs on the view model, so i think it's really recommended to make some sort of delay mechanism to send the entity only when the user finished to edit the properties:
I'm new to JavaScript and the knockout framework, (only yestarday i started to work with this wonderfull framework), so don't get mad at me if i did something wrong.. (-:
Hope this helps!