我在两种情况下更改了MyModel的longitude
和latitude
属性,但我的MyView只注意到其中一个:
update
中设置(lat,long)并且未触发任何更改事件。navigator.geolocation.getCurrentPosition
:success
的回调中设置(lat,long)并且更改事件被触发。var MyModel = Backbone.Model.extend({
initialize: function() {
_.bindAll(this,'success','update');
this.update();
},
update: function() {
if(!FAKE_LOCATION){
if(navigator.geolocation)
navigator.geolocation.getCurrentPosition(this.success);
}else{
this.set({
latitude : 37,
longitude : -122
//previously, I was using constants here
});
}
},
success: function(position) {
this.set({
latitude : position.coords.latitude,
longitude : position.coords.longitude
});
}
});
var MyView = Backbone.View.extend({
initialize: function(){
this.listenTo(this.model,'change', this.render);
}
});
var myModel = new MyModel();
var myView = new MyView({ model : myModel });
答案 0 :(得分:0)
假设FAKE_LOCATION_LAT
和FAKE_LOCATION_LONG
是常量,那么您在latitude
方法初始化期间设置longitude
和.initialize()
。对[{1}}的后续调用实际上不会更改该值,因此不会发生任何更改,也不会触发.set()
个事件。
将此添加到您的代码中以确保值实际更改:
change