Sha256: da2a73bc6d409b234248ff22d42e339689275d1c1c91e1a6ffedf52929ac6d42

Contents?: true

Size: 888 Bytes

Versions: 2

Compression:

Stored size: 888 Bytes

Contents

class Backbone.ExtendedCollection extends Backbone.Collection
  
  # Backwards compatable interface update
  # Allows the Backbone.Collection#constructor to accept a single hash of attributes.
  # The 'models' attribute is used as the collection's enumerable
  #
  #   new Sample({
  #     models:[ model1, model2],
  #     title: 'great for factories'
  #   })
  #
  constructor: (models=[], other_options={})->
    unless models instanceof Array
      _(other_options).extend( _(models).tap (obj)-> 
        models = models.models
        delete obj.options 
      )
    
    # store attributes
    @attributes = _.clone @defaults || {}
    _.chain(@attributes).keys().each (attr)=>
      @attributes[attr]= other_options[attr] if other_options[attr]?
    
    # continue building obj
    super( models, other_options )
    
  
  toJSON: => { collection: @models }
  
  first: => @at(0)

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
rails-backbone-generator-0.0.3 lib/generators/backbone/setup_generator/templates/app/assets/javascripts/shared/core_extentions/collections_extentions.coffee
rails-backbone-generator-0.0.1 lib/generators/backbone/templates/app/assets/javascripts/shared/core_extentions/collections_extentions.coffee