Skip to content
Browse files

Fix `fetchRelated` with `refresh=true`; properly include current models

  • Loading branch information...
1 parent 68bf90f commit c3a378b6377b820ce87c8a28c413bd44bf342099 @PaulUithol committed Mar 27, 2013
Showing with 13 additions and 6 deletions.
  1. +13 −6 backbone-relational.js
View
19 backbone-relational.js
@@ -1229,15 +1229,22 @@
var setUrl,
requests = [],
rel = this.getRelation( key ),
- keys = rel && ( rel.keyIds || [ rel.keyId ] ),
- toFetch = keys && _.select( keys || [], function( id ) {
- return ( id || id === 0 ) && ( refresh || !Backbone.Relational.store.find( rel.relatedModel, id ) );
- }, this );
+ idsToFetch = rel && ( rel.keyIds || ( ( rel.keyId || rel.keyId === 0 ) ? [ rel.keyId ] : [] ) );
+
+ // On `refresh`, add the ids for current models in the relation to `idsToFetch`
+ if ( refresh ) {
+ var models = rel.related instanceof Backbone.Collection ? rel.related.models : [ rel.related ];
+ _.each( models, function( model ) {
+ if ( model.id || model.id === 0 ) {
+ idsToFetch.push( model.id );
+ }
+ });
+ }
- if ( toFetch && toFetch.length ) {
+ if ( idsToFetch && idsToFetch.length ) {
// Find (or create) a model for each one that is to be fetched
var created = [],
- models = _.map( toFetch, function( id ) {
+ models = _.map( idsToFetch, function( id ) {
var model = Backbone.Relational.store.find( rel.relatedModel, id );
if ( !model ) {

0 comments on commit c3a378b

Please sign in to comment.
Something went wrong with that request. Please try again.