1

我在控制台中收到错误消息“对象 [对象对象] 没有方法“应用”。请看小提琴.. http://jsfiddle.net/vineet85/AQx63/6/

var RefreshingView = new Backbone.View.extend({
    initialize: function() {
        this.model.on('change', this.render, this);
    },
    render: function() {
        this.$el.html(this.model.get('text'));
    }
});

var m = new Backbone.Model({text: new Date().toString()});
var myView = new RefreshingView({model: m, el: 'body'});

myView.render();

setInterVal(function() {
    m.set({text: new Date().toString()});
}, 1000);
4

1 回答 1

2

您的语法有点偏离,首先要“声明”您的视图和模型,您应该省略 new 关键字,其次您不应该尝试直接“实例化” backbone.js 模型,请尝试以下操作

    var RefreshingView = Backbone.View.extend({
        el: 'body',

        initialize: function () {
            this.model.on('change', this.render, this);
        },
        render: function () {
            this.$el.html(this.model.get('text'));
        }
    });

    var MyModel = Backbone.Model.extend({
        text: null
    });

    var m = new MyModel({ text: new Date().toString() });
    var myView = new RefreshingView({ model: m, el: 'body' });

myView.render();

setInterval(function () {
    m.set({ text: new Date().toString() });
}, 1000);

更新了 jsFiddle

于 2013-01-22T00:15:27.163 回答