Added setProjection so CocoModels can more easily get subsets of data.

This commit is contained in:
Scott Erickson 2014-07-17 16:22:06 -07:00
parent 18631ec5b1
commit 90a4a1b635
2 changed files with 19 additions and 4 deletions

View file

@ -13,8 +13,10 @@ class CocoModel extends Backbone.Model
getMe: -> @me or @me = require('lib/auth').me
initialize: ->
super()
initialize: (attributes, options) ->
super(arguments...)
options ?= {}
@setProjection options.project
if not @constructor.className
console.error("#{@} needs a className set.")
@addSchemaDefaults()
@ -22,6 +24,8 @@ class CocoModel extends Backbone.Model
@on 'error', @onError, @
@on 'add', @onLoaded, @
@saveBackup = _.debounce(@saveBackup, 500)
setProjection: (@project) ->
type: ->
@constructor.className
@ -116,8 +120,11 @@ class CocoModel extends Backbone.Model
console.debug 'Patching', @get('name') or @, keys
@save(attrs, options)
fetch: ->
@jqxhr = super(arguments...)
fetch: (options) ->
options ?= {}
options.data ?= {}
options.data.project = @project.join(',') if @project
@jqxhr = super(options)
@loading = true
@jqxhr

View file

@ -14,6 +14,14 @@ class BlandClass extends CocoModel
urlRoot: '/db/bland'
describe 'CocoModel', ->
describe 'setProjection', ->
it 'takes an array of properties to project and adds them as a query parameter', ->
b = new BlandClass({})
b.setProjection ['number', 'object']
b.fetch()
request = jasmine.Ajax.requests.mostRecent()
expect(decodeURIComponent(request.url).indexOf('project=number,object')).toBeGreaterThan(-1)
describe 'save', ->
it 'saves to db/<urlRoot>', ->