基本上你可以使用初始化函数做这种东西。你可以将另一个作为模型或集合传递给另一个参数,而只需将其他参数保存为一个参数即可。你也可以看一个模型和一个集合。
var EditProfileView = Backbone.View.extend({
initialize: function(options) {
this.user = options.user:
// or do this
this.socialNetworks = options.socialNetworks;
}
});
// when creating an instance do
var foo = new EditProfileView({user: someUser, collection: socialNetworks});
// or
var foo = new EditProfileView({model: someUser, socialNetworks: socialNetworks});
// also this will work as well
var foo = new EditProfileView({model: someUser, collection:socialNetWorks});
另外,如果你不想为它分配,你可以做
var foo = new EditProfileView({user: someUser, socialNetworks: socialNetworks});
// the options given to a view on initialization
// are saved to an options property in the view
// so the following should hold true
foo.options.user === someUser;
foo.options.socialNetworks === socialNetworks;
希望这有助于!
便士下降了:)谢谢! – benhowdle89