@@ -4127,9 +4127,9 @@ function errorPrefix(resourceName) {
4127
4127
* ```js
4128
4128
* // bind the documents with ownerId of 5 to the 'docs' property of the $scope
4129
4129
* var deregisterFunc = DS.bindAll($scope, 'docs', 'document', {
4130
- * where: {
4131
- * ownerId: 5
4132
- * }
4130
+ * where: {
4131
+ * ownerId: 5
4132
+ * }
4133
4133
* });
4134
4134
* ```
4135
4135
*
@@ -4271,7 +4271,7 @@ function errorPrefix(resourceName) {
4271
4271
* @description
4272
4272
* Synchronously return the changes object of the item of the type specified by `resourceName` that has the primary key
4273
4273
* 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.
4275
4275
*
4276
4276
* ## Signature:
4277
4277
* ```js
@@ -4285,6 +4285,8 @@ function errorPrefix(resourceName) {
4285
4285
*
4286
4286
* d.author = 'Sally';
4287
4287
*
4288
+ * // You might have to do $scope.$apply() first
4289
+ *
4288
4290
* DS.changes('document', 5); // {...} Object describing changes
4289
4291
* ```
4290
4292
*
@@ -4344,12 +4346,35 @@ function errorPrefix(resourceName) {
4344
4346
* ## Example:
4345
4347
*
4346
4348
* ```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
4349
4366
*
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 });
4351
4369
*
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
4353
4378
* ```
4354
4379
*
4355
4380
* ## Throws
@@ -4363,16 +4388,17 @@ function errorPrefix(resourceName) {
4363
4388
*
4364
4389
* - `{boolean=}` - `useClass` - Whether to use the resource's wrapper class. Default: `true`.
4365
4390
*
4366
- * @returns {object } The new instance
4391
+ * @returns {object } The new instance.
4367
4392
*/
4368
4393
function createInstance ( resourceName , attrs , options ) {
4369
4394
var DS = this ;
4370
4395
var IA = DS . errors . IA ;
4396
+ var definition = DS . definitions [ resourceName ] ;
4371
4397
4372
4398
attrs = attrs || { } ;
4373
4399
options = options || { } ;
4374
4400
4375
- if ( ! DS . definitions [ resourceName ] ) {
4401
+ if ( ! definition ) {
4376
4402
throw new DS . errors . NER ( errorPrefix ( resourceName ) + resourceName ) ;
4377
4403
} else if ( attrs && ! DS . utils . isObject ( attrs ) ) {
4378
4404
throw new IA ( errorPrefix ( resourceName ) + 'attrs: Must be an object!' ) ;
@@ -4387,7 +4413,7 @@ function createInstance(resourceName, attrs, options) {
4387
4413
var item ;
4388
4414
4389
4415
if ( options . useClass ) {
4390
- var Func = DS . definitions [ resourceName ] [ DS . definitions [ resourceName ] . class ] ;
4416
+ var Func = definition [ definition . class ] ;
4391
4417
item = new Func ( ) ;
4392
4418
} else {
4393
4419
item = { } ;
@@ -4688,7 +4714,8 @@ var observe = require('../../../lib/observe-js/observe-js');
4688
4714
* @name digest
4689
4715
* @description
4690
4716
* 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.
4692
4719
*
4693
4720
* ## Signature:
4694
4721
* ```js
@@ -4753,7 +4780,7 @@ function _eject(definition, resource, id) {
4753
4780
* @name eject
4754
4781
* @description
4755
4782
* 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 .
4757
4784
*
4758
4785
* ## Signature:
4759
4786
* ```js
@@ -4785,12 +4812,12 @@ function _eject(definition, resource, id) {
4785
4812
*/
4786
4813
function eject ( resourceName , id ) {
4787
4814
var DS = this ;
4788
- if ( ! DS . definitions [ resourceName ] ) {
4815
+ var definition = DS . definitions [ resourceName ] ;
4816
+ if ( ! definition ) {
4789
4817
throw new DS . errors . NER ( errorPrefix ( resourceName , id ) + resourceName ) ;
4790
4818
} else if ( ! DS . utils . isString ( id ) && ! DS . utils . isNumber ( id ) ) {
4791
4819
throw new DS . errors . IA ( errorPrefix ( resourceName , id ) + 'id: Must be a string or a number!' ) ;
4792
4820
}
4793
- var definition = DS . definitions [ resourceName ] ;
4794
4821
var resource = DS . store [ resourceName ] ;
4795
4822
var ejected ;
4796
4823
@@ -4835,9 +4862,8 @@ function _ejectAll(definition, resource, params) {
4835
4862
* @id DS.sync_methods:ejectAll
4836
4863
* @name ejectAll
4837
4864
* @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.
4841
4867
*
4842
4868
* ## Signature:
4843
4869
* ```js
@@ -4882,7 +4908,7 @@ function _ejectAll(definition, resource, params) {
4882
4908
* - `{NonexistentResourceError}`
4883
4909
*
4884
4910
* @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:
4886
4912
*
4887
4913
* - `{object=}` - `where` - Where clause.
4888
4914
* - `{number=}` - `limit` - Limit clause.
@@ -4894,15 +4920,14 @@ function _ejectAll(definition, resource, params) {
4894
4920
*/
4895
4921
function ejectAll ( resourceName , params ) {
4896
4922
var DS = this ;
4923
+ var definition = DS . definitions [ resourceName ] ;
4897
4924
params = params || { } ;
4898
4925
4899
- if ( ! DS . definitions [ resourceName ] ) {
4926
+ if ( ! definition ) {
4900
4927
throw new DS . errors . NER ( errorPrefix ( resourceName ) + resourceName ) ;
4901
4928
} else if ( ! DS . utils . isObject ( params ) ) {
4902
4929
throw new DS . errors . IA ( errorPrefix ( resourceName ) + 'params: Must be an object!' ) ;
4903
4930
}
4904
-
4905
- var definition = DS . definitions [ resourceName ] ;
4906
4931
var resource = DS . store [ resourceName ] ;
4907
4932
var ejected ;
4908
4933
@@ -4942,17 +4967,15 @@ function errorPrefix(resourceName) {
4942
4967
*
4943
4968
* ## Example:
4944
4969
*
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).
4948
4971
*
4949
4972
* ## Throws
4950
4973
*
4951
4974
* - `{IllegalArgumentError}`
4952
4975
* - `{NonexistentResourceError}`
4953
4976
*
4954
4977
* @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:
4956
4979
*
4957
4980
* - `{object=}` - `where` - Where clause.
4958
4981
* - `{number=}` - `limit` - Limit clause.
@@ -4970,18 +4993,17 @@ function errorPrefix(resourceName) {
4970
4993
function filter ( resourceName , params , options ) {
4971
4994
var DS = this ;
4972
4995
var IA = DS . errors . IA ;
4996
+ var definition = DS . definitions [ resourceName ] ;
4973
4997
4974
4998
options = options || { } ;
4975
4999
4976
- if ( ! DS . definitions [ resourceName ] ) {
5000
+ if ( ! definition ) {
4977
5001
throw new DS . errors . NER ( errorPrefix ( resourceName ) + resourceName ) ;
4978
5002
} else if ( params && ! DS . utils . isObject ( params ) ) {
4979
5003
throw new IA ( errorPrefix ( resourceName ) + 'params: Must be an object!' ) ;
4980
5004
} else if ( ! DS . utils . isObject ( options ) ) {
4981
5005
throw new IA ( errorPrefix ( resourceName ) + 'options: Must be an object!' ) ;
4982
5006
}
4983
-
4984
- var definition = DS . definitions [ resourceName ] ;
4985
5007
var resource = DS . store [ resourceName ] ;
4986
5008
4987
5009
// Protect against null
@@ -5019,8 +5041,8 @@ function errorPrefix(resourceName, id) {
5019
5041
* @id DS.sync_methods:get
5020
5042
* @name get
5021
5043
* @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.
5024
5046
*
5025
5047
* ## Signature:
5026
5048
* ```js
@@ -5040,7 +5062,7 @@ function errorPrefix(resourceName, id) {
5040
5062
*
5041
5063
* @param {string } resourceName The resource type, e.g. 'user', 'comment', etc.
5042
5064
* @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:
5044
5066
*
5045
5067
* - `{boolean=}` - `loadFromServer` - Send the query to server if it has not been sent yet. Default: `false`.
5046
5068
*
@@ -5062,7 +5084,7 @@ function get(resourceName, id, options) {
5062
5084
// cache miss, request resource from server
5063
5085
var item = DS . store [ resourceName ] . index . get ( id ) ;
5064
5086
if ( ! item && options . loadFromServer ) {
5065
- DS . find ( resourceName , id ) . then ( null , function ( err ) {
5087
+ DS . find ( resourceName , id , options ) . then ( null , function ( err ) {
5066
5088
return DS . $q . reject ( err ) ;
5067
5089
} ) ;
5068
5090
}
@@ -5104,6 +5126,8 @@ function diffIsEmpty(utils, diff) {
5104
5126
*
5105
5127
* d.author = 'Sally';
5106
5128
*
5129
+ * // You may have to do $scope.$apply() first
5130
+ *
5107
5131
* DS.hasChanges('document', 5); // true
5108
5132
* ```
5109
5133
*
@@ -5525,11 +5549,11 @@ function errorPrefix(resourceName, id) {
5525
5549
* ## Example:
5526
5550
*
5527
5551
* ```js
5528
- * DS.lastModified('document', 5); // undefined
5552
+ * DS.lastModified('document', 5); // undefined
5529
5553
*
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
+ * });
5533
5557
* ```
5534
5558
*
5535
5559
* ## Throws
@@ -5544,18 +5568,19 @@ function errorPrefix(resourceName, id) {
5544
5568
*/
5545
5569
function lastModified ( resourceName , id ) {
5546
5570
var DS = this ;
5571
+ var resource = DS . store [ resourceName ] ;
5547
5572
if ( ! DS . definitions [ resourceName ] ) {
5548
5573
throw new DS . errors . NER ( errorPrefix ( resourceName , id ) + resourceName ) ;
5549
5574
} else if ( id && ! DS . utils . isString ( id ) && ! DS . utils . isNumber ( id ) ) {
5550
5575
throw new DS . errors . IA ( errorPrefix ( resourceName , id ) + 'id: Must be a string or a number!' ) ;
5551
5576
}
5552
5577
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 ;
5555
5580
}
5556
- return DS . store [ resourceName ] . modified [ id ] ;
5581
+ return resource . modified [ id ] ;
5557
5582
}
5558
- return DS . store [ resourceName ] . collectionModified ;
5583
+ return resource . collectionModified ;
5559
5584
}
5560
5585
5561
5586
module . exports = lastModified ;
@@ -5581,18 +5606,20 @@ function errorPrefix(resourceName, id) {
5581
5606
* ## Example:
5582
5607
*
5583
5608
* ```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
5586
5615
*
5587
- * DS.find('document', 5).then(function (document) {
5588
- * DS.lastModified('document', 5); // 1234235825494
5589
- * DS.lastSaved('document', 5); // 1234235825494
5616
+ * document.author = 'Sally';
5590
5617
*
5591
- * document.author = 'Sally';
5618
+ * // You may have to call $scope.$apply() first
5592
5619
*
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
+ * });
5596
5623
* ```
5597
5624
*
5598
5625
* ## Throws
@@ -5606,15 +5633,16 @@ function errorPrefix(resourceName, id) {
5606
5633
*/
5607
5634
function lastSaved ( resourceName , id ) {
5608
5635
var DS = this ;
5636
+ var resource = DS . store [ resourceName ] ;
5609
5637
if ( ! DS . definitions [ resourceName ] ) {
5610
5638
throw new DS . errors . NER ( errorPrefix ( resourceName , id ) + resourceName ) ;
5611
5639
} else if ( ! DS . utils . isString ( id ) && ! DS . utils . isNumber ( id ) ) {
5612
5640
throw new DS . errors . IA ( errorPrefix ( resourceName , id ) + 'id: Must be a string or a number!' ) ;
5613
5641
}
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 ;
5616
5644
}
5617
- return DS . store [ resourceName ] . saved [ id ] ;
5645
+ return resource . saved [ id ] ;
5618
5646
}
5619
5647
5620
5648
module . exports = lastSaved ;
@@ -5646,6 +5674,8 @@ function errorPrefix(resourceName, id) {
5646
5674
*
5647
5675
* d; // { author: 'Sally', id: 5 }
5648
5676
*
5677
+ * // You may have to do $scope.$apply() first
5678
+ *
5649
5679
* DS.previous('document', 5); // { author: 'John Anderson', id: 5 }
5650
5680
* ```
5651
5681
*
0 commit comments