骨干网-无法从指定的网址获取数据

时间:2018-10-30 09:57:35

标签: backbone.js

即使我很确定自己在集合中指定了URL,也遇到了以下错误: enter image description here

Uncaught Error: A `url property or function must be specified

这是我的收藏集

import $ from 'jquery';
import Backbone from 'backbone';
import ViewModel from './view-model';
import InstancesCollection from '../instances/instances-collection-model';

export default Backbone.Collection.extend({

    model: ViewModel,

    url: $.createURL('/api/view'),

    initialize: (models = []) => {
        models.forEach(element => {
            element.instances = new InstancesCollection(element.instances);
        });
    },

    parse: (response) => {
        return response.views;
    }
});

我试图遵循给出的here的回答,但徒劳无功。有人知道我在这里想念什么吗?

只要您也需要查看模型,就在这里:

    import _ from 'lodash';
import Backbone from 'backbone';
import InstancesCollection from '../instances/instances-collection-model';

export default Backbone.Model.extend({

    defaults: {
        id: null,
        name: null,
        count: 0,
        instances: []
    },

    initialize: function (models) {
        const attributes = Object.assign({}, this.defaults, models);

        if (_.isArray(attributes.instances)) {
            this.set({instances: new InstancesCollection(attributes.instances)}, {silent: true});
        }
    },

    toJSON: function () {
        let json = Object.assign({}, this.attributes);
        for (const attr of Object.keys(json)) {
            if ((json[attr] instanceof Backbone.Model) || (json[attr] instanceof Backbone.Collection)) {
                json[attr] = json[attr].toJSON();
            }
        }
        return json;
    }
});

任何帮助将不胜感激。谢谢。

0 个答案:

没有答案