【问题标题】:Uncaught TypeError: Cannot call method 'get' of undefined : BackBone未捕获的类型错误:无法调用未定义的方法“get”:BackBone
【发布时间】:2014-03-21 06:46:15
【问题描述】:

我正在骨干网中运行这个示例程序。但是我收到一个错误,例如“无法调用未定义的方法 'get'”。我用我目前所知道的尝试了一切。但我仍然有问题。谁能帮我解决这个问题。

(function(){
    //Providing a global scope
    window.App = {
        Models : {},
        Views: {},
        Collections: {}
    };

    window.template = function(id){
        return _.template($('#'+id).html());
    };

    // Declared a Model for Task       
    App.Models.Task = Backbone.Model.extend({    
    });

    App.Collections.Tasks = Backbone.Collection.extend({
        model: App.Models.Task
    });

    App.Views.Tasks = Backbone.View.extend({   
        tagName: 'ul',

        render: function(){
            this.collection.each(this.addOne,this);
        },

        addOne: function(task){
            //creating a child view 
            var taskView = new App.Views.Task({model: task});

            //append it to root element
            this.$el.append(taskView.render().el);
        }
    });     

    App.Views.Task = Backbone.View.extend({
        tagName: 'li',
        render : function(){
            this.$el.html(this.model.get('title'));
            return this;
        }
    });         

    var tasksCollection = new App.Collections.Tasks([
        {
        title: 'Goto Store',
        priority: 4
        },
        {
        title: 'Goto Store2',
        priority: 5
        },
        {
        title: 'Goto Store3',
        priority: 6
        },
        {
        title: 'Goto Store4',
        priority: 7
        }
    ]);

    var tasksView = new App.Views.Task({collection : tasksCollection});
    tasksView.render();
    console.log(tasksView.el);
    // $(document.body).append(tasksView.el);       
})();

【问题讨论】:

  • 最后一行})();的代码是什么?
  • 这是一个立即调用的函数表达式。

标签: javascript backbone.js collections model views


【解决方案1】:

我不知道这是否是印刷错误,但如果不是,您的问题是您将收藏设置在 Task 视图中,而不是在 Tasks 视图中.

var tasksView = new App.Views.Tasks({collection : tasksCollection});

【讨论】:

    猜你喜欢
    • 2013-08-31
    • 2011-11-12
    • 2013-09-14
    • 2013-06-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多