사용 backbone.js 내 서버에서 모델을 가져 와서이를 기반으로 밑줄 템플릿을 렌더링하려고합니다. 먼저 다음 호출 함수를 사용하여 API 호출 결과를 사용하지 않고 시도했습니다.myModel.fetch()가 완료되면 백본 템플릿을 렌더링 하시겠습니까?
render: function(options) {
this.ticketId = options.ticketId;
var that = this;
var ticketModel = new TicketModel({'id': that.ticketId});
$.when(ticketModel.fetch()).done(function(){
console.log(ticketModel); // this outputs the correct model
});
var template = _.template($('#tab-content-template').html(), {ticketId: that.ticketId});
this.$el.html(template);
},
이것은 완벽하게 작동합니다.
render: function(options) {
this.ticketId = options.ticketId;
var that = this;
var ticketModel = new TicketModel({'id': this.ticketId});
$.when(ticketModel.fetch()).done(function(){
console.log(ticketModel);
console.log($('#tab-content-template').html());
var template = _.template($('#tab-content-template').html(), {ticketId: that.ticketId});
this.$el.html(template);
});
},
하지만 불행히도,이 이상한 것은 그것이 콘솔에서 HTML을 출력한다는 것입니다
Uncaugt TypeError: Cannot read property 'html' of undefined.
라는 오류가 발생합니다 그래서 템플릿을 렌더링하는 API를 호출의 결과를 사용하여 시도 console.log($('#tab-content-template').html());
에서 올바르게 발생합니다. 내가 얻는 오류는 읽는 줄에 있습니다. this.$el.html(template);
어떻게 처음으로 html()을 얻을 수 있고 그 후에 html 속성을 찾을 수 없다고 할 수 있습니까? 나는 여기에 완전히 붙어있어 .. :
모든 팁을 환영합니다!
여기에서 새로운 것을 배웠습니다. 가져 오기가 $를 (를) 반환했습니다.지연 객체, 건배 – Quince