2014-07-18 2 views
1

미안하지만, 기본이지만 Backbone.js의 총 시작 자라서 얻은 데이터로 속성 (데이터)을 지정하는 방법을 찾지 못했습니다. 술책. 나는 이것이 바인딩 (이)과 관련이 있다고 믿지만, 나는 그것을 이해할 수 없다. 그러나 나는 그것을 할당하고 나의보기에의 데이터 속성을 사용할 수 없습니다 Backbone JS : 가져 오기 호출에서 모델 속성 할당

var form_model = Backbone.Model.extend({ 
    urlRoot: QuoteForm.ajaxurl, 
    data: "", 

    initialize: function() 
    { 
     this.fetch({ 
      data: { action: 'quote_form_data' }, 
      success: function (response) { 
       // this bit won't assign 
       this.data = response.toJSON(); 
      } 
     }); 
    } 

    }); 

내가 CONSOLE.LOG()이 올바른지 반환 된 데이터 : 여기에 내 코드입니다. 도와주세요.

답변

1

편집 : 바인딩하지 않고, 제대로이 포인터를 설정합니다이 바인딩 window 객체

var form_model = Backbone.Model.extend({ 
urlRoot: QuoteForm.ajaxurl, 
data: "", 

initialize: function() 
{ 
    this.fetch({ 
     data: { action: 'quote_form_data' }, 
     success: function (response) { 
      // this bit won't assign 
      this.data = response.toJSON(); 
     }.bind(this) 
    }); 
} 

}); 

에 성공 콜백 점에서이 포인터는, 네이티브 .bind 방법은 EMCAScript 작동 5 개 브라우저와 위. 때문에 백본 밑줄 JS에 따라, 당신은

var form_model = Backbone.Model.extend({ 
urlRoot: QuoteForm.ajaxurl, 
data: "", 

initialize: function() 
{ 
    this.fetch({ 
     data: { action: 'quote_form_data' }, 
     success: _.bind(function (response) { 
      // this bit won't assign 
      this.data = response.toJSON(); 
     }, this) 
    }); 
} 

}); 
1

더 많은 독자 친화적 인 버전이 될 것 대신에 별도의 호환성을 위해이 작업을 수행 할 수

var form_model = Backbone.Model.extend({ 
    urlRoot: QuoteForm.ajaxurl, 
    data: "", 

    initialize: function() { 
     var _this = this; 
     this.fetch({ 
      data: { action: 'quote_form_data' }, 
      success: function (response) { 
       // more reader freindly version would be; 
       _this.data = response.toJSON(); 
      } 
     }); 
    } 

});