How can I safely change a model on a jb Backbone view?

I am experimenting with master-detail material using Backbone js and Marionette. How can I change the model for presentation? I want view model listeners to be removed from the old model and applied to the new one. The same goes for other similar things related to the model. Has anyone received the "changeModel" code to make this clean?

+4
source share
1 answer

Wouldn't it be easier to just destroy the old view and create a new one based on a different model? In this example, I pass the model itself to ItemViewOptions, and on the model I have some defining characteristics ... and since the model comes from the server, I can manipulate what happens in the view. This is for binding dynamic templates, but I think you could do something with events. This will require you to destroy everything that you have, and not to bandage everything.

node = Backbone.Model.extend nodes = Backbone.Collection.extend model: node url: -> Myapp.rooturl + "/api/node" initialize: (nodes) -> @fetch() nodeView = Backbone.Marionette.ItemView.extend initialize: (options) -> @template = "#" + options.model.attributes.nodetemplate + "-template" tagName: 'div' nodesView = Backbone.Marionette.CollectionView.extend itemView: nodeView itemViewOptions: @model 
+1
source

Source: https://habr.com/ru/post/1438898/


All Articles