Skip to content

Commit fedd01b

Browse files
committed
Closes #93.
1 parent 9b24471 commit fedd01b

File tree

7 files changed

+273
-36
lines changed

7 files changed

+273
-36
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
##### 0.10.1 - xx July 2014
22

3+
##### Backwards compatible API changes
4+
- #93 - Added `DS.createInstance(resourceName[, attrs][, options])`
5+
36
###### Backwards compatible bug fixes
47
- #90 - DS.create isn't added to completedQueries (`DS.create` now adds a completed query entry)
58
- #91 - dist/angular-data(.min).js doesn't end with a semicolon (upgraded Browserify)

dist/angular-data.js

Lines changed: 108 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3891,7 +3891,7 @@ function DSProvider() {
38913891

38923892
module.exports = DSProvider;
38933893

3894-
},{"../utils":62,"./async_methods":38,"./sync_methods":55}],45:[function(require,module,exports){
3894+
},{"../utils":63,"./async_methods":38,"./sync_methods":56}],45:[function(require,module,exports){
38953895
var errorPrefix = 'DS.bindAll(scope, expr, resourceName, params[, cb]): ';
38963896

38973897
/**
@@ -4096,6 +4096,76 @@ function changes(resourceName, id) {
40964096
module.exports = changes;
40974097

40984098
},{}],48:[function(require,module,exports){
4099+
var errorPrefix = 'DS.createInstance(resourceName[, attrs][, options]): ';
4100+
4101+
/**
4102+
* @doc method
4103+
* @id DS.sync_methods:createInstance
4104+
* @name createInstance
4105+
* @description
4106+
* Return a new instance of the specified resource.
4107+
*
4108+
* ## Signature:
4109+
* ```js
4110+
* DS.createInstance(resourceName[, attrs][, options])
4111+
* ```
4112+
*
4113+
* ## Example:
4114+
*
4115+
* ```js
4116+
* // Thanks to createInstance, you don't have to do this anymore
4117+
* var User = DS.definitions.user[DS.definitions.user.class];
4118+
*
4119+
* var user = DS.createInstance('user');
4120+
*
4121+
* user instanceof User; // true
4122+
* ```
4123+
*
4124+
* ## Throws
4125+
*
4126+
* - `{IllegalArgumentError}`
4127+
* - `{NonexistentResourceError}`
4128+
*
4129+
* @param {string} resourceName The resource type, e.g. 'user', 'comment', etc.
4130+
* @param {object=} attrs Optional attributes to mix in to the new instance.
4131+
* @param {object=} options Optional configuration. Properties:
4132+
*
4133+
* - `{boolean=}` - `useClass` - Whether to use the resource's wrapper class. Default: `true`.
4134+
*
4135+
* @returns {object} The new instance
4136+
*/
4137+
function createInstance(resourceName, attrs, options) {
4138+
var IA = this.errors.IA;
4139+
4140+
attrs = attrs || {};
4141+
options = options || {};
4142+
4143+
if (!this.definitions[resourceName]) {
4144+
throw new this.errors.NER(errorPrefix + resourceName);
4145+
} else if (attrs && !this.utils.isObject(attrs)) {
4146+
throw new IA(errorPrefix + 'attrs: Must be an object!');
4147+
} else if (!this.utils.isObject(options)) {
4148+
throw new IA(errorPrefix + 'options: Must be an object!');
4149+
}
4150+
4151+
if (!('useClass' in options)) {
4152+
options.useClass = true;
4153+
}
4154+
4155+
var item;
4156+
4157+
if (options.useClass) {
4158+
var Func = this.definitions[resourceName][this.definitions[resourceName].class];
4159+
item = new Func();
4160+
} else {
4161+
item = {};
4162+
}
4163+
return this.utils.deepMixIn(item, attrs);
4164+
}
4165+
4166+
module.exports = createInstance;
4167+
4168+
},{}],49:[function(require,module,exports){
40994169
/*jshint evil:true*/
41004170
var errorPrefix = 'DS.defineResource(definition): ';
41014171

@@ -4259,7 +4329,7 @@ function defineResource(definition) {
42594329

42604330
module.exports = defineResource;
42614331

4262-
},{}],49:[function(require,module,exports){
4332+
},{}],50:[function(require,module,exports){
42634333
var observe = require('../../../lib/observe-js/observe-js');
42644334

42654335
/**
@@ -4294,7 +4364,7 @@ function digest() {
42944364

42954365
module.exports = digest;
42964366

4297-
},{"../../../lib/observe-js/observe-js":1}],50:[function(require,module,exports){
4367+
},{"../../../lib/observe-js/observe-js":1}],51:[function(require,module,exports){
42984368
var errorPrefix = 'DS.eject(resourceName, id): ';
42994369

43004370
function _eject(definition, resource, id) {
@@ -4372,7 +4442,7 @@ function eject(resourceName, id) {
43724442

43734443
module.exports = eject;
43744444

4375-
},{}],51:[function(require,module,exports){
4445+
},{}],52:[function(require,module,exports){
43764446
var errorPrefix = 'DS.ejectAll(resourceName[, params]): ';
43774447

43784448
function _ejectAll(definition, resource, params) {
@@ -4480,7 +4550,7 @@ function ejectAll(resourceName, params) {
44804550

44814551
module.exports = ejectAll;
44824552

4483-
},{}],52:[function(require,module,exports){
4553+
},{}],53:[function(require,module,exports){
44844554
var errorPrefix = 'DS.filter(resourceName[, params][, options]): ';
44854555

44864556
/**
@@ -4561,7 +4631,7 @@ function filter(resourceName, params, options) {
45614631

45624632
module.exports = filter;
45634633

4564-
},{}],53:[function(require,module,exports){
4634+
},{}],54:[function(require,module,exports){
45654635
var errorPrefix = 'DS.get(resourceName, id[, options]): ';
45664636

45674637
/**
@@ -4591,7 +4661,9 @@ var errorPrefix = 'DS.get(resourceName, id[, options]): ';
45914661
* @param {string} resourceName The resource type, e.g. 'user', 'comment', etc.
45924662
* @param {string|number} id The primary key of the item to retrieve.
45934663
* @param {object=} options Optional configuration. Properties:
4664+
*
45944665
* - `{boolean=}` - `loadFromServer` - Send the query to server if it has not been sent yet. Default: `false`.
4666+
*
45954667
* @returns {object} The item of the type specified by `resourceName` with the primary key specified by `id`.
45964668
*/
45974669
function get(resourceName, id, options) {
@@ -4622,7 +4694,7 @@ function get(resourceName, id, options) {
46224694

46234695
module.exports = get;
46244696

4625-
},{}],54:[function(require,module,exports){
4697+
},{}],55:[function(require,module,exports){
46264698
var errorPrefix = 'DS.hasChanges(resourceName, id): ';
46274699

46284700
function diffIsEmpty(utils, diff) {
@@ -4680,17 +4752,8 @@ function hasChanges(resourceName, id) {
46804752

46814753
module.exports = hasChanges;
46824754

4683-
},{}],55:[function(require,module,exports){
4755+
},{}],56:[function(require,module,exports){
46844756
module.exports = {
4685-
/**
4686-
* @doc method
4687-
* @id DS.sync_methods:defineResource
4688-
* @name defineResource
4689-
* @methodOf DS
4690-
* @description
4691-
* See [DS.defineResource](/documentation/api/api/DS.sync_methods:defineResource).
4692-
*/
4693-
defineResource: require('./defineResource'),
46944757

46954758
/**
46964759
* @doc method
@@ -4712,6 +4775,26 @@ module.exports = {
47124775
*/
47134776
bindAll: require('./bindAll'),
47144777

4778+
/**
4779+
* @doc method
4780+
* @id DS.sync_methods:createInstance
4781+
* @name createInstance
4782+
* @methodOf DS
4783+
* @description
4784+
* See [DS.createInstance](/documentation/api/api/DS.sync_methods:createInstance).
4785+
*/
4786+
createInstance: require('./createInstance'),
4787+
4788+
/**
4789+
* @doc method
4790+
* @id DS.sync_methods:defineResource
4791+
* @name defineResource
4792+
* @methodOf DS
4793+
* @description
4794+
* See [DS.defineResource](/documentation/api/api/DS.sync_methods:defineResource).
4795+
*/
4796+
defineResource: require('./defineResource'),
4797+
47154798
/**
47164799
* @doc method
47174800
* @id DS.sync_methods:eject
@@ -4823,7 +4906,7 @@ module.exports = {
48234906
hasChanges: require('./hasChanges')
48244907
};
48254908

4826-
},{"./bindAll":45,"./bindOne":46,"./changes":47,"./defineResource":48,"./digest":49,"./eject":50,"./ejectAll":51,"./filter":52,"./get":53,"./hasChanges":54,"./inject":56,"./lastModified":57,"./lastSaved":58,"./previous":59}],56:[function(require,module,exports){
4909+
},{"./bindAll":45,"./bindOne":46,"./changes":47,"./createInstance":48,"./defineResource":49,"./digest":50,"./eject":51,"./ejectAll":52,"./filter":53,"./get":54,"./hasChanges":55,"./inject":57,"./lastModified":58,"./lastSaved":59,"./previous":60}],57:[function(require,module,exports){
48274910
var observe = require('../../../lib/observe-js/observe-js');
48284911
var errorPrefix = 'DS.inject(resourceName, attrs[, options]): ';
48294912

@@ -5026,7 +5109,7 @@ function inject(resourceName, attrs, options) {
50265109

50275110
module.exports = inject;
50285111

5029-
},{"../../../lib/observe-js/observe-js":1}],57:[function(require,module,exports){
5112+
},{"../../../lib/observe-js/observe-js":1}],58:[function(require,module,exports){
50305113
var errorPrefix = 'DS.lastModified(resourceName[, id]): ';
50315114

50325115
/**
@@ -5079,7 +5162,7 @@ function lastModified(resourceName, id) {
50795162

50805163
module.exports = lastModified;
50815164

5082-
},{}],58:[function(require,module,exports){
5165+
},{}],59:[function(require,module,exports){
50835166
var errorPrefix = 'DS.lastSaved(resourceName[, id]): ';
50845167

50855168
/**
@@ -5135,7 +5218,7 @@ function lastSaved(resourceName, id) {
51355218

51365219
module.exports = lastSaved;
51375220

5138-
},{}],59:[function(require,module,exports){
5221+
},{}],60:[function(require,module,exports){
51395222
var errorPrefix = 'DS.previous(resourceName, id): ';
51405223

51415224
/**
@@ -5185,7 +5268,7 @@ function previous(resourceName, id) {
51855268

51865269
module.exports = previous;
51875270

5188-
},{}],60:[function(require,module,exports){
5271+
},{}],61:[function(require,module,exports){
51895272
/**
51905273
* @doc function
51915274
* @id errors.types:IllegalArgumentError
@@ -5318,7 +5401,7 @@ module.exports = [function () {
53185401
};
53195402
}];
53205403

5321-
},{}],61:[function(require,module,exports){
5404+
},{}],62:[function(require,module,exports){
53225405
(function (window, angular, undefined) {
53235406
'use strict';
53245407

@@ -5401,7 +5484,7 @@ module.exports = [function () {
54015484

54025485
})(window, window.angular);
54035486

5404-
},{"./adapters/http":31,"./adapters/localStorage":32,"./datastore":44,"./errors":60,"./utils":62}],62:[function(require,module,exports){
5487+
},{"./adapters/http":31,"./adapters/localStorage":32,"./datastore":44,"./errors":61,"./utils":63}],63:[function(require,module,exports){
54055488
module.exports = [function () {
54065489
return {
54075490
isBoolean: require('mout/lang/isBoolean'),
@@ -5484,4 +5567,4 @@ module.exports = [function () {
54845567
};
54855568
}];
54865569

5487-
},{"mout/array/contains":2,"mout/array/filter":3,"mout/array/slice":7,"mout/array/sort":8,"mout/array/toLookup":9,"mout/lang/isBoolean":14,"mout/lang/isEmpty":15,"mout/object/deepMixIn":22,"mout/object/forOwn":24,"mout/object/pick":27,"mout/object/set":28,"mout/string/makePath":29,"mout/string/upperCase":30}]},{},[61]);
5570+
},{"mout/array/contains":2,"mout/array/filter":3,"mout/array/slice":7,"mout/array/sort":8,"mout/array/toLookup":9,"mout/lang/isBoolean":14,"mout/lang/isEmpty":15,"mout/object/deepMixIn":22,"mout/object/forOwn":24,"mout/object/pick":27,"mout/object/set":28,"mout/string/makePath":29,"mout/string/upperCase":30}]},{},[62]);

dist/angular-data.min.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
var errorPrefix = 'DS.createInstance(resourceName[, attrs][, options]): ';
2+
3+
/**
4+
* @doc method
5+
* @id DS.sync_methods:createInstance
6+
* @name createInstance
7+
* @description
8+
* Return a new instance of the specified resource.
9+
*
10+
* ## Signature:
11+
* ```js
12+
* DS.createInstance(resourceName[, attrs][, options])
13+
* ```
14+
*
15+
* ## Example:
16+
*
17+
* ```js
18+
* // Thanks to createInstance, you don't have to do this anymore
19+
* var User = DS.definitions.user[DS.definitions.user.class];
20+
*
21+
* var user = DS.createInstance('user');
22+
*
23+
* user instanceof User; // true
24+
* ```
25+
*
26+
* ## Throws
27+
*
28+
* - `{IllegalArgumentError}`
29+
* - `{NonexistentResourceError}`
30+
*
31+
* @param {string} resourceName The resource type, e.g. 'user', 'comment', etc.
32+
* @param {object=} attrs Optional attributes to mix in to the new instance.
33+
* @param {object=} options Optional configuration. Properties:
34+
*
35+
* - `{boolean=}` - `useClass` - Whether to use the resource's wrapper class. Default: `true`.
36+
*
37+
* @returns {object} The new instance
38+
*/
39+
function createInstance(resourceName, attrs, options) {
40+
var IA = this.errors.IA;
41+
42+
attrs = attrs || {};
43+
options = options || {};
44+
45+
if (!this.definitions[resourceName]) {
46+
throw new this.errors.NER(errorPrefix + resourceName);
47+
} else if (attrs && !this.utils.isObject(attrs)) {
48+
throw new IA(errorPrefix + 'attrs: Must be an object!');
49+
} else if (!this.utils.isObject(options)) {
50+
throw new IA(errorPrefix + 'options: Must be an object!');
51+
}
52+
53+
if (!('useClass' in options)) {
54+
options.useClass = true;
55+
}
56+
57+
var item;
58+
59+
if (options.useClass) {
60+
var Func = this.definitions[resourceName][this.definitions[resourceName].class];
61+
item = new Func();
62+
} else {
63+
item = {};
64+
}
65+
return this.utils.deepMixIn(item, attrs);
66+
}
67+
68+
module.exports = createInstance;

src/datastore/sync_methods/get.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@ var errorPrefix = 'DS.get(resourceName, id[, options]): ';
2727
* @param {string} resourceName The resource type, e.g. 'user', 'comment', etc.
2828
* @param {string|number} id The primary key of the item to retrieve.
2929
* @param {object=} options Optional configuration. Properties:
30+
*
3031
* - `{boolean=}` - `loadFromServer` - Send the query to server if it has not been sent yet. Default: `false`.
32+
*
3133
* @returns {object} The item of the type specified by `resourceName` with the primary key specified by `id`.
3234
*/
3335
function get(resourceName, id, options) {

src/datastore/sync_methods/index.js

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,4 @@
11
module.exports = {
2-
/**
3-
* @doc method
4-
* @id DS.sync_methods:defineResource
5-
* @name defineResource
6-
* @methodOf DS
7-
* @description
8-
* See [DS.defineResource](/documentation/api/api/DS.sync_methods:defineResource).
9-
*/
10-
defineResource: require('./defineResource'),
112

123
/**
134
* @doc method
@@ -29,6 +20,26 @@ module.exports = {
2920
*/
3021
bindAll: require('./bindAll'),
3122

23+
/**
24+
* @doc method
25+
* @id DS.sync_methods:createInstance
26+
* @name createInstance
27+
* @methodOf DS
28+
* @description
29+
* See [DS.createInstance](/documentation/api/api/DS.sync_methods:createInstance).
30+
*/
31+
createInstance: require('./createInstance'),
32+
33+
/**
34+
* @doc method
35+
* @id DS.sync_methods:defineResource
36+
* @name defineResource
37+
* @methodOf DS
38+
* @description
39+
* See [DS.defineResource](/documentation/api/api/DS.sync_methods:defineResource).
40+
*/
41+
defineResource: require('./defineResource'),
42+
3243
/**
3344
* @doc method
3445
* @id DS.sync_methods:eject

0 commit comments

Comments
 (0)