是什么VS urlRoot URL之间的区别 [英] What is the difference between url vs urlRoot
问题描述
我想知道什么是使用之间的网址
或 urlRoot
的差异。我已阅读文档( backbonejs.org/#Model-url ),但我还是觉得我缺乏知识,在这一领域我想知道更多。如果你有使用网址
?而在另一种情况下你必须使用时, urlRoot
?
.urlRoot
仅在模型可用,并且是唯一有用的当任一种模式是无法一部分集合,或当你想忽略它的模型是部分集合的的.url
属性。
在换句话说,一个模型不需要也不的.url
也不 .urlRoot
属性时,它的一部分以的.url
属性设置的集合,在这种情况下,这种模式将使用该集合的的.url
,因为它是自己的 .urlRoot
。
下面几个例子说明的差别。当您运行脚本,在HTTP请求中可以看到浏览器的网络面板。
的例1。的帖子不是集合的一部分。 urlRoot
定义网址的基本组成部分。当一个模型是牵强,它的ID被追加到 urlRoot
。
VAR邮政= Backbone.Model.extend({\r
urlRoot:HTTP://jsonplaceholder.typi$c$c.com/posts\r
});\r
\r
VAR secondPost =新邮({ID:2});\r
secondPost.fetch();\r
/ *请求HTTP://jsonplaceholder.typi$c$c.com/posts/2 * /
\r
<脚本的src =HTTP://$c$c.jquery。 COM / jquery.js和'>< / SCRIPT>\r
<脚本的src =HTTP://underscorejs.org/underscore.js'>< / SCRIPT>\r
<脚本的src =HTTP://backbonejs.org/backbone.js'>< / SCRIPT>
\r
的例2 的调用获取一个模型,它是一个集合的一部分,采用了集合的网址
为 urlRoot
VAR邮政= Backbone.Model.extend();\r
VAR帖子= Backbone.Collection.extend({\r
网址:'HTTP://jsonplaceholder.typi$c$c.com/posts',\r
型号:帖子\r
});\r
\r
VAR帖=新帖();\r
posts.add({ID:2});\r
posts.first()取()。\r
/ *请求HTTP://jsonplaceholder.typi$c$c.com/posts/2 * /
\r
<脚本的src =HTTP://$c$c.jquery。 COM / jquery.js和'>< / SCRIPT>\r
<脚本的src =HTTP://underscorejs.org/underscore.js'>< / SCRIPT>\r
<脚本的src =HTTP://backbonejs.org/backbone.js'>< / SCRIPT>
\r
的例3。的网址
设置模型将逐字将该网址用于任何模型的实例。
VAR邮政= Backbone.Model.extend({\r
网址:'HTTP://jsonplaceholder.typi$c$c.com/posts\r
});\r
VAR secondPost =新邮({ID:2});\r
secondPost.fetch();\r
/ *请求HTTP://jsonplaceholder.typi$c$c.com/posts * /
\r
<脚本的src =HTTP://$c$c.jquery。 COM / jquery.js和'>< / SCRIPT>\r
<脚本的src =HTTP://underscorejs.org/underscore.js'>< / SCRIPT>\r
<脚本的src =HTTP://backbonejs.org/backbone.js'>< / SCRIPT>
\r
的例4。的网址
可以是一个功能,它开始再次意义。
VAR邮政= Backbone.Model.extend({\r
网址:函数(){\r
回归的http://jsonplaceholder.typi$c$c.com/posts/'+ this.get('塞');\r
}\r
});\r
VAR secondPost =新邮({弹头:2});\r
secondPost.fetch();\r
/ *请求HTTP://jsonplaceholder.typi$c$c.com/posts/2 * /
\r
<脚本的src =HTTP://$c$c.jquery。 COM / jquery.js和'>< / SCRIPT>\r
<脚本的src =HTTP://underscorejs.org/underscore.js'>< / SCRIPT>\r
<脚本的src =HTTP://backbonejs.org/backbone.js'>< / SCRIPT>
\r
I would like to know what is the difference between using url
or urlRoot
. I have read the documentation (backbonejs.org/#Model-url), but I still feel that I lack knowledge in this area and I would like to know more. When do you have to use url
? And in another instance when do you have to use urlRoot
?
.urlRoot
is only available in a Model, and is only useful when either a model is not part of a collection, or when you want to override the .url
property of the collection which that model is part of.
In other words, a model doesn't require neither .url
nor .urlRoot
property when it is part of a collection with a .url
property set, in which case this model will use that collection's .url
as it's own .urlRoot
.
Here are several examples that show the difference. When you run the scripts, the http requests can be seen in browser's network panel.
Example 1. Post is not part of a collection. urlRoot
defines the base part of the url. When a model is fetched, it's id is appended to the urlRoot
.
var Post = Backbone.Model.extend({
urlRoot: 'http://jsonplaceholder.typicode.com/posts'
});
var secondPost = new Post({ id: 2 });
secondPost.fetch();
/*requests http://jsonplaceholder.typicode.com/posts/2 */
<script src='http://code.jquery.com/jquery.js'></script>
<script src='http://underscorejs.org/underscore.js'></script>
<script src='http://backbonejs.org/backbone.js'></script>
Example 2. Calling fetch on a model which is a part of a collection uses the collection's url
as the urlRoot
var Post = Backbone.Model.extend();
var Posts = Backbone.Collection.extend({
url: 'http://jsonplaceholder.typicode.com/posts',
model: Post
});
var posts = new Posts();
posts.add({id: 2});
posts.first().fetch();
/*requests http://jsonplaceholder.typicode.com/posts/2 */
<script src='http://code.jquery.com/jquery.js'></script>
<script src='http://underscorejs.org/underscore.js'></script>
<script src='http://backbonejs.org/backbone.js'></script>
Example 3. url
set on a model will literally use that url for any model instance.
var Post = Backbone.Model.extend({
url: 'http://jsonplaceholder.typicode.com/posts'
});
var secondPost = new Post({ id: 2 });
secondPost.fetch();
/*requests http://jsonplaceholder.typicode.com/posts */
<script src='http://code.jquery.com/jquery.js'></script>
<script src='http://underscorejs.org/underscore.js'></script>
<script src='http://backbonejs.org/backbone.js'></script>
Example 4. url
can be a function and it starts to make sense again.
var Post = Backbone.Model.extend({
url: function(){
return 'http://jsonplaceholder.typicode.com/posts/' + this.get('slug');
}
});
var secondPost = new Post({ slug: 2 });
secondPost.fetch();
/*requests http://jsonplaceholder.typicode.com/posts/2 */
<script src='http://code.jquery.com/jquery.js'></script>
<script src='http://underscorejs.org/underscore.js'></script>
<script src='http://backbonejs.org/backbone.js'></script>
这篇关于是什么VS urlRoot URL之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!