I have 2 views and need to pass values between them.
我有2个视图,需要在它们之间传递值。
var appModel1 = Backbone.Model.extend({
url: '/',
defaults: {
name: 'Default name'
},
validate: function(attr){
}
});
var appCollection1 = Backbone.Collection.extend({
model: appModel1
});
var collection1 = new appCollection1();
var model1 = new appModel1();
model1.bind('change', function(){
model1.save();
});
var appView1 = Backbone.View.extend({
model: model1,
_modelDataBind: undefined,
el:'#container1',
initialize: function(){
this._modelDataBind = new Backbone.ModelBinder();
this.render();
},
render: function(){
var template = _.template($('#app1').html());
this.$el.html(template);
var bindings = {
name: '#txtName'
};
var changeTrigger = {
'changeTriggers': {
'': 'keyup',
'#txtName': 'keyup'
}
};
this._modelDataBind.bind(this.model, this.$el, bindings, changeTrigger);
},
'events': {
'click #btnSubmit': 'addRecord'
},
addRecord: function(){
collection1.push(model1);
var item = '<li>Name: ' + model1.get('name') + '</li>';
var app2 = new appView2({collection: collection1});
$('#txtName').val('').focus();
}
});
var view1 = new appView1({});
var appView2 = Backbone.View.extend({
el: '#container2',
initialize: function(){
this.render();
},
render: function(){
var template = _.template($('#app2').html());
this.$el.html(template);
this.collection.each(function(model){
var item = '<li>Name: ' + model.get('name') + '</li>';
$('#infoList').append(item);
});
}
});
var view2 = new appView2({});
As of now, I have only been able to pass values using collection and id variables.
截至目前,我只能使用collection和id变量传递值。
Are they reserved variables?
它们是保留变量吗?
Can i use any other variable to pass data?
我可以使用任何其他变量来传递数据吗?
var app2 = new appView2({nameObj: nameObj});
this fails.
1 个解决方案
#1
2
As per docs, Backbone view only acts upon model, collection, el, id, className, tagName, attributes and events
properties of options object.
根据文档,Backbone视图仅作用于选项对象的model,collection,el,id,className,tagName,attributes和events属性。
However, you can pass any other data you want, the whole options
object will be available as the first parameter of initialize
callback.
但是,您可以传递任何其他所需的数据,整个选项对象将作为初始化回调的第一个参数。
For example:
var AppView = Backbone.View.extend({
el: '#container2',
initialize: function(options){
// access your data here
this.valuableInformation = options.valuableInformation;
if(!options.doNotRender)
this.render();
},
render: function(){
}
});
new AppView({
valuableInformation:"it's nothing",
doNotRender: true
});
#1
2
As per docs, Backbone view only acts upon model, collection, el, id, className, tagName, attributes and events
properties of options object.
根据文档,Backbone视图仅作用于选项对象的model,collection,el,id,className,tagName,attributes和events属性。
However, you can pass any other data you want, the whole options
object will be available as the first parameter of initialize
callback.
但是,您可以传递任何其他所需的数据,整个选项对象将作为初始化回调的第一个参数。
For example:
var AppView = Backbone.View.extend({
el: '#container2',
initialize: function(options){
// access your data here
this.valuableInformation = options.valuableInformation;
if(!options.doNotRender)
this.render();
},
render: function(){
}
});
new AppView({
valuableInformation:"it's nothing",
doNotRender: true
});