javascript 带有自定义获取 URL 的 Backbone.js
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18383205/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Backbone.js with a custom fetch URL
提问by Keith Hymanson
I am trying to set up a variant fetch method on my backbone model that will fetch the current model for a given user. This is available from the API on /api/mealplans/owner/{username}/current
.
我正在尝试在我的主干模型上设置一个变体获取方法,该方法将为给定用户获取当前模型。这可以从 上的 API 获得/api/mealplans/owner/{username}/current
。
I have written the following model. I commented out the URL Root, as the prototype fetch call was simply using the urlRoot
and I wanted to see if that was overriding the url parameter I passed in portions somehow.
我编写了以下模型。我注释掉了 URL Root,因为原型 fetch 调用只是使用urlRoot
,我想看看它是否覆盖了我以某种方式传入的 url 参数。
var mealPlan = Backbone.Model.extend({
name: 'Meal Plan',
//urlRoot: '/api/mealplans',
defaults: {},
fetchCurrent: function (username, attributes, options) {
attributes = attributes || {};
options = options || {};
if (options.url === undefined) {
options.url = "/api/mealplans/owner/" + username + "/current";
}
return Backbone.Model.prototype.fetch.call(this, attributes, options);
},
validate: function (attributes) {
// To be done
return null;
}
});
I've seen this done, in some variations in other places, such as at backbone.js use different urls for model save and fetch- In that case the code is slightly different (I started with that and broke it down to make it easier for me to read.)
我已经看到这样做了,在其他地方的一些变化中,例如在backbone.js 使用不同的 url 进行模型保存和获取- 在这种情况下,代码略有不同(我从它开始并将其分解以使其更容易供我阅读。)
The options object has the url parameter in it fine when I pass it to fetch, but then it seems to ignore it!
当我将它传递给 fetch 时,options 对象中的 url 参数很好,但它似乎忽略了它!
回答by Keith Hymanson
I was assuming the same parameters to fetch as to save - This is not the case.
我假设要获取的参数与要保存的参数相同 - 情况并非如此。
The method signature for fetch ONLY takes 'options' and not 'attributes', hence the url parameter wasn't found.
fetch 的方法签名仅采用“选项”而不是“属性”,因此未找到 url 参数。
The model code should look a bit more like this..
模型代码应该看起来更像这样..
var mealPlan = Ministry.Model.extend({
name: 'Meal Plan',
urlRoot: '/api/mealplans',
defaults: {
},
fetchCurrent: function (username, options) {
options = options || {};
if (options.url === undefined) {
options.url = this.urlRoot + "/owner/" + username + "/current";
}
return Backbone.Model.prototype.fetch.call(this, options);
},
validate: function (attributes) {
// To be done
return null;
}
});
回答by Nadir
I think it is better to override url() method, like below:
我认为最好覆盖 url() 方法,如下所示:
var mealPlan = Ministry.Model.extend({
name: 'Meal Plan',
urlRoot: '/api/mealplans',
//--> this gets called each time fetch() builds its url
url: function () {
//call the parent url()
var url=Backbone.Model.prototype.url.call(this);
//here you can transform the url the way you need
url += "?code=xxxx";
return url;
}
...
besides, in your example above I think there is a mistake and you should replace fetchCurrentby fetch
除此之外,在你上面的例子我认为这是一个错误,你应该更换fetchCurrent通过取