骨干渲染列表多次

时间:2015-07-01 08:20:34

标签: javascript json backbone.js

我从服务器获取json并呈现它时遇到了问题。问题是每个json对象都会再次渲染。

Traceback (most recent call last):
  File "ch6_1.py", line 64, in <module>
    send_email(app ,MAIL_USERNAME, "test mail", "hello")
  File "ch6_1.py", line 50, in send_email
    msg.body = render_template(template + '.txt', **kwargs)
  File "D:\INSTALL\Python\lib\site-packages\flask\templating.py", line 126, in r
ender_template
    ctx.app.update_template_context(context)
AttributeError: 'NoneType' object has no attribute 'app'

这是我的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中有两个对象,所有两个对象都被渲染,所以在页面上有一个列表而不是一个列表。有什么想法吗?

ANSWER! &#39;更新&#39;活动对我有用。从规范&#34;在从集合中添加或删除任意数量的模型后触发的单个事件。&#34;这正是我所需要的。也许这会帮助遇到同样问题的人。

1 个答案:

答案 0 :(得分:0)

&#39;更新&#39;活动对我有用。从规范&#34;在从集合中添加或删除任意数量的模型后触发的单个事件。&#34;这正是我所需要的。也许这会帮助遇到同样问题的人。