您可以使用只允许一个模型的集合。这样您就不会接触模型,并且可以根据需要多次调用渲染。像这样的东西:
var SpecialCollection = Backbone.Collection.extend({
swap: function (model) {
//remove all models
this.reset();
//add one model
this.add(model);
}
});
var MyView = Backbone.View.extend({
initialize: function(){
this.listenTo(this.collection, 'add', this.render);
},
render: function() {
this.model = this.collection.first()
//do your normal rendering here
}
});
var c = new SpecialCollection();
var v = new MyView({collection: c});
c.swap({name: 'Sam'});
//view should render
c.swap({name: 'Dave'});
//view should render
您可以进一步锁定 Collection 规则,但我认为它是一个很好的例子,可以让您继续前进。