【发布时间】:2014-07-18 10:35:13
【问题描述】:
使用backbone.js 我正在尝试从我的服务器获取一个模型,并在此基础上呈现一个下划线模板。我首先尝试使用以下渲染函数而不使用 api 调用的结果:
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);
},
这很好用。所以我尝试使用api调用的结果来渲染模板:
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);
});
},
但不幸的是,这会导致错误提示
Uncaugt TypeError:无法读取未定义的属性“html”。
奇怪的是它在控制台中正确输出了由console.log($('#tab-content-template').html()); 生成的html。我得到的错误是this.$el.html(template);
怎么会先能拿到html(),然后又说找不到属性html呢?我完全被困在这里..:S
欢迎所有提示!
【问题讨论】:
-
从中学到了一些新东西,没有意识到 fetch 返回了 $.deferred 对象,干杯
标签: javascript jquery templates backbone.js underscore.js