错误:“undefined不是函数”backbone.js

时间:2012-01-31 07:30:24

标签: node.js backbone.js runtime-error

有谁能告诉我为什么这段代码会在var myview = new view();处抛出“未定义的不是函数”错误?我一直在寻找,我很确定一切都是对的。

router = Backbone.Router.extend({
    routes: {
        '/': 'project'
    },

    project: function() {

        projects = new models.Projects();

        var view = Backbone.View.extend({
            el: 'body' ,
            render : function() {
                $(this.el).empty().append(templates.Project({content : 'this works!' }));
                return this;
            }
        });
        console.log(view);
        /*
         * Output:
         * { [Function]
              extend: [Function],
              augment: [Function],
              toString: [Function],
              register: [Function],
              __super__: 
               { bind: [Function],
                 unbind: [Function],
                 trigger: [Function],
                 tagName: 'div',
                 '$': [Function],
                 initialize: [Function],
                 render: [Function],
                 remove: [Function],
                 make: [Function],
                 delegateEvents: [Function],
                 _configure: [Function],
                 _ensureElement: [Function],
                 html: [Function],
                 toString: [Function],
                 template: [Function] } }
        */
        var myview = new view();
        this.send(myview, {collection: projects});
    }
});

2 个答案:

答案 0 :(得分:0)

尝试

怎么样?
new view

而不是

new view();

或者尝试传递一个模型:

new view({});

答案 1 :(得分:0)

设置el:

时需要使用JQuery
var view = Backbone.View.extend({
        el: $('body'),
        render: function() {
            $(this.el).empty().append(templates.Project({content : 'this works!' }));
            return this;
        }
    });
var myview = new view();

查看this answer了解详情。