javascript Backbone.js:带有 http 查询字符串的 urlRoot?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9884179/
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: urlRoot with http query string?
提问by Lai Yu-Hsuan
In Backbone.js
I can appoint where the model fetches it's data:
在Backbone.js
我可以指定模型获取数据的位置:
var Book = Backbone.Model.extend({urlRoot : '/books'});
var mybook = new Book({id: "1"});
mybook.fetch(); //it will access '/books/1'
But if I want to append a query string after the URL? e.g. the book data is at /books/1&details=true
. Can I specify this in model?
但是如果我想在 URL 后附加一个查询字符串呢?例如,书籍数据位于/books/1&details=true
。我可以在模型中指定这个吗?
回答by Shuping
You can also use the option for the method fetch
您还可以使用 fetch 方法的选项
mybook.fetch({data:{details: true}});
回答by abraham
You will have to use a custom url function for the model.
您必须为模型使用自定义 url 函数。
Book.url = function() {
return this.urlRoot + '/' + this.id + '?details=true';
};