Sha256: f122c0b3e861628d9321e9fc95ab2dfadc0025092ed7cfbb986c69b1cdd36a9a

Contents?: true

Size: 1.95 KB

Versions: 1

Compression:

Stored size: 1.95 KB

Contents

#= require ./loading-mixin

class window.Brainstem.Collection extends Backbone.Collection
  constructor: ->
    super
    @setLoaded false

  update: (models) ->
    models = models.models if models.models?
    for model in models
      model = this.model.parse(model) if this.model.parse?
      backboneModel = @_prepareModel(model)
      if backboneModel
        if modelInCollection = @get(backboneModel.id)
          modelInCollection.set backboneModel.attributes
        else
          @add backboneModel
      else
        Brainstem.Utils.warn "Unable to update collection with invalid model", model

  loadNextPage: (options) ->
    oldLength = @length
    pageSize = 0
    paginationOptions = {}

    if @lastFetchOptions.perPage
      paginationOptions.page = @lastFetchOptions.page + 1
      pageSize = @lastFetchOptions.perPage
    else
      paginationOptions.offset = @lastFetchOptions.offset + @lastFetchOptions.limit
      pageSize = @lastFetchOptions.limit

    success = (collection) =>
      options.success(collection, collection.length == oldLength + pageSize) if options.success?
    base.data.loadCollection @lastFetchOptions.name, _.extend({}, @lastFetchOptions, options, paginationOptions, collection: this, success: success)

  reload: (options) ->
    base.data.reset()
    @reset [], silent: true
    @setLoaded false
    base.data.loadCollection @lastFetchOptions.name, _.extend({}, @lastFetchOptions, options, page: 1, collection: this)

  getWithAssocation: (id) ->
    @get(id)

  toServerJSON: (method) ->
    @toJSON()

  @getComparatorWithIdFailover: (order) ->
    [field, direction] = order.split(":")
    comp = @getComparator(field)
    (a, b) ->
      [b, a] = [a, b] if direction.toLowerCase() == "desc"
      result = comp(a, b)
      if result == 0
        a.get('id') - b.get('id')
      else
        result

  @getComparator: (field) ->
    return (a, b) -> a.get(field) - b.get(field)

_.extend(Brainstem.Collection.prototype, Brainstem.LoadingMixin);

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
brainstem-js-0.3.0 vendor/assets/javascripts/brainstem/brainstem-collection.coffee