From 90a4a1b635db04f91564537ad8a3e4afeaa6ea47 Mon Sep 17 00:00:00 2001 From: Scott Erickson Date: Thu, 17 Jul 2014 16:22:06 -0700 Subject: [PATCH] Added setProjection so CocoModels can more easily get subsets of data. --- app/models/CocoModel.coffee | 15 +++++++++++---- test/app/models/CocoModel.spec.coffee | 8 ++++++++ 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/app/models/CocoModel.coffee b/app/models/CocoModel.coffee index 045843306..5da19cab3 100644 --- a/app/models/CocoModel.coffee +++ b/app/models/CocoModel.coffee @@ -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 diff --git a/test/app/models/CocoModel.spec.coffee b/test/app/models/CocoModel.spec.coffee index 67e019c9a..7f2529466 100644 --- a/test/app/models/CocoModel.spec.coffee +++ b/test/app/models/CocoModel.spec.coffee @@ -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/', ->