我正在构建的 Ember 应用程序使用 Leaflet.js 作为其大地图。我在模型上有一个观察者,它向地图添加了一个向量并保持更新:
Qp.Region = DS.Model.extend({
// Attributes
name: DS.attr('string'),
maxLat: DS.attr('number'),
minLat: DS.attr('number'),
minLon: DS.attr('number'),
maxLon: DS.attr('number'),
// Helper properties
_vector: null,
// Computed properties
leafletBounds: function() {
var properties = ['minLat', 'maxLat', 'minLon', 'maxLon'],
bounds = [];
for ( var i = 0; i < 2; i++ ) {
var lat = Number(this.get(properties[i])),
lng = Number(this.get(properties[i + 2]));
if ( lat !== lat || lng !== lng )
return;
bounds.pushObject(L.latLng({
lat: lat,
lng: lng
}));
}
return bounds;
}.property('minLat', 'maxLat', 'minLon', 'maxLon'),
boundsDidChange: function() {
var existingVector = this.get('_vector'),
vector = existingVector || Ember.Object.create({
_layer: null,
model: this
}),
bounds = this.get('leafletBounds');
if ( !bounds )
return;
vector.set('bounds', bounds);
if ( !existingVector ) {
Qp.L.regions.pushObject(vector);
this.set('_vector', vector);
}
}.observes('leafletBounds'),
shouldRemoveSelf: function() {
if ( !this.get('isDeleted') && !this.get('isDestroying') )
return;
var existingVector = this.get('_vector');
if ( existingVector ) {
Qp.L.regions.removeObject(existingVector);
this.set('_vector', null);
}
}.observes('isDeleted', 'isDestroying')
})
注意这与 Ember Data rev 完美配合。0.13 .
现在我更新到 Ember Data 1.0 beta 2,矢量不再添加到地图中。如果我在初始化时保存对模型的引用...
init: function() {
this._super.apply(this, arguments);
window.test = this;
}
...并window.test.boundsDidChange()
从 Chrome 开发工具控制台调用,我的灯具中最后一个区域的矢量出现了。因此,我知道一切仍在工作,除了模型数据加载时不再调用观察者。
如何让boundsDidChange
观察者在模型加载或更新时触发?