Skip to content

Commit fb65a0e

Browse files
committed
Cleaned and corrected api documentation for sync DS methods. #124.
1 parent 5a4cc97 commit fb65a0e

14 files changed

+167
-107
lines changed

dist/angular-data.js

+83-53
Original file line numberDiff line numberDiff line change
@@ -4127,9 +4127,9 @@ function errorPrefix(resourceName) {
41274127
* ```js
41284128
* // bind the documents with ownerId of 5 to the 'docs' property of the $scope
41294129
* var deregisterFunc = DS.bindAll($scope, 'docs', 'document', {
4130-
* where: {
4131-
* ownerId: 5
4132-
* }
4130+
* where: {
4131+
* ownerId: 5
4132+
* }
41334133
* });
41344134
* ```
41354135
*
@@ -4271,7 +4271,7 @@ function errorPrefix(resourceName) {
42714271
* @description
42724272
* Synchronously return the changes object of the item of the type specified by `resourceName` that has the primary key
42734273
* specified by `id`. This object represents the diff between the item in its current state and the state of the item
4274-
* the last time it was saved via an async adapter.
4274+
* the last time it was saved via an adapter.
42754275
*
42764276
* ## Signature:
42774277
* ```js
@@ -4285,6 +4285,8 @@ function errorPrefix(resourceName) {
42854285
*
42864286
* d.author = 'Sally';
42874287
*
4288+
* // You might have to do $scope.$apply() first
4289+
*
42884290
* DS.changes('document', 5); // {...} Object describing changes
42894291
* ```
42904292
*
@@ -4344,12 +4346,35 @@ function errorPrefix(resourceName) {
43444346
* ## Example:
43454347
*
43464348
* ```js
4347-
* // Thanks to createInstance, you don't have to do this anymore
4348-
* var User = DS.definitions.user[DS.definitions.user.class];
4349+
* var User = DS.defineResource({
4350+
* name: 'user',
4351+
* methods: {
4352+
* say: function () {
4353+
* return 'hi';
4354+
* }
4355+
* }
4356+
* });
4357+
*
4358+
* var user = User.createInstance();
4359+
* var user2 = DS.createInstance('user');
4360+
*
4361+
* user instanceof User[User.class]; // true
4362+
* user2 instanceof User[User.class]; // true
4363+
*
4364+
* user.say(); // hi
4365+
* user2.say(); // hi
43494366
*
4350-
* var user = DS.createInstance('user');
4367+
* var user3 = User.createInstance({ name: 'John' }, { useClass: false });
4368+
* var user4 = DS.createInstance('user', { name: 'John' }, { useClass: false });
43514369
*
4352-
* user instanceof User; // true
4370+
* user3; // { name: 'John' }
4371+
* user3 instanceof User[User.class]; // false
4372+
*
4373+
* user4; // { name: 'John' }
4374+
* user4 instanceof User[User.class]; // false
4375+
*
4376+
* user3.say(); // TypeError: undefined is not a function
4377+
* user4.say(); // TypeError: undefined is not a function
43534378
* ```
43544379
*
43554380
* ## Throws
@@ -4363,16 +4388,17 @@ function errorPrefix(resourceName) {
43634388
*
43644389
* - `{boolean=}` - `useClass` - Whether to use the resource's wrapper class. Default: `true`.
43654390
*
4366-
* @returns {object} The new instance
4391+
* @returns {object} The new instance.
43674392
*/
43684393
function createInstance(resourceName, attrs, options) {
43694394
var DS = this;
43704395
var IA = DS.errors.IA;
4396+
var definition = DS.definitions[resourceName];
43714397

43724398
attrs = attrs || {};
43734399
options = options || {};
43744400

4375-
if (!DS.definitions[resourceName]) {
4401+
if (!definition) {
43764402
throw new DS.errors.NER(errorPrefix(resourceName) + resourceName);
43774403
} else if (attrs && !DS.utils.isObject(attrs)) {
43784404
throw new IA(errorPrefix(resourceName) + 'attrs: Must be an object!');
@@ -4387,7 +4413,7 @@ function createInstance(resourceName, attrs, options) {
43874413
var item;
43884414

43894415
if (options.useClass) {
4390-
var Func = DS.definitions[resourceName][DS.definitions[resourceName].class];
4416+
var Func = definition[definition.class];
43914417
item = new Func();
43924418
} else {
43934419
item = {};
@@ -4688,7 +4714,8 @@ var observe = require('../../../lib/observe-js/observe-js');
46884714
* @name digest
46894715
* @description
46904716
* Trigger a digest loop that checks for changes and updates the `lastModified` timestamp if an object has changed.
4691-
* Anything $watching `DS.lastModified(...)` will detect the updated timestamp and execute the callback function.
4717+
* Anything $watching `DS.lastModified(...)` will detect the updated timestamp and execute the callback function. If
4718+
* your browser supports `Object.observe` then this function has no effect.
46924719
*
46934720
* ## Signature:
46944721
* ```js
@@ -4753,7 +4780,7 @@ function _eject(definition, resource, id) {
47534780
* @name eject
47544781
* @description
47554782
* Eject the item of the specified type that has the given primary key from the data store. Ejection only removes items
4756-
* from the data store and does not attempt to delete items on the server.
4783+
* from the data store and does not attempt to destroy items via an adapter.
47574784
*
47584785
* ## Signature:
47594786
* ```js
@@ -4785,12 +4812,12 @@ function _eject(definition, resource, id) {
47854812
*/
47864813
function eject(resourceName, id) {
47874814
var DS = this;
4788-
if (!DS.definitions[resourceName]) {
4815+
var definition = DS.definitions[resourceName];
4816+
if (!definition) {
47894817
throw new DS.errors.NER(errorPrefix(resourceName, id) + resourceName);
47904818
} else if (!DS.utils.isString(id) && !DS.utils.isNumber(id)) {
47914819
throw new DS.errors.IA(errorPrefix(resourceName, id) + 'id: Must be a string or a number!');
47924820
}
4793-
var definition = DS.definitions[resourceName];
47944821
var resource = DS.store[resourceName];
47954822
var ejected;
47964823

@@ -4835,9 +4862,8 @@ function _ejectAll(definition, resource, params) {
48354862
* @id DS.sync_methods:ejectAll
48364863
* @name ejectAll
48374864
* @description
4838-
* Eject all matching items of the specified type from the data store. If query is specified then all items of the
4839-
* specified type will be removed. Ejection only removes items from the data store and does not attempt to delete items
4840-
* on the server.
4865+
* Eject all matching items of the specified type from the data store. Ejection only removes items from the data store
4866+
* and does not attempt to destroy items via an adapter.
48414867
*
48424868
* ## Signature:
48434869
* ```js
@@ -4882,7 +4908,7 @@ function _ejectAll(definition, resource, params) {
48824908
* - `{NonexistentResourceError}`
48834909
*
48844910
* @param {string} resourceName The resource type, e.g. 'user', 'comment', etc.
4885-
* @param {object} params Parameter object that is serialized into the query string. Properties:
4911+
* @param {object} params Parameter object that is used to filter items. Properties:
48864912
*
48874913
* - `{object=}` - `where` - Where clause.
48884914
* - `{number=}` - `limit` - Limit clause.
@@ -4894,15 +4920,14 @@ function _ejectAll(definition, resource, params) {
48944920
*/
48954921
function ejectAll(resourceName, params) {
48964922
var DS = this;
4923+
var definition = DS.definitions[resourceName];
48974924
params = params || {};
48984925

4899-
if (!DS.definitions[resourceName]) {
4926+
if (!definition) {
49004927
throw new DS.errors.NER(errorPrefix(resourceName) + resourceName);
49014928
} else if (!DS.utils.isObject(params)) {
49024929
throw new DS.errors.IA(errorPrefix(resourceName) + 'params: Must be an object!');
49034930
}
4904-
4905-
var definition = DS.definitions[resourceName];
49064931
var resource = DS.store[resourceName];
49074932
var ejected;
49084933

@@ -4942,17 +4967,15 @@ function errorPrefix(resourceName) {
49424967
*
49434968
* ## Example:
49444969
*
4945-
* ```js
4946-
* TODO: filter(resourceName, params[, options]) example
4947-
* ```
4970+
* For many examples see the [tests for DS.filter](https://github.com/jmdobry/angular-data/blob/master/test/integration/datastore/sync_methods/filter.test.js).
49484971
*
49494972
* ## Throws
49504973
*
49514974
* - `{IllegalArgumentError}`
49524975
* - `{NonexistentResourceError}`
49534976
*
49544977
* @param {string} resourceName The resource type, e.g. 'user', 'comment', etc.
4955-
* @param {object=} params Parameter object that is serialized into the query string. Properties:
4978+
* @param {object=} params Parameter object that is used to filter items. Properties:
49564979
*
49574980
* - `{object=}` - `where` - Where clause.
49584981
* - `{number=}` - `limit` - Limit clause.
@@ -4970,18 +4993,17 @@ function errorPrefix(resourceName) {
49704993
function filter(resourceName, params, options) {
49714994
var DS = this;
49724995
var IA = DS.errors.IA;
4996+
var definition = DS.definitions[resourceName];
49734997

49744998
options = options || {};
49754999

4976-
if (!DS.definitions[resourceName]) {
5000+
if (!definition) {
49775001
throw new DS.errors.NER(errorPrefix(resourceName) + resourceName);
49785002
} else if (params && !DS.utils.isObject(params)) {
49795003
throw new IA(errorPrefix(resourceName) + 'params: Must be an object!');
49805004
} else if (!DS.utils.isObject(options)) {
49815005
throw new IA(errorPrefix(resourceName) + 'options: Must be an object!');
49825006
}
4983-
4984-
var definition = DS.definitions[resourceName];
49855007
var resource = DS.store[resourceName];
49865008

49875009
// Protect against null
@@ -5019,8 +5041,8 @@ function errorPrefix(resourceName, id) {
50195041
* @id DS.sync_methods:get
50205042
* @name get
50215043
* @description
5022-
* Synchronously return the resource with the given id. The data store will forward the request to the server if the
5023-
* item is not in the cache and `loadFromServer` is set to `true` in the options hash.
5044+
* Synchronously return the resource with the given id. The data store will forward the request to an adapter if
5045+
* `loadFromServer` is `true` in the options hash.
50245046
*
50255047
* ## Signature:
50265048
* ```js
@@ -5040,7 +5062,7 @@ function errorPrefix(resourceName, id) {
50405062
*
50415063
* @param {string} resourceName The resource type, e.g. 'user', 'comment', etc.
50425064
* @param {string|number} id The primary key of the item to retrieve.
5043-
* @param {object=} options Optional configuration. Properties:
5065+
* @param {object=} options Optional configuration. Also passed along to `DS.find` if `loadFromServer` is `true`. Properties:
50445066
*
50455067
* - `{boolean=}` - `loadFromServer` - Send the query to server if it has not been sent yet. Default: `false`.
50465068
*
@@ -5062,7 +5084,7 @@ function get(resourceName, id, options) {
50625084
// cache miss, request resource from server
50635085
var item = DS.store[resourceName].index.get(id);
50645086
if (!item && options.loadFromServer) {
5065-
DS.find(resourceName, id).then(null, function (err) {
5087+
DS.find(resourceName, id, options).then(null, function (err) {
50665088
return DS.$q.reject(err);
50675089
});
50685090
}
@@ -5104,6 +5126,8 @@ function diffIsEmpty(utils, diff) {
51045126
*
51055127
* d.author = 'Sally';
51065128
*
5129+
* // You may have to do $scope.$apply() first
5130+
*
51075131
* DS.hasChanges('document', 5); // true
51085132
* ```
51095133
*
@@ -5525,11 +5549,11 @@ function errorPrefix(resourceName, id) {
55255549
* ## Example:
55265550
*
55275551
* ```js
5528-
* DS.lastModified('document', 5); // undefined
5552+
* DS.lastModified('document', 5); // undefined
55295553
*
5530-
* DS.find('document', 5).then(function (document) {
5531-
* DS.lastModified('document', 5); // 1234235825494
5532-
* });
5554+
* DS.find('document', 5).then(function (document) {
5555+
* DS.lastModified('document', 5); // 1234235825494
5556+
* });
55335557
* ```
55345558
*
55355559
* ## Throws
@@ -5544,18 +5568,19 @@ function errorPrefix(resourceName, id) {
55445568
*/
55455569
function lastModified(resourceName, id) {
55465570
var DS = this;
5571+
var resource = DS.store[resourceName];
55475572
if (!DS.definitions[resourceName]) {
55485573
throw new DS.errors.NER(errorPrefix(resourceName, id) + resourceName);
55495574
} else if (id && !DS.utils.isString(id) && !DS.utils.isNumber(id)) {
55505575
throw new DS.errors.IA(errorPrefix(resourceName, id) + 'id: Must be a string or a number!');
55515576
}
55525577
if (id) {
5553-
if (!(id in DS.store[resourceName].modified)) {
5554-
DS.store[resourceName].modified[id] = 0;
5578+
if (!(id in resource.modified)) {
5579+
resource.modified[id] = 0;
55555580
}
5556-
return DS.store[resourceName].modified[id];
5581+
return resource.modified[id];
55575582
}
5558-
return DS.store[resourceName].collectionModified;
5583+
return resource.collectionModified;
55595584
}
55605585

55615586
module.exports = lastModified;
@@ -5581,18 +5606,20 @@ function errorPrefix(resourceName, id) {
55815606
* ## Example:
55825607
*
55835608
* ```js
5584-
* DS.lastModified('document', 5); // undefined
5585-
* DS.lastSaved('document', 5); // undefined
5609+
* DS.lastModified('document', 5); // undefined
5610+
* DS.lastSaved('document', 5); // undefined
5611+
*
5612+
* DS.find('document', 5).then(function (document) {
5613+
* DS.lastModified('document', 5); // 1234235825494
5614+
* DS.lastSaved('document', 5); // 1234235825494
55865615
*
5587-
* DS.find('document', 5).then(function (document) {
5588-
* DS.lastModified('document', 5); // 1234235825494
5589-
* DS.lastSaved('document', 5); // 1234235825494
5616+
* document.author = 'Sally';
55905617
*
5591-
* document.author = 'Sally';
5618+
* // You may have to call $scope.$apply() first
55925619
*
5593-
* DS.lastModified('document', 5); // 1234304985344 - something different
5594-
* DS.lastSaved('document', 5); // 1234235825494 - still the same
5595-
* });
5620+
* DS.lastModified('document', 5); // 1234304985344 - something different
5621+
* DS.lastSaved('document', 5); // 1234235825494 - still the same
5622+
* });
55965623
* ```
55975624
*
55985625
* ## Throws
@@ -5606,15 +5633,16 @@ function errorPrefix(resourceName, id) {
56065633
*/
56075634
function lastSaved(resourceName, id) {
56085635
var DS = this;
5636+
var resource = DS.store[resourceName];
56095637
if (!DS.definitions[resourceName]) {
56105638
throw new DS.errors.NER(errorPrefix(resourceName, id) + resourceName);
56115639
} else if (!DS.utils.isString(id) && !DS.utils.isNumber(id)) {
56125640
throw new DS.errors.IA(errorPrefix(resourceName, id) + 'id: Must be a string or a number!');
56135641
}
5614-
if (!(id in DS.store[resourceName].saved)) {
5615-
DS.store[resourceName].saved[id] = 0;
5642+
if (!(id in resource.saved)) {
5643+
resource.saved[id] = 0;
56165644
}
5617-
return DS.store[resourceName].saved[id];
5645+
return resource.saved[id];
56185646
}
56195647

56205648
module.exports = lastSaved;
@@ -5646,6 +5674,8 @@ function errorPrefix(resourceName, id) {
56465674
*
56475675
* d; // { author: 'Sally', id: 5 }
56485676
*
5677+
* // You may have to do $scope.$apply() first
5678+
*
56495679
* DS.previous('document', 5); // { author: 'John Anderson', id: 5 }
56505680
* ```
56515681
*

dist/angular-data.min.js

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

src/datastore/sync_methods/bindAll.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ function errorPrefix(resourceName) {
1919
* ```js
2020
* // bind the documents with ownerId of 5 to the 'docs' property of the $scope
2121
* var deregisterFunc = DS.bindAll($scope, 'docs', 'document', {
22-
* where: {
23-
* ownerId: 5
24-
* }
22+
* where: {
23+
* ownerId: 5
24+
* }
2525
* });
2626
* ```
2727
*

src/datastore/sync_methods/changes.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ function errorPrefix(resourceName) {
99
* @description
1010
* Synchronously return the changes object of the item of the type specified by `resourceName` that has the primary key
1111
* specified by `id`. This object represents the diff between the item in its current state and the state of the item
12-
* the last time it was saved via an async adapter.
12+
* the last time it was saved via an adapter.
1313
*
1414
* ## Signature:
1515
* ```js
@@ -23,6 +23,8 @@ function errorPrefix(resourceName) {
2323
*
2424
* d.author = 'Sally';
2525
*
26+
* // You might have to do $scope.$apply() first
27+
*
2628
* DS.changes('document', 5); // {...} Object describing changes
2729
* ```
2830
*

0 commit comments

Comments
 (0)