基本上你可以使用初始化函數做這種東西。你可以將另一個作爲模型或集合傳遞給另一個參數,而只需將其他參數保存爲一個參數即可。你也可以看一個模型和一個集合。
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