Backbone.js: complex views combining multiple models

后端 未结 2 412
梦谈多话
梦谈多话 2020-12-23 11:39

So far all tests and tutorials i\'ve been trying, to get the structure in my head, show me that a view is bound to 1 model.

lets say i have a small app to store and

2条回答
  •  再見小時候
    2020-12-23 12:30

    In that case, I'd consider a dynamic model with both of your sub-models. In your route handler, you could do something like this:

    var model = new Backbone.Model();
    model.set({contacts: new ContactsModel(), users: new UsersModel()});
    var view = new GridView({model: model});
    

    Of course, there is nothing stopping you from passing in the models separately:

    var contacts = new ContactsModel();
    var users = new UsersModel();
    var view = new GridView({model: contacts, users: users})
    

    I still prefer the composite approach of the first because it doesn't conflate what the model is.

提交回复
热议问题