问题
For the past few days I'm getting more interested in Knockoutjs. It looks very promising because it models the MVVM pattern and WPF like bindings, but I do have some doubts whenever it bring something useful to non RIA web apps (and when I say RIA I mean complex in browser applications, let's say an ERP, anyway something a bit more complex than adding a few rows to a table and hiding one)
Let's say you have a combobox with 10 items, and you need to be able to create on client side another 2 items and save them on the server.
The way I see it you would have to create a viewmodel with a Obs. array prefilled with the 10 items, and also render the 10 items inside the combobox (as 10 option elements).
Basically you would have 2 loop twice the item collection and render the items in js viewmodel and the combobox (options).
Now imagine you would have 30 controls wouldn't having information on both knockout view model and html controls with pre-filled data be an overhead?
回答1:
Every situation is different but I don't think you would normally need to render those combobox options as well as defining them in a Knockout view model - Knockout will build the options HTML for you. I have numerous comboboxes populated in this way in a complex object graph. When the page first loads the initial state of the object graph is rendered in the page as an object literal which I then pass into my top-level Knockout view model constructor, which creates the entire structure via the mapping plugin (creating child view models etc). Then all saving and updating is done via small discrete Ajax operations and remapping with the mapping plugin.
来源:https://stackoverflow.com/questions/11327138/knockoutjs-duplicating-data-overhead