0

我是 Backbone.js 和 Parse 的新手,如果我不够具体,我深表歉意。

我有一个在初始化时抛出 TypeError 的视图,即使我通过删除有问题的行来修复,也只会在代码中抛出另一个完全可以接受的错误。

这是我得到的错误:

Uncaught TypeError: Cannot call method 'bind' of undefined views.js:59
Uncaught TypeError: Cannot call method 'bind' of undefined views.js:59

这是视图的 init 函数,第 59 和 60 行:

initialize: function() {
    this.model.bind("reset", this.render, this);
    this.model.bind("add", this.appendNewJob, this);
}

如果您需要更多代码,请在评论中告诉我。更新:根据要求,我的 render 和 appendNewJob 函数:

render: function(eventName) {
    _.each(this.model.models, function(job){
        this.appendNewJob(job);
    }, this);
    this.delegateEvents();
    return this.el;
},
appendNewJob: function(job){
    this.$el.append(new JobListItemView({
        model: job
    }).render());
}

我的路由器:

var AppRouter = Parse.Router.extend({
    initialize: function() {
        $('#header').html(new HeaderView().render());
    },
    routes: {
        "": "list",
        "jobs": "list",
        "settings": "viewSettings"
    },
    list: function() {
        var self = this;
        FB.getLoginStatus(function(response){
            if(response.status === 'connected') {
                // logged in.
                self.before(function() {
                    self.showView('#content', new JobListView());
                });
            } else if(response.status === 'not_authorized') {
                // not authorized.
            } else {
                // not connected.
                $('#app').hide();
                self.before(function() {
                    self.showView('#login', new StartView());
                });
            }
        });
    },
    viewSettings: function() {
        var self = this;
        FB.getLoginStatus(function(response){
            if(response.status === 'connected') {
                // logged in.
                self.before(function() {
                    self.showView('#content', new SettingsView());
                });
            } else if(response.status === 'not_authorized') {
                // not authorized.
            } else {
                // not connected.
                $('#app').hide();
                self.before(function() {
                    self.showView('#login', new StartView());
                });
            }
        });
        $('#filterBar').hide();
    },
    showView: function(selector, view) {
        if(this.currentView) this.currentView.close();
        $(selector).html(view.render());
        this.currentView = view;
        return view;
    },
    before: function(callback) {
        if(this.jobList) {
            if(callback) callback.call(this);
        } else {
            this.jobList = new JobCollection();
            var self= this;
            this.jobList.fetch({
                success: function() {
                    var joblist = new JobListView({
                        model: self.jobList
                    }).render();
                    $('#content').html(joblist);
                    if(callback) callback.call(self);
                }
            });
        }
    }
});

更新:我还使用 parse js 库代替骨干。

4

1 回答 1

1

您正在初始化视图而不传递model选项。该错误是一个标准的 javascript 错误,它告诉您第 59 行this.model未定义:

this.model.bind("reset", this.render, this);

视图不能有this.model属性,除非您将模型赋予视图!

所以而不是:

new JobListView();

你需要初始化它

new JobListView({model: <insert your model here>});
于 2012-12-12T12:11:38.343 回答