Backbone View无法识别该模型

时间:2014-02-22 13:26:26

标签: javascript backbone.js

模型

var MyModel = Backbone.Model.extend({
        url:'http://localhost:3000/getMiningInfo',
        defaults: {
           blocks: "",
        },
        parse: function(resp) {
           return resp;
        }
});
var info = new MyModel();
info.fetch();

查看

var MyInfoView = Backbone.View.extend({
    tagName:'table',
    id:'info',
    class:'table table-hover',
    template:_.template('<tbody><tr><td><span>Number Of Blocks</span></td>'+
                   '<td><span><%= blocks %> </span></td>'+
                '</tr></tbody>'),
    initialize: function() {
                   this.model.on('change', this.render, this);
    },
    render: function() {
           var attributes = this.model.toJSON();
           this.$el.html(this.template(attributes));
    } 
});
var myView = new MyInfoView({model: info});
myView.render();
$('#info').html(myView.el);

this.model.onthis.model.toJSON()的错误分别为Uncaught TypeError: Object #<HTMLDivElement> has no method 'on'no method 'toJSON'

1 个答案:

答案 0 :(得分:0)

我认为您希望视图监听模型的更改。

initialize: function() {

   this.listenTo( this.model, 'change', this.render ); 

}

Backbone建议在'on'上使用'listenTo'进行垃圾清理。