我在 Leaflet 和 Backbone.js 之间集成时遇到问题。我在网上找到了一个样本 ( http://jsfiddle.net/dPqQy/ ),其中包含以下 map View 和模型:
App.Views.MapView = Backbone.View.extend({
id:"map",
render: function() {
//render map element
var map = this.map = L.map(this.$el.attr('id'))
.setView([this.model.get('centerLon'), this.model.get('centerLat') ], 13)
.addLayer(L.tileLayer(this.model.get('layerUrl'), { maxZoom: 18 }));
//render each marker
this.markerViews = this.model.get('markers').map(function(marker) {
return new App.Views.MarkerView({model:marker, map:map}).render();
});
}
});
var map = new App.Models.Map({
centerLon: 51.505,
centerLat: -0.09,
layerUrl: 'http://{s}.tile.cloudmade.com/BC9A493B41014CAABB98F0471D759707/997/256/{z}/{x}/{y}.png',
markers: new App.Collections.Markers([
{id:1, latitude:-0.09, longitude:51.505},
{id:2, latitude:-0.092, longitude:51.503},
{id:3, latitude:-0.086, longitude:51.506}
])
});
var mapView = new App.Views.MapView({model:map});
mapView.render();
现在,问题是当我尝试使用以下方法更新模型中的标记集合时:
map.get('markers').add(node);
( Node 是我要添加的自定义标记, View 未更新且标记未显示在 map 中。我做错了什么?谢谢!
最佳答案
也许你应该把它放在你的 View 中
initialize: function () {
this.listenTo(this.model, 'add', this.render);
},
这也会更新您的 View 和 map
关于javascript - 传单 + Backbone.js : how to update the view on model collection update?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22154721/