[Pkg-javascript-commits] [backbone] 31/34: Adding an error when URLs are left unspecified, and highlighting the importance of the URL property in the docs for persistence to work.
Jonas Smedegaard
js at moszumanska.debian.org
Sat May 3 16:58:46 UTC 2014
This is an automated email from the git hooks/post-receive script.
js pushed a commit to tag 0.1.2
in repository backbone.
commit f0f7c8d5e3daba3779d1d19566727e16462874f4
Author: Jeremy Ashkenas <jashkenas at gmail.com>
Date: Tue Oct 19 10:47:40 2010 -0400
Adding an error when URLs are left unspecified, and highlighting the importance of the URL property in the docs for persistence to work.
---
backbone.js | 1 +
index.html | 13 ++++++++++++-
test/model.js | 9 +++++++++
3 files changed, 22 insertions(+), 1 deletion(-)
diff --git a/backbone.js b/backbone.js
index 501224b..638cc91 100644
--- a/backbone.js
+++ b/backbone.js
@@ -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;
};
diff --git a/index.html b/index.html
index 8f7a35c..8af0cd7 100644
--- a/index.html
+++ b/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>
diff --git a/test/model.js b/test/model.js
index 1573bb0..dff0196 100644
--- a/test/model.js
+++ b/test/model.js
@@ -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() {
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/backbone.git
More information about the Pkg-javascript-commits
mailing list