Javascript 主干:必须指定“url”属性或函数
我正在获取一些数据并用它创建一个集合,如下所示:Javascript 主干:必须指定“url”属性或函数,javascript,backbone.js,Javascript,Backbone.js,我正在获取一些数据并用它创建一个集合,如下所示: reader: function(fileName){ console.log(fileName); _this = this; $.ajax({ type: "GET", url: "some/rest/url", data: {"fileName": fileName}, success: funct
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"
我的模型是这样的:
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"
],,
functionBaseModel{
return BaseModel.extend({
idAttribute: "id",
url: "some/rest/url",
});
});
理想情况下,我希望在同步时发生的是,集合会将其所有模型发布到后端进行验证,但我一直遇到这样的错误:必须指定url属性或函数如果直接调用sync,则需要传入它期望的参数。现在,您没有向importCollection传递任何内容.sync调用,因此当主干尝试解析此处要使用的url时
if (!options.url) {
params.url = _.result(model, 'url') || urlError();
}
它找不到url,将执行urlError。您需要调用重载的fetch方法,或者在同步调用中传递正确的参数。请注意,fetch for Backbone.Collection通过以下方式调用同步:
return this.sync('read', this, options);