javascript - 传单 + Backbone.js : how to update the view on model collection update?

标签 javascript node.js backbone.js leaflet

我在 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/

相关文章:

javascript - 如何在javascript中将字符串对象转换为json对象

Javascript简易计时器不起作用

javascript - 如何以 JSON 格式提取网页中的最后一条推文?

Node.js 异步回调工厂

javascript - 适用于服务器和客户端的模板语言

javascript - 跨路线持久模型和 View 的设计模式

javascript - 我无法测试在主干 View 中呈现的元素的可见性

javascript - 附加表行时不复制按钮

javascript - Meteor 对象在控制台中可用,但抛出“无法读取未定义的属性 findOne”

sockets - 在node.js中接收数据