@@ -3891,7 +3891,7 @@ function DSProvider() {
38913891
38923892module . 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 ) {
38953895var errorPrefix = 'DS.bindAll(scope, expr, resourceName, params[, cb]): ' ;
38963896
38973897/**
@@ -4096,6 +4096,76 @@ function changes(resourceName, id) {
40964096module . 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*/
41004170var errorPrefix = 'DS.defineResource(definition): ' ;
41014171
@@ -4259,7 +4329,7 @@ function defineResource(definition) {
42594329
42604330module . exports = defineResource ;
42614331
4262- } , { } ] , 49 :[ function ( require , module , exports ) {
4332+ } , { } ] , 50 :[ function ( require , module , exports ) {
42634333var observe = require ( '../../../lib/observe-js/observe-js' ) ;
42644334
42654335/**
@@ -4294,7 +4364,7 @@ function digest() {
42944364
42954365module . 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 ) {
42984368var errorPrefix = 'DS.eject(resourceName, id): ' ;
42994369
43004370function _eject ( definition , resource , id ) {
@@ -4372,7 +4442,7 @@ function eject(resourceName, id) {
43724442
43734443module . exports = eject ;
43744444
4375- } , { } ] , 51 :[ function ( require , module , exports ) {
4445+ } , { } ] , 52 :[ function ( require , module , exports ) {
43764446var errorPrefix = 'DS.ejectAll(resourceName[, params]): ' ;
43774447
43784448function _ejectAll ( definition , resource , params ) {
@@ -4480,7 +4550,7 @@ function ejectAll(resourceName, params) {
44804550
44814551module . exports = ejectAll ;
44824552
4483- } , { } ] , 52 :[ function ( require , module , exports ) {
4553+ } , { } ] , 53 :[ function ( require , module , exports ) {
44844554var errorPrefix = 'DS.filter(resourceName[, params][, options]): ' ;
44854555
44864556/**
@@ -4561,7 +4631,7 @@ function filter(resourceName, params, options) {
45614631
45624632module . exports = filter ;
45634633
4564- } , { } ] , 53 :[ function ( require , module , exports ) {
4634+ } , { } ] , 54 :[ function ( require , module , exports ) {
45654635var 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 */
45974669function get ( resourceName , id , options ) {
@@ -4622,7 +4694,7 @@ function get(resourceName, id, options) {
46224694
46234695module . exports = get ;
46244696
4625- } , { } ] , 54 :[ function ( require , module , exports ) {
4697+ } , { } ] , 55 :[ function ( require , module , exports ) {
46264698var errorPrefix = 'DS.hasChanges(resourceName, id): ' ;
46274699
46284700function diffIsEmpty ( utils , diff ) {
@@ -4680,17 +4752,8 @@ function hasChanges(resourceName, id) {
46804752
46814753module . exports = hasChanges ;
46824754
4683- } , { } ] , 55 :[ function ( require , module , exports ) {
4755+ } , { } ] , 56 :[ function ( require , module , exports ) {
46844756module . 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 ) {
48274910var observe = require ( '../../../lib/observe-js/observe-js' ) ;
48284911var errorPrefix = 'DS.inject(resourceName, attrs[, options]): ' ;
48294912
@@ -5026,7 +5109,7 @@ function inject(resourceName, attrs, options) {
50265109
50275110module . 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 ) {
50305113var errorPrefix = 'DS.lastModified(resourceName[, id]): ' ;
50315114
50325115/**
@@ -5079,7 +5162,7 @@ function lastModified(resourceName, id) {
50795162
50805163module . exports = lastModified ;
50815164
5082- } , { } ] , 58 :[ function ( require , module , exports ) {
5165+ } , { } ] , 59 :[ function ( require , module , exports ) {
50835166var errorPrefix = 'DS.lastSaved(resourceName[, id]): ' ;
50845167
50855168/**
@@ -5135,7 +5218,7 @@ function lastSaved(resourceName, id) {
51355218
51365219module . exports = lastSaved ;
51375220
5138- } , { } ] , 59 :[ function ( require , module , exports ) {
5221+ } , { } ] , 60 :[ function ( require , module , exports ) {
51395222var errorPrefix = 'DS.previous(resourceName, id): ' ;
51405223
51415224/**
@@ -5185,7 +5268,7 @@ function previous(resourceName, id) {
51855268
51865269module . 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 ) {
54055488module . 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 ] ) ;
0 commit comments