Skip to content

Commit 42b158d

Browse files
committed
Add getIdsToFetch to RelationalModel. Ref #463
1 parent 290ade0 commit 42b158d

File tree

3 files changed

+52
-16
lines changed

3 files changed

+52
-16
lines changed

backbone-relational.js

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1358,12 +1358,36 @@
13581358
return _.values( this._relations );
13591359
},
13601360

1361+
1362+
/**
1363+
* Get a list of ids that will be fetched on a call to `fetchRelated`.
1364+
* @param {string|Backbone.Relation} key The relation key to fetch models for.
1365+
* @param [refresh=false] Add ids for models that are already in the relation, refreshing them?
1366+
* @return {Array} An array of ids that need to be fetched.
1367+
*/
1368+
getIdsToFetch: function( key, refresh ) {
1369+
var rel = key instanceof Backbone.Relation ? key : this.getRelation( key ),
1370+
ids = rel ? ( rel.keyIds && rel.keyIds.slice( 0 ) ) || ( ( rel.keyId || rel.keyId === 0 ) ? [ rel.keyId ] : [] ) : [];
1371+
1372+
// On `refresh`, add the ids for current models in the relation to `idsToFetch`
1373+
if ( refresh ) {
1374+
var models = rel.related && ( rel.related.models || [ rel.related ] );
1375+
_.each( models, function( model ) {
1376+
if ( model.id || model.id === 0 ) {
1377+
ids.push( model.id );
1378+
}
1379+
});
1380+
}
1381+
1382+
return ids;
1383+
},
1384+
13611385
/**
13621386
* Retrieve related objects.
13631387
* @param {string} key The relation key to fetch models for.
13641388
* @param {Object} [options] Options for 'Backbone.Model.fetch' and 'Backbone.sync'.
13651389
* @param {Boolean} [refresh=false] Fetch existing models from the server as well (in order to update them).
1366-
* @return {jQuery.when[]} An array of request objects
1390+
* @return {jQuery.when[]} An array of request objects.
13671391
*/
13681392
fetchRelated: function( key, options, refresh ) {
13691393
// Set default `options` for fetch
@@ -1373,17 +1397,7 @@
13731397
setUrl,
13741398
requests = [],
13751399
rel = this.getRelation( key ),
1376-
idsToFetch = rel && ( ( rel.keyIds && rel.keyIds.slice( 0 ) ) || ( ( rel.keyId || rel.keyId === 0 ) ? [ rel.keyId ] : [] ) );
1377-
1378-
// On `refresh`, add the ids for current models in the relation to `idsToFetch`
1379-
if ( refresh ) {
1380-
models = rel.related instanceof Backbone.Collection ? rel.related.models : [ rel.related ];
1381-
_.each( models, function( model ) {
1382-
if ( model.id || model.id === 0 ) {
1383-
idsToFetch.push( model.id );
1384-
}
1385-
});
1386-
}
1400+
idsToFetch = rel && this.getIdsToFetch( rel, refresh );
13871401

13881402
if ( idsToFetch && idsToFetch.length ) {
13891403
// Find (or create) a model for each one that is to be fetched

index.html

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@
7272
<ul>
7373
<li><a href="#RelationalModel-getRelation">getRelation</a></li>
7474
<li><a href="#RelationalModel-getRelations">getRelations</a></li>
75+
<li><a href="#RelationalModel-getIdsToFetch">getIdsToFetch</a></li>
7576
<li><a href="#RelationalModel-fetchRelated">fetchRelated</a></li>
7677
<li><a href="#RelationalModel-set">set</a></li>
7778
<li><a href="#RelationalModel-toJSON">toJSON</a></li>
@@ -297,6 +298,7 @@ <h2>
297298
<li><a href="#RelationalModel-build">build</a></li>
298299
<li><a href="#RelationalModel-findOrCreate">findOrCreate</a></li>
299300
<li><a href="#RelationalModel-find">find</a></li>
301+
<li><a href="#RelationalModel-findModel">findModel</a></li>
300302
</ul>
301303

302304
<h3 id="RelationalModel-properties">
@@ -777,12 +779,21 @@ <h4 class="code">
777779
</p>
778780
</section>
779781

782+
<section id="RelationalModel-getIdsToFetch">
783+
<h4 class="code">
784+
getRelations<code>relationModel.getIdsToFetch(key&lt;string|Backbone.Relation&gt;, )</code>
785+
</h4>
786+
<p>
787+
Returns: <q>Array</q> A list of the ids that will be fetched when calling `fetchRelated`.
788+
</p>
789+
</section>
790+
780791
<section id="RelationalModel-fetchRelated">
781792
<h4 class="code">
782-
fetchRelated<code>relationalModel.fetchRelated(key&lt;string&gt;, [options&lt;object&gt;], [update&lt;boolean&gt;])</code>
793+
fetchRelated<code>relationalModel.fetchRelated(key&lt;string&gt;, [options&lt;object&gt;], [refresh&lt;boolean&gt;])</code>
783794
</h4>
784795
<p>
785-
Returns: <q>deferred[]</q> An array of request objects.
796+
Returns: <q>deferred[]</q> An array of (zero or more) request objects.
786797
</p>
787798
<p>
788799
Fetch models from the server that were referenced in the model's attributes, but have not been found/created yet.
@@ -939,7 +950,7 @@ <h4 class="code">
939950
<section id="RelationalModel-find">
940951
<h4 class="code">
941952
find
942-
<code>relationalModel.findOrCreate(attributes&lt;string|number|object&gt;, [options&lt;object&gt;])</code>
953+
<code>relationalModel.find(attributes&lt;string|number|object&gt;, [options&lt;object&gt;])</code>
943954
</h4>
944955
<p>
945956
Returns: <q>Backbone.RelationalModel</q> A model instance.
@@ -1238,6 +1249,11 @@ <h4>Master
12381249
<a href="https://raw.github.com/PaulUithol/Backbone-relational/master/backbone-relational.js">download</a>
12391250
</small>
12401251
</h4>
1252+
<ul>
1253+
<li>
1254+
Add `getIdsToFetch` to `Backbone.RelationalModel`.
1255+
</li>
1256+
</ul>
12411257

12421258
<h4>0.8.8
12431259
<small>

test/tests.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -955,6 +955,9 @@ $(document).ready(function() {
955955
resource_uri: 'person-10',
956956
user: 'user-10'
957957
});
958+
959+
var idsToFetch = person.getIdsToFetch( 'user' );
960+
deepEqual( idsToFetch, [ 'user-10' ] );
958961

959962
var requests = person.fetchRelated( 'user', { error: function() {
960963
errorCount++;
@@ -987,6 +990,9 @@ $(document).ready(function() {
987990
var zoo = new Zoo({
988991
animals: [ { id: 'monkey-1' }, 'lion-1', 'zebra-1' ]
989992
});
993+
994+
var idsToFetch = zoo.getIdsToFetch( 'animals' );
995+
deepEqual( idsToFetch, [ 'lion-1', 'zebra-1' ] );
990996

991997
//
992998
// Case 1: separate requests for each model
@@ -996,7 +1002,7 @@ $(document).ready(function() {
9961002
equal( requests.length, 2, "Two requests have been made (a separate one for each animal)" );
9971003
equal( zoo.get( 'animals' ).length, 3, "Three animals in the zoo" );
9981004

999-
// Triggering the 'error' callback for a request should destroy the model
1005+
// Triggering the 'error' callback for one request should destroy the model
10001006
requests[ 0 ].error();
10011007
// Trigger the 'success' callback on the `destroy` call to fire the 'destroy' event
10021008
_.last( window.requests ).success();

0 commit comments

Comments
 (0)