简体   繁体   中英

Uncaught TypeError: Cannot call method 'get' of undefined : BackBone

I am running this sample program in backbone. But I am getting a error like 'Cannot call method 'get' of undefined'. I tried everything with what i know so far. But I still have the problem. Can anyone help me to solve this issue.

(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);       
})();

我不知道这是否是印刷错误,但如果不是,您的问题是您在“ 任务”视图而不是在“ 任务”视图中设置了收藏集。

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM