Backbone:必须指定“url”属性或函数

时间:2014-02-13 14:35:41

标签: javascript backbone.js

我正在获取一些数据并使用它创建一个集合:

reader: function(fileName){
        console.log(fileName);
        _this = this;
        $.ajax({
            type: "GET",
            url: "some/rest/url",
            data: {"fileName": fileName},
            success: function(response) {
                console.log("reader");
                console.log(response);
                importCollection = new ImportCollection(response);
                importCollection.sync();
            }
        });
    },

我的收藏如下:

define([
        "underscore",
        "backbone",
        "models/import",
    "helpers/localstorage"
   ],
function(_, Backbone, ImportModel, localstorage) {

return Backbone.Collection.extend({

    model: ImportModel,
    url: "some/rest/url",

    projectId: null,

    fetch: function(options){
        //TODO remove this hardcode
        console.log(options.url);
        this.url = this.url + "/PU000101/reader";

        Backbone.Collection.prototype.fetch.call(this,options);
    }

});

});

我的模型看起来像这样:

define([
"models/base"

] function(BaseModel){

return BaseModel.extend({

    idAttribute: "id",
    url: "some/rest/url",


});

});

理想情况下,我希望在同步时发生的是该集合会将其所有模型发布到后端进行验证,但我不断收到此错误:A "url" property or function must be specified

1 个答案:

答案 0 :(得分:0)

如果直接调用sync(),则需要传入它期望的参数。现在,您没有向importCollection.sync()电话传递任何内容,因此当Backbone尝试解决此处使用的网址时

if (!options.url) {
  params.url = _.result(model, 'url') || urlError();
}

它找不到网址并执行urlError()。您需要调用重载的fetch()方法,或在sync()调用中传递正确的参数。请注意,Backbone.Collection的fetch()以这种方式调用sync

return this.sync('read', this, options);
相关问题