Backbone: multiple View Models for the same model

前端 未结 4 1216
暗喜
暗喜 2021-02-05 23:25

Newbie backbone question:

Context: Building a shopping list with backbone

I have a model class called with name, description and tags (array) properties. I would

4条回答
  •  忘了有多久
    2021-02-05 23:38

    @machineghost is right on; The models are totally decoupled from the views so you can make as many views attached to the same model as you please. You could also extend a view, if they have logic or attributes you would like to share. When I use Backbone I often find myself extending a parent view just to override the render function, or to provide a different template.

    First view

    ShoppingCartView = Backbone.View.extend({
      model: ShoppingCart
      ...
    });
    

    Second independent view

    CheckoutView = Backbone.View.extend({
      model: ShoppingCart
      ...
    });
    

    Second view extends first

    CheckoutView = ShoppingCartView.extend({ 
      template: a_different_template // syntax depends on your templating framework
      ... 
    });
    

提交回复
热议问题