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

View file

@ -14,6 +14,14 @@ class BlandClass extends CocoModel
urlRoot: '/db/bland' urlRoot: '/db/bland'
describe 'CocoModel', -> 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', -> describe 'save', ->
it 'saves to db/<urlRoot>', -> it 'saves to db/<urlRoot>', ->