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
?
相关问题
- Backbone.js PushState routes .htaccess only workin
- Updating a LayoutView's Model
- Disable Backbone.js hashes entirely, but keep push
- Is there an easy way to distribute a Flask server
- React + Backbone, Target container is not a DOM el
相关文章
- Get all models in backbone collection where attrib
- How can I dynamically set a className for a Backbo
- Nesting Views within Views in backbone js
- Backbone-relational hasmany best practices
- JavaScript error: “is not a constructor”
- Marionette + i18n in templates
- Rendering reCAPTCHA v2.0 widget within Backbone vi
- Backbone.js PUT/DELETE problems with Codeigniter R
.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 theurlRoot
.Example 2. Calling fetch on a model which is a part of a collection uses the collection's
url
as theurlRoot
Example 3.
url
set on a model will literally use that url for any model instance.Example 4.
url
can be a function and it starts to make sense again.