コレクションがあり、2 つの異なるビュー間で選択されている現在のモデルに関する情報を共有しようとしています。
-->簡単に言うと、あるビューで選択したモデルに別のビューからアクセスして、モデルの属性を変更/割り当てできるようにしたい
最初のビューは次のように定義されています。
App.Views.Person = Backbone.View.extend({
tagName: 'a',
template: template('personTemplate'),
initialize: function(){
this.model.on('change', this.render, this);
this.model.on('destroy', this.remove, this);
},
render: function() {
this.$el.html( this.template(this.model.toJSON()) );
this.input = this.$('.view');
return this;
},
2 番目のビューは次のように定義されます。
App.Views.App = Backbone.View.extend({
el: 'html',
initialize: function(){
_.bindAll(this,"render");
},
render: function() {
return this;
},
そして、次のようにビューを作成しました
addPersonView = new App.Views.AddPerson({ collection: peopleCollection });
appView = new App.Views.App({ model: person, collection: peopleCollection });
2 番目のビューで選択されたモデルが、コレクションから取得された最初のビューのモデルと同じになるようにするにはどうすればよいですか --> たとえば、下のビューの入力ボックスに何かを入力すると、使用できるようにする:this.set.model({name:title})
これにより、トップビューで選択された要素(モデルに関連付けられた)のモデル属性を設定しますがthis.set.model
、最初のビューで選択された正しいモデルを選択していません
さらなる参照と混乱のために: 私のモデルは、配列からロードしている次のコードを使用して PeopleView に追加されています。
App.Views.People = Backbone.View.extend({
// tagName: '',
initialize: function() {
var i = 1;
while(i < size)
{
var person = new App.Models.Person({ url: jsArray[i] });// creating a new person object..
this.collection.add(person);
i++
}
this.collection.on('add', this.addOne, this);
console.log(jsArray[1]);
// listeners/anouncers for the collection on add..
},
// refactored render method...
render: function() {
this.collection.each(this.addOne, this);
return this;
},
// called from render method of collection view..
addOne: function(person) {
var personView = new App.Views.Person({ model: person, vent: vent });
this.$el.append(personView.render().el);
}
});