Javascript 主干渲染列表多次出现
我从服务器获取json并呈现它时遇到问题。问题是每个json对象都会被再次渲染Javascript 主干渲染列表多次出现,javascript,json,backbone.js,Javascript,Json,Backbone.js,我从服务器获取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 =
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中有两个对象,所有两个对象都被呈现,所以我得到了两个列表,而不是页面上的一个列表。有什么想法吗
回答强>
“更新”活动对我有效。根据规范“在集合中添加或删除任意数量的模型后触发单个事件”。这正是我所需要的。也许这会对遇到同样问题的人有所帮助。“更新”活动对我来说很有效。根据规范“在集合中添加或删除任意数量的模型后触发单个事件”。这正是我所需要的。也许这会对遇到同样问题的人有所帮助。“更新”活动对我来说很有效。根据规范“在集合中添加或删除任意数量的模型后触发单个事件”。这正是我所需要的。也许这会对遇到同样问题的人有所帮助。“更新”活动对我很有效。根据规范“在集合中添加或删除任意数量的模型后触发单个事件”。这正是我所需要的。也许这会对遇到同样问题的人有所帮助。请随意将其作为答案发布,这将使其更有可能在将来帮助他人:)@Ivan我相信这意味着你应该将其作为问题的答案发布,而不是编辑问题以包含答案(这样,未来的访问者更容易找到答案)。要做到这一点,请使用问题下方的回答后表格。一旦你添加了答案,你也应该接受它(通过勾选答案左边的复选标记),但请注意,你可能需要等待一天才能这样做。是的,谢谢你的澄清!最后我做到了:)“更新”活动对我很有效。根据规范“在集合中添加或删除任意数量的模型后触发单个事件”。这正是我所需要的。也许这会对遇到同样问题的人有所帮助。请随意将其作为答案发布,这将使其更有可能在将来帮助他人:)@Ivan我相信这意味着你应该将其作为问题的答案发布,而不是编辑问题以包含答案(这样,未来的访问者更容易找到答案)。要做到这一点,请使用问题下方的回答后表格。一旦你添加了答案,你也应该接受它(通过勾选答案左边的复选标记),但请注意,你可能需要等待一天才能这样做。是的,谢谢你的澄清!最后我做到了:)
[{
"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
}]
}]