간단한 날씨 위젯을 만들고 있습니다. 현재 기상 조건을 National Weather Service xml 파일에서 읽은 다음 모델에서 관련 데이터를 구문 분석하고 저장하려고하지만 $ .ajax에 대한 콜백이 연결되지 않습니다 (내가하는 방식).Backbone.Model 콜백 문제 및이
var Weather = Backbone.Model.extend({
initialize: function(){
_.bindAll(this, 'update', 'startLoop', 'stopLoop');
this.startLoop();
},
startLoop: function(){
this.update();
this.interval = window.setInterval(_.bind(this.update, this), 1000 * 60 * 60);
},
stopLoop: function(){
this.interval = window.clearInterval(this.interval);
},
store: function(data){
this.set({
icon : $(data).find('icon_url_name').text()
});
},
update: function(){
$.ajax({
type: 'GET',
url: 'xml/KROC.xml',
datatype: 'xml'
})
.done(function(data) {
var that = this;
that.store($(data).find('current_observation')[ 0 ]);
});
}
});
var weather = new Weather();
데이터가 올바르게 읽혀 지지만 콜백의 완료 기능을 통해 저장소 기능을 호출 할 수 없습니다. .. 단지 구문 분석하고 "this.set"을 할 것 "완료"경우 (나는 행복 할 것이다 당신의 도움에 미리
감사
대안 :'.done (_. bind (function() {...}, this))' – Yaroslav
그게 문제였습니다. 고맙습니다. –
가장 유용한 명명 규칙은 외부 범위에서'var _this = this;'입니다. –