私は自分のアプリケーションで次のビューを持っています。基本的には、App.HouseListElemViewのliをクリックするとApp.MapViewでshow_house()を呼びたいと思います。別のビューからビュー関数を呼び出す - バックボーン
これを行う最善の方法は何ですか?
App.HouseListElemView = Backbone.View.extend({
tagName: 'li',
events: {
'click': function() {
// call show_house in App.MapView
}
},
initialize: function() {
this.template = _.template($('#house-list-template').html());
this.render();
},
render: function() {
var html = this.template({model: this.model.toJSON()});
$(this.el).append(html);
},
});
App.MapView = Backbone.View.extend({
el: '.map',
events: {
'list_house_click': 'show_house',
},
initialize: function() {
this.map = new GMaps({
div: this.el,
lat: -12.043333,
lng: -77.028333,
});
App.houseCollection.bind('reset', this.populate_markers, this);
},
populate_markers: function(collection) {
_.each(collection.models, function(house) {
var html = 'hello'
this.map.addMarker({
lat: house.attributes.lat,
lng: house.attributes.lng,
infoWindow: {
content: html,
}
});
}, this);
},
show_house: function() {
console.log('show house');
}
});
美しい。ありがとう – AlexBrand
+1。私の無知を許すが、この方法とappstateとの違いは何か[Backbone.Events](http://lostechies.com/derickbailey/2012/04/03/revisiting-the-backbone-event-aggregator-lessons-学んだ/)伝統的なPubSub? – TYRONEMICHAEL
@TyroneMichael:ほとんどの場合、簡単に永続化します。 PubSubは情報を経路指定するだけで忘れてしまうので、モデルは記憶しておき、サーバに状態を永続化させるのが簡単になります。 –