Skip to content

Commit af1faf8

Browse files
committed
1 parent 7df67db commit af1faf8

File tree

10 files changed

+371
-332
lines changed

10 files changed

+371
-332
lines changed

CHANGELOG.md

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
##### 0.1.0 - xx September 2014
2+
3+
###### Backwards compatible API changes
4+
- jmdobry/angular-data#145 - Add "useClass" option to inject, find, findAll, create
5+
- jmdobry/angular-data#167 - Default params argument of bindAll to empty object
6+
7+
###### Backwards compatible bug fixes
8+
- jmdobry/angular-data#156 - cached findAll pending query doesn't get removed sometimes

dist/js-data.js

+20-10
Original file line numberDiff line numberDiff line change
@@ -3994,6 +3994,7 @@ function errorPrefix(resourceName) {
39943994
* @param {object} attrs The attributes with which to create the item of the type specified by `resourceName`.
39953995
* @param {object=} options Configuration options. Also passed along to the adapter's `create` method. Properties:
39963996
*
3997+
* - `{boolean=}` - `useClass` - Whether to wrap the injected item with the resource's instance constructor.
39973998
* - `{boolean=}` - `cacheResponse` - Inject the data returned by the adapter into the data store. Default: `true`.
39983999
* - `{boolean=}` - `upsert` - If `attrs` already contains a primary key, then attempt to call `DS.update` instead. Default: `true`.
39994000
* - `{function=}` - `beforeValidate` - Override the resource or global lifecycle hook.
@@ -4076,7 +4077,7 @@ function create(resourceName, attrs, options) {
40764077
resource.saved[id] = DSUtils.updateTimestamp(resource.saved[id]);
40774078
return DS.get(definition.name, id);
40784079
} else {
4079-
return data;
4080+
return DS.createInstance(resourceName, data, options);
40804081
}
40814082
});
40824083
}
@@ -4292,6 +4293,7 @@ function errorPrefix(resourceName, id) {
42924293
* @param {string|number} id The primary key of the item to retrieve.
42934294
* @param {object=} options Optional configuration. Also passed along to the adapter's `find` method. Properties:
42944295
*
4296+
* - `{boolean=}` - `useClass` - Whether to wrap the injected item with the resource's instance constructor.
42954297
* - `{boolean=}` - `bypassCache` - Bypass the cache. Default: `false`.
42964298
* - `{boolean=}` - `cacheResponse` - Inject the data returned by the adapter into the data store. Default: `true`.
42974299
*
@@ -4347,7 +4349,7 @@ function find(resourceName, id, options) {
43474349
resource.completedQueries[id] = new Date().getTime();
43484350
return DS.inject(resourceName, data, options);
43494351
} else {
4350-
return data;
4352+
return DS.createInstance(resourceName, data, options);
43514353
}
43524354
}).catch(function (err) {
43534355
delete resource.pendingQueries[id];
@@ -4447,6 +4449,7 @@ function processResults(data, resourceName, queryHash, options) {
44474449
*
44484450
* @param {object=} options Optional configuration. Also passed along to the adapter's `findAll` method. Properties:
44494451
*
4452+
* - `{boolean=}` - `useClass` - Whether to wrap the injected item with the resource's instance constructor.
44504453
* - `{boolean=}` - `bypassCache` - Bypass the cache. Default: `false`.
44514454
* - `{boolean=}` - `cacheResponse` - Inject the data returned by the adapter into the data store. Default: `true`.
44524455
*
@@ -4509,6 +4512,9 @@ function findAll(resourceName, params, options) {
45094512
throw err;
45104513
}
45114514
} else {
4515+
DS.utils.forEach(data, function (item, i) {
4516+
data[i] = DS.createInstance(resourceName, item, options);
4517+
});
45124518
return data;
45134519
}
45144520
}).catch(function (err) {
@@ -5432,7 +5438,7 @@ Defaults.prototype.defaultFilter = function (collection, resourceName, params, o
54325438
};
54335439
Defaults.prototype.baseUrl = '';
54345440
Defaults.prototype.endpoint = '';
5435-
Defaults.prototype.useClass = false;
5441+
Defaults.prototype.useClass = true;
54365442
/**
54375443
* @doc property
54385444
* @id DSProvider.properties:defaults.beforeValidate
@@ -6134,7 +6140,7 @@ function errorPrefix(resourceName) {
61346140
* @param {object=} attrs Optional attributes to mix in to the new instance.
61356141
* @param {object=} options Optional configuration. Properties:
61366142
*
6137-
* - `{boolean=}` - `useClass` - Whether to use the resource's wrapper class. Default: `true`.
6143+
* - `{boolean=}` - `useClass` - Whether to wrap the injected item with the resource's instance constructor.
61386144
*
61396145
* @returns {object} The new instance.
61406146
*/
@@ -6156,7 +6162,7 @@ function createInstance(resourceName, attrs, options) {
61566162
}
61576163

61586164
if (!('useClass' in options)) {
6159-
options.useClass = true;
6165+
options.useClass = definition.useClass;
61606166
}
61616167

61626168
var item;
@@ -7077,7 +7083,7 @@ function errorPrefix(resourceName) {
70777083
return 'DS.inject(' + resourceName + ', attrs[, options]): ';
70787084
}
70797085

7080-
function _inject(definition, resource, attrs) {
7086+
function _inject(definition, resource, attrs, options) {
70817087
var DS = this;
70827088

70837089
function _react(added, removed, changed, oldValueFn, firstTime) {
@@ -7108,7 +7114,7 @@ function _inject(definition, resource, attrs) {
71087114
});
71097115
compute = compute || !fn.deps.length;
71107116
if (compute) {
7111-
_compute.call(item, fn, field);
7117+
_compute.call(item, fn, field, DS.utils);
71127118
}
71137119
});
71147120
}
@@ -7133,7 +7139,7 @@ function _inject(definition, resource, attrs) {
71337139
if (DS.utils.isArray(attrs)) {
71347140
injected = [];
71357141
for (var i = 0; i < attrs.length; i++) {
7136-
injected.push(_inject.call(DS, definition, resource, attrs[i]));
7142+
injected.push(_inject.call(DS, definition, resource, attrs[i], options));
71377143
}
71387144
} else {
71397145
// check if "idAttribute" is a computed property
@@ -7157,7 +7163,7 @@ function _inject(definition, resource, attrs) {
71577163
var item = DS.get(definition.name, id);
71587164

71597165
if (!item) {
7160-
if (definition.methods || definition.useClass) {
7166+
if (options.useClass) {
71617167
if (attrs instanceof definition[definition.class]) {
71627168
item = attrs;
71637169
} else {
@@ -7279,6 +7285,7 @@ function _injectRelations(definition, injected, options) {
72797285
* @param {object|array} attrs The item or collection of items to inject into the data store.
72807286
* @param {object=} options The item or collection of items to inject into the data store. Properties:
72817287
*
7288+
* - `{boolean=}` - `useClass` - Whether to wrap the injected item with the resource's instance constructor.
72827289
* - `{boolean=}` - `findBelongsTo` - Find and attach any existing "belongsTo" relationships to the newly injected item. Potentially expensive if enabled. Default: `false`.
72837290
* - `{boolean=}` - `findHasMany` - Find and attach any existing "hasMany" relationships to the newly injected item. Potentially expensive if enabled. Default: `false`.
72847291
* - `{boolean=}` - `findHasOne` - Find and attach any existing "hasOne" relationships to the newly injected item. Potentially expensive if enabled. Default: `false`.
@@ -7308,7 +7315,10 @@ function inject(resourceName, attrs, options) {
73087315
stack++;
73097316

73107317
try {
7311-
injected = _inject.call(DS, definition, resource, attrs);
7318+
if (!('useClass' in options)) {
7319+
options.useClass = definition.useClass;
7320+
}
7321+
injected = _inject.call(DS, definition, resource, attrs, options);
73127322
if (definition.relations) {
73137323
_injectRelations.call(DS, definition, injected, options);
73147324
}

dist/js-data.min.js

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/datastore/async_methods/create.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ function errorPrefix(resourceName) {
3232
* @param {object} attrs The attributes with which to create the item of the type specified by `resourceName`.
3333
* @param {object=} options Configuration options. Also passed along to the adapter's `create` method. Properties:
3434
*
35+
* - `{boolean=}` - `useClass` - Whether to wrap the injected item with the resource's instance constructor.
3536
* - `{boolean=}` - `cacheResponse` - Inject the data returned by the adapter into the data store. Default: `true`.
3637
* - `{boolean=}` - `upsert` - If `attrs` already contains a primary key, then attempt to call `DS.update` instead. Default: `true`.
3738
* - `{function=}` - `beforeValidate` - Override the resource or global lifecycle hook.
@@ -114,7 +115,7 @@ function create(resourceName, attrs, options) {
114115
resource.saved[id] = DSUtils.updateTimestamp(resource.saved[id]);
115116
return DS.get(definition.name, id);
116117
} else {
117-
return data;
118+
return DS.createInstance(resourceName, data, options);
118119
}
119120
});
120121
}

src/datastore/async_methods/find.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ function errorPrefix(resourceName, id) {
3131
* @param {string|number} id The primary key of the item to retrieve.
3232
* @param {object=} options Optional configuration. Also passed along to the adapter's `find` method. Properties:
3333
*
34+
* - `{boolean=}` - `useClass` - Whether to wrap the injected item with the resource's instance constructor.
3435
* - `{boolean=}` - `bypassCache` - Bypass the cache. Default: `false`.
3536
* - `{boolean=}` - `cacheResponse` - Inject the data returned by the adapter into the data store. Default: `true`.
3637
*
@@ -86,7 +87,7 @@ function find(resourceName, id, options) {
8687
resource.completedQueries[id] = new Date().getTime();
8788
return DS.inject(resourceName, data, options);
8889
} else {
89-
return data;
90+
return DS.createInstance(resourceName, data, options);
9091
}
9192
}).catch(function (err) {
9293
delete resource.pendingQueries[id];

src/datastore/async_methods/findAll.js

+4
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ function processResults(data, resourceName, queryHash, options) {
8181
*
8282
* @param {object=} options Optional configuration. Also passed along to the adapter's `findAll` method. Properties:
8383
*
84+
* - `{boolean=}` - `useClass` - Whether to wrap the injected item with the resource's instance constructor.
8485
* - `{boolean=}` - `bypassCache` - Bypass the cache. Default: `false`.
8586
* - `{boolean=}` - `cacheResponse` - Inject the data returned by the adapter into the data store. Default: `true`.
8687
*
@@ -143,6 +144,9 @@ function findAll(resourceName, params, options) {
143144
throw err;
144145
}
145146
} else {
147+
DS.utils.forEach(data, function (item, i) {
148+
data[i] = DS.createInstance(resourceName, item, options);
149+
});
146150
return data;
147151
}
148152
}).catch(function (err) {

src/datastore/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ Defaults.prototype.defaultFilter = function (collection, resourceName, params, o
194194
};
195195
Defaults.prototype.baseUrl = '';
196196
Defaults.prototype.endpoint = '';
197-
Defaults.prototype.useClass = false;
197+
Defaults.prototype.useClass = true;
198198
/**
199199
* @doc property
200200
* @id DSProvider.properties:defaults.beforeValidate

src/datastore/sync_methods/createInstance.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ function errorPrefix(resourceName) {
5757
* @param {object=} attrs Optional attributes to mix in to the new instance.
5858
* @param {object=} options Optional configuration. Properties:
5959
*
60-
* - `{boolean=}` - `useClass` - Whether to use the resource's wrapper class. Default: `true`.
60+
* - `{boolean=}` - `useClass` - Whether to wrap the injected item with the resource's instance constructor.
6161
*
6262
* @returns {object} The new instance.
6363
*/
@@ -79,7 +79,7 @@ function createInstance(resourceName, attrs, options) {
7979
}
8080

8181
if (!('useClass' in options)) {
82-
options.useClass = true;
82+
options.useClass = definition.useClass;
8383
}
8484

8585
var item;

src/datastore/sync_methods/inject.js

+9-5
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ function errorPrefix(resourceName) {
99
return 'DS.inject(' + resourceName + ', attrs[, options]): ';
1010
}
1111

12-
function _inject(definition, resource, attrs) {
12+
function _inject(definition, resource, attrs, options) {
1313
var DS = this;
1414

1515
function _react(added, removed, changed, oldValueFn, firstTime) {
@@ -40,7 +40,7 @@ function _inject(definition, resource, attrs) {
4040
});
4141
compute = compute || !fn.deps.length;
4242
if (compute) {
43-
_compute.call(item, fn, field);
43+
_compute.call(item, fn, field, DS.utils);
4444
}
4545
});
4646
}
@@ -65,7 +65,7 @@ function _inject(definition, resource, attrs) {
6565
if (DS.utils.isArray(attrs)) {
6666
injected = [];
6767
for (var i = 0; i < attrs.length; i++) {
68-
injected.push(_inject.call(DS, definition, resource, attrs[i]));
68+
injected.push(_inject.call(DS, definition, resource, attrs[i], options));
6969
}
7070
} else {
7171
// check if "idAttribute" is a computed property
@@ -89,7 +89,7 @@ function _inject(definition, resource, attrs) {
8989
var item = DS.get(definition.name, id);
9090

9191
if (!item) {
92-
if (definition.methods || definition.useClass) {
92+
if (options.useClass) {
9393
if (attrs instanceof definition[definition.class]) {
9494
item = attrs;
9595
} else {
@@ -211,6 +211,7 @@ function _injectRelations(definition, injected, options) {
211211
* @param {object|array} attrs The item or collection of items to inject into the data store.
212212
* @param {object=} options The item or collection of items to inject into the data store. Properties:
213213
*
214+
* - `{boolean=}` - `useClass` - Whether to wrap the injected item with the resource's instance constructor.
214215
* - `{boolean=}` - `findBelongsTo` - Find and attach any existing "belongsTo" relationships to the newly injected item. Potentially expensive if enabled. Default: `false`.
215216
* - `{boolean=}` - `findHasMany` - Find and attach any existing "hasMany" relationships to the newly injected item. Potentially expensive if enabled. Default: `false`.
216217
* - `{boolean=}` - `findHasOne` - Find and attach any existing "hasOne" relationships to the newly injected item. Potentially expensive if enabled. Default: `false`.
@@ -240,7 +241,10 @@ function inject(resourceName, attrs, options) {
240241
stack++;
241242

242243
try {
243-
injected = _inject.call(DS, definition, resource, attrs);
244+
if (!('useClass' in options)) {
245+
options.useClass = definition.useClass;
246+
}
247+
injected = _inject.call(DS, definition, resource, attrs, options);
244248
if (definition.relations) {
245249
_injectRelations.call(DS, definition, injected, options);
246250
}

0 commit comments

Comments
 (0)