将json对象传递给集合会导致错误

时间:2015-02-16 22:58:32

标签: backbone.js requirejs

所以这是我的代码到目前为止。只是试图了解所有这些库如何工作,但无法找到问题。我使用RequireJS,BackboneJS,Handlebars。

正如您在ClientsView.js中看到的那样,我试图创建一个新的ClientsCollection实例并传递json对象。 这是我在chrome控制台中遇到的错误,如果你展开它:

Uncaught TypeError: Cannot read property 'prototype' of undefinedbackbone.js:689 i.extend.setbackbone.js:638 i.extend.addbackbone.js:769 i.extend.resetbackbone.js:607 e.Collectionbackbone.js:1566 sClientsView.js:17 Backbone.View.extend.initializebackbone.js:1001 e.Viewbackbone.js:1566 srouter.js:10 initializeapp.js:9 initializemain.js:15 (anonymous function)require.js:27 d.execCbrequire.js:10 orequire.js:12 arequire.js:12 orequire.js:12 arequire.js:12 orequire.js:12 arequire.js:12 orequire.js:12 arequire.js:12 orequire.js:12 arequire.js:12 orequire.js:15 xrequire.js:15 mrequire.js:21 g.completeLoadrequire.js:27 d.onScriptLoad

但是,如果我没有通过,则没有错误。

我检查了requireJS路径和名称,没有。

我必须要有一些非常愚蠢的东西。谢谢你的时间。

ClientModel.js

define([
    'underscore',
    'handlebars',
    'backbone'
], function(_, Handlebars, Backbone){

    var ClientModel = Backbone.Model.extend({
        defaults: {
            'ID' : '',
            'name' : '',
            'birthdate' : '',
            'email' : ''
        }
    });
});

ClientsCollection.js

define([
    'underscore',
    'handlebars',
    'backbone',
    'models/Client/ClientModel'
], function(_, handlebars, backbone, ClientModel){

    var ClientsCollection = Backbone.Collection.extend({

        model: ClientModel,

        //initialize: function(models, options) {},

        // comparator: function(client) {
        //  return client.get('name');
        // }
    });

    return ClientsCollection;
});

ClientsView.js

define([
    'jquery',
    'underscore',
    'handlebars',
    'backbone',
    'text!templates/clients/ClientsListTemplate.html',
    'collections/ClientsCollection/ClientsCollection'
], function($, _, Handlebars, Backbone, ClientsListTemplate, ClientsCollection){

    var ClientsView = Backbone.View.extend({

        tagName: 'div',

        template: Handlebars.compile(ClientsListTemplate),

        initialize: function() {
            this.collection = new ClientsCollection([ { 'ID' : 1, 'name' : 'egis', 'birthdate' : '02', 'email' : 'email' } ]);
            this.render();
        },

        render: function() {
            this.$el.html(this.template({ clients : this.collection.toJSON() }));
            return this;
        }
    });

    return ClientsView;
});

router.js

define([
    'jquery',
    'handlebars',
    'underscore',
    'backbone',
    'views/clients/ClientsView'
], function($, _, Handlebars, backbone, ClientsView){

    var initialize = function() {
        var newClientsView = new ClientsView();
        $('#clients-list').html(newClientsView.render().el);
    }

    return {
            initialize: initialize
        }
});

1 个答案:

答案 0 :(得分:1)

我认为您的模型缺少idAttribute

define([
    'underscore',
    'handlebars',
    'backbone'
], function(_, Handlebars, Backbone){

    var ClientModel = Backbone.Model.extend({

        idAttribute: 'ID',

        defaults: {
            'ID' : '',
            'name' : '',
            'birthdate' : '',
            'email' : ''
        }
    });
});

默认情况下,Backbone将查找小写id的属性名称,以唯一标识您的每个模型,除非您另有明确说明。