Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Correctly fetch nested relations on a HasMany relation fetched using a... #457

Closed
wants to merge 1 commit into from

2 participants

@eriks5

...set url.

This fixes #455

@PaulUithol
Owner

Thanks, encountered this while debugging getAsync myself.. should have merged this earlier ;). See 30b0abb

@PaulUithol PaulUithol closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Apr 28, 2014
  1. @eriks5
This page is out of date. Refresh to see the latest.
Showing with 152 additions and 1 deletion.
  1. +1 −1  backbone-relational.js
  2. +151 −0 test/tests.js
View
2  backbone-relational.js
@@ -1934,7 +1934,7 @@
// Add 'models' in a single batch, so the original add will only be called once (and thus 'sort', etc).
// If `parse` was specified, the collection and contained models have been parsed now.
toAdd = singular ? ( toAdd.length ? toAdd[ 0 ] : null ) : toAdd;
- var result = set.call( this, toAdd, _.defaults( { parse: false }, options ) );
+ var result = set.call( this, toAdd, _.defaults( { parse: false, merge: false }, options ) );
_.each( newModels, function( model ) {
// Fire a `relational:add` event for any model in `newModels` that has actually been added to the collection.
View
151 test/tests.js
@@ -4845,5 +4845,156 @@ $(document).ready(function() {
console.log( 'registerCount=%o, unregisterCount=%o', registerCount, unregisterCount );
});
+
+ module("Nested relations with HasMany");
+
+ test( "Separate requests for each object in the relation", function() {
+ var LeafModel = Backbone.RelationalModel.extend({
+ idAttribute: 'resource_uri'
+ }),
+ MiddleModel = Backbone.RelationalModel.extend({
+ idAttribute: 'resource_uri',
+ relations: [
+ {
+ type: Backbone.HasOne,
+ key: 'leaf',
+ relatedModel: LeafModel,
+ includeInJSON: 'resource_uri'
+ }
+ ]
+ }),
+ MiddleCollection = Backbone.Collection.extend({
+ model: MiddleModel
+ }),
+ TopModel = Backbone.RelationalModel.extend({
+ idAttribute: 'resource_uri',
+ relations: [
+ {
+ type: Backbone.HasMany,
+ key: 'middle',
+ relatedModel: MiddleModel,
+ includeInJSON: 'resource_uri',
+ collectionType: MiddleCollection
+ }
+ ]
+ });
+
+ var top = TopModel.build({
+ resource_uri: 'top/1',
+ middle: ['middle/1'],
+ label: 'top object'
+ });
+
+ equal( top.get('label'), 'top object', "Top object is initialized" );
+
+ var requests = top.fetchRelated('middle', {
+ response: {
+ status: 200,
+ responseText: {
+ resource_uri: 'middle/1',
+ label: 'middle object',
+ leaf: 'leaf/1'
+ }
+ }
+ });
+
+ equal( requests.length, 1, "A request has been made" );
+
+ var middle_coll = top.get('middle');
+ equal( middle_coll.length, 1, "Middle collection contains 1 item");
+
+ var middle = middle_coll.get('middle/1');
+ equal( middle.get('label'), "middle object", "Middle object was initialized" );
+
+ requests = middle.fetchRelated('leaf', {
+ response: {
+ status: 200,
+ responseText: {
+ resource_uri: 'leaf/1',
+ label: 'leaf object'
+ }
+ }
+ });
+
+ equal( requests.length, 1, "A request has been made" );
+
+ equal( middle.get('leaf').get('label'), "leaf object", "Leaf object was initialized" );
+ });
+
+ test( "One request for the relation", function() {
+ var LeafModel = Backbone.RelationalModel.extend({
+ idAttribute: 'resource_uri'
+ }),
+ MiddleModel = Backbone.RelationalModel.extend({
+ idAttribute: 'resource_uri',
+ relations: [
+ {
+ type: Backbone.HasOne,
+ key: 'leaf',
+ relatedModel: LeafModel,
+ includeInJSON: 'resource_uri'
+ }
+ ]
+ }),
+ MiddleCollection = Backbone.Collection.extend({
+ model: MiddleModel,
+ url: function(models) { return models && '/middle/set/'; }
+ }),
+ TopModel = Backbone.RelationalModel.extend({
+ idAttribute: 'resource_uri',
+ relations: [
+ {
+ type: Backbone.HasMany,
+ key: 'middle',
+ relatedModel: MiddleModel,
+ includeInJSON: 'resource_uri',
+ collectionType: MiddleCollection
+ }
+ ]
+ });
+
+ var top = TopModel.build({
+ resource_uri: 'top/1',
+ middle: ['middle/1'],
+ label: 'top object'
+ });
+
+ equal( top.get('label'), 'top object', "Top object was initialized" );
+
+ var requests = top.fetchRelated('middle', {
+ response: {
+ status: 200,
+ responseText: [{
+ resource_uri: 'middle/1',
+ label: 'middle object',
+ leaf: 'leaf/1'
+ }]
+ }
+ });
+
+ equal( requests.length, 1, "A request has been made" );
+
+ var middle_coll = top.get('middle');
+ equal( middle_coll.length, 1, "Middle collection contains 1 item");
+
+ var middle = middle_coll.get('middle/1');
+ equal( middle.get('label'), "middle object", "Middle object was initialized" );
+
+ requests = middle.fetchRelated('leaf', {
+ response: {
+ status: 200,
+ responseText: {
+ resource_uri: 'leaf/1',
+ label: 'leaf object'
+ }
+ }
+ });
+
+ // The following two assertions fail, as `leaf` was not fetched (requests is empty)
+ equal( requests.length, 1, "A request has been made" );
+
+ equal( middle.get('leaf').get('label'), "leaf object", "Leaf object was initialized" );
+ });
+
});
Something went wrong with that request. Please try again.