[Pkg-javascript-commits] [backbone] 01/173: update modelId docs

Jonas Smedegaard dr at jones.dk
Wed Aug 31 07:43:55 UTC 2016


This is an automated email from the git hooks/post-receive script.

js pushed a commit to branch master
in repository backbone.

commit d0fd17070948f6a647d66ccb08c1d065df361611
Author: Adam Krebs <amk528 at cs.nyu.edu>
Date:   Wed Jun 24 11:16:27 2015 -0400

    update modelId docs
---
 index.html | 52 ++++++++++++++++++++++++++++++----------------------
 1 file changed, 30 insertions(+), 22 deletions(-)

diff --git a/index.html b/index.html
index de7ccc6..fa8e0cb 100644
--- a/index.html
+++ b/index.html
@@ -1739,7 +1739,7 @@ bill.set({name : "Bill Jones"});
     </p>
 
     <p id="Collection-model">
-      <b class="header">model</b><code>collection.model</code>
+      <b class="header">model</b><code>collection.model([attrs], [options])</code>
       <br />
       Override this property to specify the model class that the collection
       contains. If defined, you can pass raw attributes objects (and arrays) to
@@ -1774,33 +1774,41 @@ var Library = Backbone.Collection.extend({
 </pre>
 
     <p id="Collection-modelId">
-      <b class="header">modelId</b><code>collection.modelId</code>
+      <b class="header">modelId</b><code>collection.modelId(attrs)</code>
       <br />
-      Override this method to specify the attribute the collection will use to
-      refer to its models in <a href="#Collection-get">collection.get</a>.
-      <br />
-      By default returns the <tt>idAttribute</tt> of the collection's model
-      class or failing that, <tt>'id'</tt>. If your collection uses polymorphic
-      models and those models have an <tt>idAttribute</tt> other than <tt>id</tt>
-      you must override this method with your own custom logic.
+      Override this method to return the value the collection will use to
+      identify a model given its attributes. Useful for combining models from
+      multiple tables with different <a href="Model-idAttribute"><tt>idAttribute</tt></a>
+      values into a single collection.
     </p>
 
-<pre>
-var Library = Backbone.Collection.extend({
+    <p>
+      By default returns the value of the attributes'
+      <a href="Model-idAttribute"><tt>idAttribute</tt></a>
+      from the collection's model class or failing that, <tt>id</tt>. If
+      your collection uses a <a href="#Collection-model">model factory</a> and
+      those models have an <tt>idAttribute</tt> other than <tt>id</tt> you must
+      override this method.
+    </p>
 
-  model: function(attrs, options) {
-    if (condition) {
-      return new PublicDocument(attrs, options);
+<pre class="runnable">
+var Library = Backbone.Collection.extend({
+  modelId: function(attrs) {
+    if (attrs.type === 'dvd') {
+      return 'dvd-' + attrs.dvd_id;
     } else {
-      return new PrivateDocument(attrs, options);
+      return 'vhs-' attrs.vhs_id;
     }
-  },
-
-  modelId: function(attrs) {
-    return attrs.private ? 'private_id' : 'public_id';
   }
-
 });
+
+var library = new Library([
+  {type: 'dvd', dvd_id: 1},
+  {type: 'vhs', vhs_id: 1}
+]);
+
+alert(library.get('dvd-1').id + ' ' + library.get('vhs-1').id);
+
 </pre>
 
     <p id="Collection-constructor">
@@ -4263,7 +4271,7 @@ ActiveRecord::Base.include_root_in_json = false
         in 1.2.0.
       </li>
     </ul>
-  
+
     <b class="header">1.2.0</b> — <small><i>May 13, 2015</i></small>
     — <a href="https://github.com/jashkenas/backbone/compare/1.1.2...1.2.0">Diff</a>
     — <a href="https://cdn.rawgit.com/jashkenas/backbone/1.2.0/index.html">Docs</a>
@@ -4286,7 +4294,7 @@ ActiveRecord::Base.include_root_in_json = false
       <li>
         Views now always delegate their events in <a href="#View-setElement">setElement</a>.
         You can no longer modify the events hash or your view's <tt>el</tt> property in
-        <tt>initialize</tt>. 
+        <tt>initialize</tt>.
       </li>
       <li>
         Added an <tt>"update"</tt> event that triggers after any amount of

-- 
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