Backbone 多次渲染列表
Backbone rendering list multiple times
我 运行 在从服务器获取 json 并呈现它时遇到了问题。问题是每个 json 对象都被再次渲染。
var Student = Backbone.Model.extend({
getConvertedToHash: function () {
return $.extend({}, this.attributes.student[0], this.attributes.student[1], this.attributes.student[2]);
}
});
var Group = Backbone.Collection.extend({
model: Student,
url: '/students.json'
});
var StudentView = Backbone.View.extend({
tagName: 'li',
className: 'alert alert-info',
initialize: function () {
_.bindAll(this, 'render');
},
render: function () {
this.$el.html(this.model.getConvertedToHash().name);
return this;
}
});
var GroupView = Backbone.View.extend({
el: $('.container'),
initialize: function () {
this.group = new Group();
this.group.on('add', this.render, this);
this.group.fetch({
success: function () {
console.log('Fetch successful!');
},
error: function(model, xhr, options) {
console.log('error');
}
});
},
render: function () {
var $ul = $('<ul>').addClass('student-list');
this.group.each(function (student, index) {
console.log(student.toJSON());
var studentView = new StudentView({model: student});
$ul.append(studentView.render().el);
});
this.$el.append($ul);
}
});
var groupView = new GroupView();
这是我的 json:
[{
"student": [{
"name": "john",
"lastName": "fox",
"middleName": "yonson"
},{
"age": 26,
"gender": "male"
},{
"passport": "qpuid5423",
"inn": 123542
}]
},{
"student": [{
"name": "peter",
"lastName": "powell",
"middleName": "rivierra"
},{
"age": 26,
"gender": "male"
},{
"passport": "qpuid5423",
"inn": 123542
}]
}]
我的 json 中有 2 个对象,并且所有两个对象都已呈现,所以我得到的不是页面上的 1 个列表,而是 2 个列表。有什么想法吗?
回答!
'update' 活动对我有用。从规范 "single event triggered after any number of models have been added or removed from a collection." 这正是我所需要的。也许这会帮助 运行 遇到同样问题的人。
'update' 活动对我有用。从规范 "single event triggered after any number of models have been added or removed from a collection." 这正是我所需要的。也许这会帮助 运行 遇到同样问题的人。
我 运行 在从服务器获取 json 并呈现它时遇到了问题。问题是每个 json 对象都被再次渲染。
var Student = Backbone.Model.extend({
getConvertedToHash: function () {
return $.extend({}, this.attributes.student[0], this.attributes.student[1], this.attributes.student[2]);
}
});
var Group = Backbone.Collection.extend({
model: Student,
url: '/students.json'
});
var StudentView = Backbone.View.extend({
tagName: 'li',
className: 'alert alert-info',
initialize: function () {
_.bindAll(this, 'render');
},
render: function () {
this.$el.html(this.model.getConvertedToHash().name);
return this;
}
});
var GroupView = Backbone.View.extend({
el: $('.container'),
initialize: function () {
this.group = new Group();
this.group.on('add', this.render, this);
this.group.fetch({
success: function () {
console.log('Fetch successful!');
},
error: function(model, xhr, options) {
console.log('error');
}
});
},
render: function () {
var $ul = $('<ul>').addClass('student-list');
this.group.each(function (student, index) {
console.log(student.toJSON());
var studentView = new StudentView({model: student});
$ul.append(studentView.render().el);
});
this.$el.append($ul);
}
});
var groupView = new GroupView();
这是我的 json:
[{
"student": [{
"name": "john",
"lastName": "fox",
"middleName": "yonson"
},{
"age": 26,
"gender": "male"
},{
"passport": "qpuid5423",
"inn": 123542
}]
},{
"student": [{
"name": "peter",
"lastName": "powell",
"middleName": "rivierra"
},{
"age": 26,
"gender": "male"
},{
"passport": "qpuid5423",
"inn": 123542
}]
}]
我的 json 中有 2 个对象,并且所有两个对象都已呈现,所以我得到的不是页面上的 1 个列表,而是 2 个列表。有什么想法吗?
回答! 'update' 活动对我有用。从规范 "single event triggered after any number of models have been added or removed from a collection." 这正是我所需要的。也许这会帮助 运行 遇到同样问题的人。
'update' 活动对我有用。从规范 "single event triggered after any number of models have been added or removed from a collection." 这正是我所需要的。也许这会帮助 运行 遇到同样问题的人。