                 success.apply(@,arguments) if success
         })
         Lanes.Models.Sync.perform('delete', this, options)
-
 }
 
 class ModelsCollection

        @errors=[]
        Lanes.Vendor.Ampersand.Collection.apply(this, arguments)

    @fetch: (options)->
        collection = new this
        collection.fetch(options)
        collection
        
    fetch: (options={})->
        @_isLoaded = true
        Lanes.Models.Sync.wrapRequest(this,options)
        return this.sync('read', this, options)

    # Sets the attribute data from a server respose
    setFromServer: (data, options)->
        method = if options.reset then 'reset' else 'set'
        this[method](data, options)
        this.trigger('sync', this, data, options)

    isLoaded:->
        @_isLoaded

    ensureLoaded: ( callback )->
        if ! @_isLoaded && ! this.length
            this.fetch({ success: callback })
        else if callback
            callback()

    isDirty: ->
        for model in @models
            return true if model.isDirty()
        false

    parse:(resp)->
        resp['data']

    viewJSON: (options)->
        this.map( (model) ->
            model.viewJSON(options)
        )

    url: ->
        @model::urlRoot()

    sync: Lanes.Models.Sync.perform

    save: (options)->
        Lanes.Models.Sync.perform('update', this, options)

    dataForSave: (options)->
        unsaved = []
        for model in @models
            unsaved.push( model.unsavedModels() ) if model.isDirty()
        unsaved

    _prepareModel: (attrs, options={})->

            break if found != -1
        found

    # convenience method to create a new subCollection
    subcollection: (options)->
        new Lanes.Models.SubCollection(this, options)

    destroyAll: (options={})->
        success = options.success
