Skip to content

Commit

Permalink
Changed to collectionOptions and accepts an options hash or a function
Browse files Browse the repository at this point in the history
  • Loading branch information
ulmus committed Mar 23, 2012
1 parent 1a06536 commit c3ddc6a
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
7 changes: 4 additions & 3 deletions README.md
Expand Up @@ -171,11 +171,12 @@ By default, the relation's `key` attribute will be used to create a reference to
If you set `collectionKey` to a string, it will use that string as the reference to the RelationalModel, rather than the relation's `key` attribute. If you set `collectionKey` to a string, it will use that string as the reference to the RelationalModel, rather than the relation's `key` attribute.
If you don't want this behavior at all, set `collectionKey` to false (or any falsy value) and this reference will not be created. If you don't want this behavior at all, set `collectionKey` to false (or any falsy value) and this reference will not be created.


### collectionOptionCallback ### collectionOptions


Value: a function that accepts an instance of a `Backbone.RelationalModed` and returns an option hash Value: an options hash or a function that accepts an instance of a `Backbone.RelationalModed` and returns an option hash


If supplied, this function should return an option hash that will be used when initializing the collection at the other end of a ToMany-relation. The function will be called with the initialized instance from the first end of the relation as the only argument Used to provide options for the initialization of the collection in the "Many"-end of a `HasMany` relation. Can be an options hash or
a function that should take the instance in the "One"-end of the "HasMany" relation and return an options hash


### includeInJSON ### includeInJSON


Expand Down
9 changes: 6 additions & 3 deletions backbone-relational.js
Expand Up @@ -653,7 +653,7 @@
reverseRelation: { type: 'HasOne' }, reverseRelation: { type: 'HasOne' },
collectionType: Backbone.Collection, collectionType: Backbone.Collection,
collectionKey: true, collectionKey: true,
collectionOptionsCallback: function( instance ){ return {} } collectionOptions: {}
}, },


initialize: function() { initialize: function() {
Expand All @@ -668,8 +668,11 @@
if ( !this.collectionType.prototype instanceof Backbone.Collection.prototype.constructor ){ if ( !this.collectionType.prototype instanceof Backbone.Collection.prototype.constructor ){
throw new Error( 'collectionType must inherit from Backbone.Collection' ); throw new Error( 'collectionType must inherit from Backbone.Collection' );
} }


this.setRelated( this.prepareCollection( new this.collectionType( [], this.options.collectionOptionsCallback( this.instance ) ) ) ); var collectionOptions = _.isFunction( self.options.collectionOptions ) ?
self.options.collectionOptions( this.instance ) :
self.options.collectionOptions;
this.setRelated( this.prepareCollection( new this.collectionType( [], collectionOptions ) ) );
this.findRelated( { silent: true } ); this.findRelated( { silent: true } );
}, },


Expand Down

0 comments on commit c3ddc6a

Please sign in to comment.