I am working on a project which is using Knockout.js to give us MVVM style binding between our data model (view model) and our views. We are also MVC4 and Web API. This means that my ViewModels will be making calls in to our Web API endpoints to get json back. When we receive this json back we are using the property from the resulting json to bind to our UI. This works great in a read-only scenario, but what happens when I want to update my UI via binding?
If you need to update the values AFTER the initial binding you have 2 choices (there may be more but these are what I know)
1) You can create a new property, mark it as being observable, and set the original value from the json result as the seed value in your new property.
item.newProperty = ko.observable(item.OriginalProperty)
2) You can simply overwrite your original property as make it observable (gotta love how javascript is dynamic, right)
item.(item.OriginalProperty) = ko.observable(item.OriginalProperty)
In my opinion option 2 is the way to go, I feel it is a bit cleaner and ‘it just works’
Thoughts?
Till next time,
Posted
12-15-2012 12:52 PM
by
Derik Whittaker