1

我有一个 Backbone 视图,例如,一个 Edit Profile 视图,我将在其中将视图交给我的 User 模型实例。但是,例如,我希望允许用户从选择下拉列表中选择他们最喜欢的社交网络。现在,这个社交网络列表由他们在应用程序的另一个区域填充。

我的问题是,1. 存储这些社交网络的最佳方式是什么?每个人都有一个模型,所以是社交网络的集合?和 2. 然后我是否将集合(社交网络)和模型(用户)传递给我的 Edit Profile 视图?

这是最好的方法吗?

4

1 回答 1

8

基本上你可以使用 initialize -function 来做这种事情。您可以将另一个作为模型或集合传递,另一个作为一些参数传递,您只需保存对它的引用。您也可以给视图一个模型和一个集合。

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;

希望这可以帮助!

于 2012-12-04T10:08:19.767 回答