Tôi có các chế độ xem sau đây trong ứng dụng của mình. Về cơ bản tôi muốn gọi show_house() trong App.MapView khi li của App.HouseListElemView được nhấp.Chức năng xem cuộc gọi từ một giao diện khác - Backbone
Cách tốt nhất để làm điều này là gì?
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');
}
});
đẹp .. cảm ơn – AlexBrand
+1. Xin lỗi sự thiếu hiểu biết của tôi, nhưng sự khác biệt giữa phương pháp này/appstate là gì và mở rộng [Backbone.Events] (http://lostechies.com/derickbailey/2012/04/03/revisiting-the-backbone-event-aggregator-lessons- đã học /) cho PubSub truyền thống? – TYRONEMICHAEL
@TyroneMichael: Dễ dàng kiên trì đối với hầu hết các phần. PubSub chỉ định tuyến thông tin xung quanh và quên đi nó, một mô hình ghi nhớ và làm cho nó dễ dàng duy trì trạng thái đến máy chủ của bạn. –