knockout / knockout

Knockout makes it easier to create rich, responsive UIs with JavaScript
http://knockoutjs.com/
Other
10.47k stars 1.51k forks source link

How to share VM aware models across different VMs #235

Closed kapv89 closed 12 years ago

kapv89 commented 12 years ago

I am basically looking for a design pattern or something which would allow same set of objects to be shared across different VMs .. do note that when making templates, i use data-bind only (no jquery.tmpl stuff), and when using nested templates, the only way I found for an object(which is in another object) to be aware of the VM to use(for handling events like click etc) was to store the pointer to VM in the object itself. This makes it impossible to share a single set of data across different VMs whose bindings have been applied to different sections of the dom.

In other words .. how do i make actions handled by different VMs to modify same set of data which should be shared by all the VMs (by having the same object which holds all data as their prototype)

kapv89 commented 12 years ago

one way could be to define all the ViewModels like window.vm1 = new VM1() (or any other object other than window which encloses the app) .... I am just looking for a conceptually sound solution .. best practices sort of thing ..

mbest commented 12 years ago

Not really sure what you're asking for here. Is there a particular feature you'd like to see in Knockout to support this?

rniemeyer commented 12 years ago

If this is still an issue, then please reopen to discuss.

vicary commented 12 years ago

Object.prototype might not be a good solution.

Just share the same observable/computed across all the view models you created, or, like the previous issue that mentioned about multiple calls to applyBindings() over the same element, you can always use template or with bindings and share the same observable in the parent view model.

This is all I can read in your words... I smell a data mindset that is quite different from knockout here, maybe more study on the documents?