首先,我對Backbone.js是全新的,我正在使用this example進行學習。Uncaught ReferenceError:高度未定義 - Backbone.js
我想從文本輸入中獲取值並從輸入中創建模型。應該加載該模板,並且應該將每個模型的屬性height
顯示在相同的HTML上。
我能夠創建模型並將它們添加到集合,因爲我在console.log(Sections)中看到n {length: 105, models: Array[105], _byId: Object}
。然而,(我猜)當我嘗試加載模板時,它給了我錯誤:'未捕獲的ReferenceError:高度未定義'。我在想如果我錯誤地獲取了屬性。
任何幫助,將不勝感激!先謝謝你。
這裏是模板:
<script type="text/template" id="section-template">
<div class="view">
<label><%- height %></label>
<label><%- color %></label>
<a class="destroy"></a>
</div>
</script>
型號:
var Section = Backbone.Model.extend({
defaults: function(){
return {
height: 200,
color: ''
};
}
});
模型系列:
var SectionList = Backbone.Collection.extend({
model: Section,
localStorage: new Backbone.LocalStorage("sections-backbone")
});
var Sections = new SectionList;
模型視圖&事件操作:
var SectionView = Backbone.View.extend({
tagName: "li",
template: _.template($("#section-template").html()),
initialize: function() {
},
render: function() {
this.$el.html(this.template(this.model.toJSON()));
return this;
}
});
提出申請:
var AppView = Backbone.View.extend({
el: $("#sectionboard"),
events: {
"keypress #new-height": "createOnEnter"
},
initialize: function() {
this.input = this.$("#new-height");
this.main = $("#main");
Sections.fetch();
},
createOnEnter: function(e) {
if (e.keyCode != 13) return;
if (!this.input.val()) return;
Sections.create({height: this.input.val(), color: '#FFFFFF'});
this.input.val("");
console.log(Sections);
var view = new SectionView({model: Sections});
this.$("#section-list").append(view.render().el);
}
});
var App = new AppView;
爲了表明問題已解決,請接受解決此問題的答案http://stackoverflow.com/help/someone-answers而不是更新標題 –