Skip to content

Commit cb45988

Browse files
committed
Merge pull request #116 from teamsnap/smart-load
Smart load
2 parents 87506d2 + 9d2992e commit cb45988

File tree

8 files changed

+85
-21
lines changed

8 files changed

+85
-21
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# TeamSnap JavaScript SDK CHANGELOG
22

3+
### Feb 15, 2016 // Version 1.9.0
4+
- Refactors `bulkLoad` to accept "Smart Load" params.
5+
6+
---
7+
38
### Feb 4, 2016 // Version 1.8.1
49
- Update `canEditItem()` helper to return false when a manager attempts to edit an owner's member item.
510

docs/collections/teams.md

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,21 +146,44 @@ teamsnap.saveTeam(team).then(function(){
146146

147147

148148
<a id="bulkLoad"></a>
149-
## `bulkLoad(teamId, types, callback)`
149+
## `bulkLoad(teamId, types, callback)` / `bulkLoad(params, callback)`
150150
Query that returns multiple types based on provided parameters.
151151

152+
In addition to bulk loading specific types for a given team, you can scope and filter the query to
153+
return more specific datasets. We sometimes refer to this as "Smart Load", but it's still APIv3's
154+
`bulk_load` query.
155+
152156
_Note: Some items, such as `availability` are not currently available for `bulkLoad` for performance reasons. Likewise, loading many types at once without narrowing the scope may provide a less than optimal response, or possibly incur an API timeout with larger datasets._
153157

154158
### Params
159+
_Note: There are two ways you can call `bulkLoad`. This is the simple / classic way._
155160
* `teamId`: [int] - a `teamId` to load items for.
156161
* `types`: [array] - array of types to load.
157162
* `callback`: [function] - callback to be executed when the operation completes.
158163

164+
_Using "Smart Load" params_
165+
* `params`: [object] - object should contain the following params:
166+
- `teamId`: [int] - a `teamId` to load items for.
167+
- `types`: [array] - array of types to load.
168+
- `scopeTo`: [string] - item in the query to scope other items to.
169+
- `[itemType__queryParam]`: [string] - additional filters can be set with the item's type and one of its available search params separated by a double underscore (`__`). This accepts a string for the value - which could be a date, an ID, or event a list of IDs. See the specific event's available search params with `teamsnap.collections.[itemCollection].queries.search.params` where `itemCollection` is the collection to look up.
170+
* `callback`: [function] - callback to be executed when the operation completes.
171+
159172
### Examples
160173
```javascript
161174
// ~~~~~
162175
// Bulk loads several types at once.
163-
teamsnap.bulkLoad(1, ['member', 'event', 'availability']);
176+
teamsnap.bulkLoad(1, ['member', 'event', 'assignment']);
177+
178+
// ~~~~~
179+
// Bulk load events and assignments related to eventId 1-6.
180+
bulkLoadParams = {
181+
teamId: 1,
182+
types: ['event', 'assignment'],
183+
scopeTo: 'event',
184+
event__id: '1,2,3,4,5,6'
185+
}
186+
teamsnap.bulkLoad(bulkLoadParams);
164187
```
165188

166189

lib/teamsnap.js

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2768,13 +2768,22 @@ exports.deleteTeam = function(team, callback) {
27682768
};
27692769

27702770
exports.bulkLoad = function(teamId, types, callback) {
2771-
var params;
2772-
if (!this.isId(teamId)) {
2773-
throw new TSArgsError('teamsnap.bulkLoad', 'teamId must be provided');
2774-
}
2775-
if (typeof types === 'function') {
2776-
callback = types;
2777-
types = null;
2771+
var loadParams, params;
2772+
if (typeof teamId === 'object') {
2773+
loadParams = teamId;
2774+
if (!this.isId(loadParams.teamId)) {
2775+
throw new TSArgsError('teamsnap.bulkLoad', 'teamId must be provided');
2776+
}
2777+
teamId = loadParams.teamId;
2778+
types = loadParams.types;
2779+
} else {
2780+
if (!this.isId(teamId)) {
2781+
throw new TSArgsError('teamsnap.bulkLoad', 'teamId must be provided');
2782+
}
2783+
if (typeof types === 'function') {
2784+
callback = types;
2785+
types = null;
2786+
}
27782787
}
27792788
if (!Array.isArray(types)) {
27802789
types = this.getTeamTypes();
@@ -2784,6 +2793,9 @@ exports.bulkLoad = function(teamId, types, callback) {
27842793
teamId: teamId,
27852794
types: types.map(this.underscoreType).join(',')
27862795
};
2796+
if ((loadParams != null ? loadParams.scopeTo : void 0) != null) {
2797+
params.scopeTo = this.underscoreType(loadParams.scopeTo);
2798+
}
27872799
return this.collections.root.queryItems('bulkLoad', params, callback);
27882800
};
27892801

@@ -3854,7 +3866,7 @@ MetaList = (function() {
38543866
};
38553867

38563868
MetaList.prototype._request = function(request, method, rel, params, type) {
3857-
var data, entry, key, value;
3869+
var data, entry, filteredOn, itemCollection, key, value;
38583870
if (!(entry = this[rel])) {
38593871
throw new TSError("Unable to find rel '" + rel + "'");
38603872
}
@@ -3874,6 +3886,12 @@ MetaList = (function() {
38743886
}
38753887
if (entry.params.hasOwnProperty(key)) {
38763888
data[underscore(key)] = value;
3889+
} else if (key.indexOf('__') !== -1) {
3890+
filteredOn = key.split('__');
3891+
itemCollection = teamsnap.getCollectionForItem(filteredOn[0]);
3892+
if (itemCollection.queries.search.params.hasOwnProperty(filteredOn[1])) {
3893+
data[underscore(key)] = value;
3894+
}
38773895
}
38783896
}
38793897
}
@@ -5769,7 +5787,7 @@ ref = require('./model'), Collection = ref.Collection, Item = ref.Item;
57695787
require('./errors');
57705788

57715789
TeamSnap = (function() {
5772-
TeamSnap.prototype.version = '1.8.1';
5790+
TeamSnap.prototype.version = '1.9.0';
57735791

57745792
TeamSnap.prototype.promises = promises;
57755793

lib/teamsnap.min.js

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "teamsnap.js",
3-
"version": "1.8.1",
3+
"version": "1.9.0",
44
"description": "A JavaScript library for using the TeamSnap API.",
55
"author": "Jacob Wright with TeamSnap (http://www.teamsnap.com)",
66
"homepage": "https://github.com/teamsnap/teamsnap-javascript-sdk",

src/collections/teams.coffee

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,18 +46,29 @@ exports.deleteTeam = (team, callback) ->
4646

4747
# Loads all items associated with a team, optionally limited by the types array
4848
exports.bulkLoad = (teamId, types, callback) ->
49-
unless @isId teamId
50-
throw new TSArgsError 'teamsnap.bulkLoad', 'teamId must be provided'
49+
if typeof teamId is 'object'
50+
loadParams = teamId
51+
# Using params object (smartload)
52+
unless @isId loadParams.teamId
53+
throw new TSArgsError 'teamsnap.bulkLoad', 'teamId must be provided'
54+
teamId = loadParams.teamId
55+
types = loadParams.types
56+
else
57+
# Using classic bulk_load
58+
unless @isId teamId
59+
throw new TSArgsError 'teamsnap.bulkLoad', 'teamId must be provided'
5160

52-
if typeof types is 'function'
53-
callback = types
54-
types = null
61+
if typeof types is 'function'
62+
callback = types
63+
types = null
5564

5665
unless Array.isArray types
5766
types = @getTeamTypes()
5867
types.splice types.indexOf('availability'), 1
5968

6069
params = teamId: teamId, types: types.map(@underscoreType).join(',')
70+
if loadParams?.scopeTo?
71+
params.scopeTo = @underscoreType(loadParams.scopeTo)
6172
@collections.root.queryItems 'bulkLoad', params, callback
6273

6374

src/model.coffee

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,13 @@ class MetaList
302302

303303
if entry.params.hasOwnProperty(key)
304304
data[underscore key] = value
305+
# Check for search filters (example: `event__id`)
306+
else if key.indexOf('__') isnt -1
307+
filteredOn = key.split '__'
308+
itemCollection = teamsnap.getCollectionForItem(filteredOn[0])
309+
# Ensure valid param on filtered item type's search query
310+
if itemCollection.queries.search.params.hasOwnProperty filteredOn[1]
311+
data[underscore(key)] = value
305312

306313
request(method, entry.href, data).then (xhr) ->
307314
items = if xhr.data?.collection?.items

src/teamsnap.coffee

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ promises = require './promises'
33
require './errors'
44

55
class TeamSnap
6-
version: '1.8.1'
6+
version: '1.9.0'
77
promises: promises
88
when: promises.when
99
TeamSnap: TeamSnap

0 commit comments

Comments
 (0)