我在我的網站的一部分上收到了一個非常奇怪的錯誤,該錯誤由backbone.js,backbone.marionette構建。我正在使用underscore.js模板和TESTypie作爲REST框架。Underscore.js - xyz未定義(8超出範圍5)
Firebug輸出「ReferenceError:xyz(<%= xyz%>在下劃線模板中)未定義」。 (8超出範圍5)。
當我回溯到underscore-min.js文件時,它沒有8行,因爲它是一個min文件。這是可以預料的。
但是,我懷疑一個語法錯誤,而不是手動操作控制檯中的變量。
我可以從REST框架中手動定義名稱模型獲取並在其視圖中呈現它。我甚至可以使用MyApp.testSection.show(「渲染視圖」)正確地將其輸出到前端,而沒有任何問題。
我懷疑一些錯誤的語法要麼NameView模板中:_.template(...)部分或MyApp.addInitializer
下面的代碼
Name = TastypieModel.extend({
urlRoot:'/api/v1/names/Calvin'
});
//returns an array {'name':'Calvin', 'age':32, etc....}
NameView = Backbone.Marionette.ItemView.extend({
model: Name,
template: _.template("<h1>My name is <%= name %></h1>"),
tagName: 'p',
initialize: function(){
this.bindTo(this.model, "change", this.render);
}
});
MyApp.addInitializer(function(options){
var name = new Name();
name.fetch();
var nameview = new NameView ({
model: name
});
MyApp.testSection.show(nameview);
});
做創造和展示的成功回調內部視圖獲取method.Since它是一個異步調用它可能會產生問題 – deven98602
木偶使用下劃線作爲默認編譯你templates.So u能避免這樣做的工作。 – deven98602
您的模板聲明看起來不錯。你的渲染方法在哪裏?那麼testSection.show()會做什麼?在渲染方法中,確保模型在傳遞到模板時調用.toJSON(),因爲Backbone模型屬性實際存儲在model.attributes中。 –