2012-12-04 53 views
1

我有一个Backbone视图,例如,一个编辑配置文件视图,我将视图交给我的用户模型实例。不过,例如,我希望允许用户从选择下拉菜单中选择他们最喜欢的社交网络。现在,这个社交网络列表由他们填充在应用程序的另一个区域。骨干 - 将两个集合交给一个视图

我的问题是,1.什么是存储这些社交网络的最佳方式?每个模型,所以社交网络的集合?和2.然后我将一个集合(社交网络)和一个模型(用户)传递给我的编辑配置文件视图?

这是最好的方法吗?

回答

8

基本上你可以使用初始化函数做这种东西。你可以将另一个作为模型或集合传递给另一个参数,而只需将其他参数保存为一个参数即可。你也可以看一个模型和一个集合。

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; 

希望这有助于!

+1

便士下降了:)谢谢! – benhowdle89