mirror of
https://github.com/jashkenas/backbone.git
synced 2026-04-08 03:00:26 -04:00
Adding an error when URLs are left unspecified, and highlighting the importance of the URL property in the docs for persistence to work.
This commit is contained in:
@@ -678,6 +678,7 @@
|
||||
// Helper function to get a URL from a Model or Collection as a property
|
||||
// or as a function.
|
||||
var getUrl = function(object) {
|
||||
if (!(object && object.url)) throw new Error("A 'url' property or function must be specified");
|
||||
return _.isFunction(object.url) ? object.url() : object.url;
|
||||
};
|
||||
|
||||
|
||||
13
index.html
13
index.html
@@ -190,7 +190,7 @@
|
||||
<li>– <a href="#Collection-comparator">comparator</a></li>
|
||||
<li>– <a href="#Collection-sort">sort</a></li>
|
||||
<li>– <a href="#Collection-pluck">pluck</a></li>
|
||||
<li>– <a href="#Model-url">url</a></li>
|
||||
<li>– <a href="#Collection-url">url</a></li>
|
||||
<li>– <a href="#Collection-fetch">fetch</a></li>
|
||||
<li>– <a href="#Collection-refresh">refresh</a></li>
|
||||
<li>– <a href="#Collection-create">create</a></li>
|
||||
@@ -586,6 +586,15 @@ setInterval(function() {
|
||||
}, 10000);
|
||||
</pre>
|
||||
|
||||
<p>
|
||||
<i>
|
||||
<b>Cautionary Note:</b> When fetching or saving a model, make sure that the model is part of
|
||||
a collection with a <a href="#Collection-url">url</a> property specified,
|
||||
or that the model itself has a complete <a href="#Model-url">url</a> function
|
||||
of its own, so that the request knows where to go.
|
||||
</i>
|
||||
</p>
|
||||
|
||||
<p id="Model-save">
|
||||
<b class="header">save</b><code>model.save(attributes, [options])</code>
|
||||
<br />
|
||||
@@ -678,6 +687,8 @@ one.set({
|
||||
</p>
|
||||
|
||||
<p>
|
||||
Delegates to <a href="#Collection-url">Collection#url</a> to generate the
|
||||
URL, so make sure that you have it defined.
|
||||
A model with an id of <tt>101</tt>, stored in a
|
||||
<a href="#Collection">Backbone.Collection</a> with a <tt>url</tt> of <tt>"/notes"</tt>,
|
||||
would have this URL: <tt>"/notes/101"</tt>
|
||||
|
||||
@@ -40,6 +40,15 @@ $(document).ready(function() {
|
||||
|
||||
test("Model: url", function() {
|
||||
equals(doc.url(), '/collection/1-the-tempest');
|
||||
doc.collection = null;
|
||||
var failed = false;
|
||||
try {
|
||||
doc.url();
|
||||
} catch (e) {
|
||||
failed = true;
|
||||
}
|
||||
equals(failed, true);
|
||||
doc.collection = collection;
|
||||
});
|
||||
|
||||
test("Model: clone", function() {
|
||||
|
||||
Reference in New Issue
Block a user