2014-07-07 13:09:12 -04:00
|
|
|
CocoModel = require 'models/CocoModel'
|
|
|
|
|
2014-01-03 13:32:13 -05:00
|
|
|
module.exports = class CocoCollection extends Backbone.Collection
|
|
|
|
loaded: false
|
2014-07-07 13:09:12 -04:00
|
|
|
model: null
|
2014-01-03 13:32:13 -05:00
|
|
|
|
2014-08-28 14:24:45 -04:00
|
|
|
initialize: (models, options) ->
|
|
|
|
options ?= {}
|
|
|
|
@model ?= options.model
|
2014-07-07 13:09:12 -04:00
|
|
|
if not @model
|
|
|
|
console.error @constructor.name, 'does not have a model defined. This will not do!'
|
2014-08-28 14:24:45 -04:00
|
|
|
super(models, options)
|
|
|
|
@setProjection options.project
|
|
|
|
if options.url then @url = options.url
|
2014-01-03 13:32:13 -05:00
|
|
|
@once 'sync', =>
|
|
|
|
@loaded = true
|
2014-04-26 15:54:03 -04:00
|
|
|
model.loaded = true for model in @models
|
2016-04-14 18:26:29 -04:00
|
|
|
if window.application?.testing
|
|
|
|
@fakeRequests = []
|
|
|
|
@on 'request', -> @fakeRequests.push jasmine.Ajax.requests.mostRecent()
|
2014-04-26 15:54:03 -04:00
|
|
|
|
|
|
|
getURL: ->
|
2014-05-02 13:31:20 -04:00
|
|
|
return if _.isString @url then @url else @url()
|
|
|
|
|
2014-08-28 14:24:45 -04:00
|
|
|
fetch: (options) ->
|
|
|
|
options ?= {}
|
|
|
|
if @project
|
|
|
|
options.data ?= {}
|
|
|
|
options.data.project = @project.join(',')
|
|
|
|
@jqxhr = super(options)
|
2014-05-02 13:31:20 -04:00
|
|
|
@loading = true
|
|
|
|
@jqxhr
|
2014-08-28 14:24:45 -04:00
|
|
|
|
2016-05-03 13:38:20 -04:00
|
|
|
setProjection: (@project) ->
|
|
|
|
|
2016-08-23 13:43:31 -04:00
|
|
|
stringify: -> return JSON.stringify(@toJSON())
|
|
|
|
|
|
|
|
wait: (event) -> new Promise((resolve) => @once(event, resolve))
|