Backbone.js backboneJS呈现视图与集合有关的问题:仅显示第一个添加的元素
我将backboneJS与requireJS和jQueryMobile一起使用 对于我的所有视图/集合,除了一个集合没有在每次添加时触发render方法,而只在第一次添加时触发render方法之外,一切都很好 代码如下: HTML: 和JavaScript:Backbone.js backboneJS呈现视图与集合有关的问题:仅显示第一个添加的元素,backbone.js,Backbone.js,我将backboneJS与requireJS和jQueryMobile一起使用 对于我的所有视图/集合,除了一个集合没有在每次添加时触发render方法,而只在第一次添加时触发render方法之外,一切都很好 代码如下: HTML: 和JavaScript: var NoteModel = Backbone.Model.extend({ initialize: function(attributes, options) { this.set({
var NoteModel = Backbone.Model.extend({
initialize: function(attributes, options)
{
this.set({
"id": attributes["id"] ? attributes["id"] : "",
"label": attributes["label"] ? attributes["label"] : "",
"text": attributes["text"] ? attributes["text"] : "",
"size" : attributes["size"] ? attributes["size"] : ""
});
},
});
var NotesCollection = Backbone.Collection.extend( {
model: NoteModel
});
// Extends Backbone.View
var BoardDetailsView = Backbone.View.extend({
/**
* The View Constructor
* @param el, DOM element of the page
*/
initialize: function(options)
{
this.collection.on("add", this.render, this);
},
render: function() {
this.$el.empty(); // clear the element to make sure you don't double
var self = this; // so you can use this inside the each function
this.collection.each(function(noteModel){
console.log('render note : ' + noteModel);
var id = noteModel.get("id");
var text = noteModel.get("text");
var li = "<li><a href=\"#note/"+id+"\">"+text+"</a></li>";
self.$el.append(li);
});
self.$el.listview().listview("refresh", true);
return this;
},
/**
* clear the view manually
*/
empty: function() {
this.$el.empty();
}
});
var note1 = new NoteModel({'noteId':'1', 'text':'label 1'});
var note2 = new NoteModel({'noteId':'2', 'text':'label 2'});
var note3 = new NoteModel({'noteId':'3', 'text':'label 3'});
window.app.boardDetailsView = new BoardDetailsView({el: '#listOfNotes', collection: new NotesCollection()});
window.app.boardDetailsView.collection.add(note1);
window.app.boardDetailsView.collection.add(note2);
window.app.boardDetailsView.collection.add(note3);
问题是,您的所有笔记都被分配了相同的id,实际上是一个空白字符串,这本身可能不是一个好主意,因为您正在检查id属性,但传入了noteId属性
var NoteModel = Backbone.Model.extend({
initialize: function(attributes, options)
{
this.set({
"id": attributes["noteId"] ? attributes["noteId"] : "",
"label": attributes["label"] ? attributes["label"] : "",
"text": attributes["text"] ? attributes["text"] : "",
"size" : attributes["size"] ? attributes["size"] : ""
});
}
});
这里有一个链接到
var NoteModel = Backbone.Model.extend({
initialize: function(attributes, options)
{
this.set({
"id": attributes["noteId"] ? attributes["noteId"] : "",
"label": attributes["label"] ? attributes["label"] : "",
"text": attributes["text"] ? attributes["text"] : "",
"size" : attributes["size"] ? attributes["size"] : ""
});
}
});