@@ -2199,6 +2199,7 @@ function errorPrefix(resourceName) {
21992199 * @param {object } attrs The attributes with which to create the item of the type specified by `resourceName`.
22002200 * @param {object= } options Configuration options. Also passed along to the adapter's `create` method. Properties:
22012201 *
2202+ * - `{boolean=}` - `useClass` - Whether to wrap the injected item with the resource's instance constructor.
22022203 * - `{boolean=}` - `cacheResponse` - Inject the data returned by the adapter into the data store. Default: `true`.
22032204 * - `{boolean=}` - `upsert` - If `attrs` already contains a primary key, then attempt to call `DS.update` instead. Default: `true`.
22042205 * - `{function=}` - `beforeValidate` - Override the resource or global lifecycle hook.
@@ -2280,7 +2281,7 @@ function create(resourceName, attrs, options) {
22802281 resource . saved [ id ] = DS . utils . updateTimestamp ( resource . saved [ id ] ) ;
22812282 return DS . get ( definition . name , id ) ;
22822283 } else {
2283- return data ;
2284+ return DS . createInstance ( resourceName , data , options ) ;
22842285 }
22852286 } ) ;
22862287 }
@@ -2510,6 +2511,7 @@ function errorPrefix(resourceName, id) {
25102511 * @param {string|number } id The primary key of the item to retrieve.
25112512 * @param {object= } options Optional configuration. Also passed along to the adapter's `find` method. Properties:
25122513 *
2514+ * - `{boolean=}` - `useClass` - Whether to wrap the injected item with the resource's instance constructor.
25132515 * - `{boolean=}` - `bypassCache` - Bypass the cache. Default: `false`.
25142516 * - `{boolean=}` - `cacheResponse` - Inject the data returned by the adapter into the data store. Default: `true`.
25152517 *
@@ -2563,7 +2565,7 @@ function find(resourceName, id, options) {
25632565 resource . completedQueries [ id ] = new Date ( ) . getTime ( ) ;
25642566 return DS . inject ( resourceName , data , options ) ;
25652567 } else {
2566- return data ;
2568+ return DS . createInstance ( resourceName , data , options ) ;
25672569 }
25682570 } , function ( err ) {
25692571 delete resource . pendingQueries [ id ] ;
@@ -2640,6 +2642,7 @@ function _findAll(resourceName, params, options) {
26402642 // This particular query has never even been made
26412643 resource . pendingQueries [ queryHash ] = DS . adapters [ options . adapter || definition . defaultAdapter ] . findAll ( definition , params , options )
26422644 . then ( function ( res ) {
2645+ delete resource . pendingQueries [ queryHash ] ;
26432646 var data = definition . deserialize ( resourceName , res ) ;
26442647 if ( options . cacheResponse ) {
26452648 try {
@@ -2648,6 +2651,10 @@ function _findAll(resourceName, params, options) {
26482651 return DS . $q . reject ( err ) ;
26492652 }
26502653 } else {
2654+ console . log ( data ) ;
2655+ DS . utils . forEach ( data , function ( item , i ) {
2656+ data [ i ] = DS . createInstance ( resourceName , item , options ) ;
2657+ } ) ;
26512658 return data ;
26522659 }
26532660 } , function ( err ) {
@@ -2708,6 +2715,7 @@ function _findAll(resourceName, params, options) {
27082715 *
27092716 * @param {object= } options Optional configuration. Also passed along to the adapter's `findAll` method. Properties:
27102717 *
2718+ * - `{boolean=}` - `useClass` - Whether to wrap the injected item with the resource's instance constructor.
27112719 * - `{boolean=}` - `bypassCache` - Bypass the cache. Default: `false`.
27122720 * - `{boolean=}` - `cacheResponse` - Inject the data returned by the adapter into the data store. Default: `true`.
27132721 *
@@ -2970,7 +2978,9 @@ function loadRelations(resourceName, instance, relations, options) {
29702978 if ( def . localKey && instance [ def . localKey ] ) {
29712979 task = DS . find ( relationName , instance [ def . localKey ] , options ) ;
29722980 } else if ( def . foreignKey ) {
2973- task = DS . findAll ( relationName , params , options ) ;
2981+ task = DS . findAll ( relationName , params , options ) . then ( function ( hasOnes ) {
2982+ return hasOnes . length ? hasOnes [ 0 ] : null ;
2983+ } ) ;
29742984 }
29752985 } else {
29762986 task = DS . find ( relationName , instance [ def . localKey ] , options ) ;
@@ -3681,7 +3691,7 @@ Defaults.prototype.defaultFilter = function (collection, resourceName, params, o
36813691} ;
36823692Defaults . prototype . baseUrl = '' ;
36833693Defaults . prototype . endpoint = '' ;
3684- Defaults . prototype . useClass = false ;
3694+ Defaults . prototype . useClass = true ;
36853695/**
36863696 * @doc property
36873697 * @id DSProvider.properties:defaults.beforeValidate
@@ -4300,6 +4310,8 @@ function bindAll(scope, expr, resourceName, params, cb) {
43004310 var DS = this ;
43014311 var IA = DS . errors . IA ;
43024312
4313+ params = params || { } ;
4314+
43034315 if ( ! DS . utils . isObject ( scope ) ) {
43044316 throw new IA ( errorPrefix ( resourceName ) + 'scope: Must be an object!' ) ;
43054317 } else if ( ! DS . utils . isString ( expr ) ) {
@@ -4625,7 +4637,7 @@ function errorPrefix(resourceName) {
46254637 * @param {object= } attrs Optional attributes to mix in to the new instance.
46264638 * @param {object= } options Optional configuration. Properties:
46274639 *
4628- * - `{boolean=}` - `useClass` - Whether to use the resource's wrapper class. Default: `true` .
4640+ * - `{boolean=}` - `useClass` - Whether to wrap the injected item with the resource's instance constructor .
46294641 *
46304642 * @returns {object } The new instance.
46314643 */
@@ -4646,7 +4658,7 @@ function createInstance(resourceName, attrs, options) {
46464658 }
46474659
46484660 if ( ! ( 'useClass' in options ) ) {
4649- options . useClass = true ;
4661+ options . useClass = definition . useClass ;
46504662 }
46514663
46524664 var item ;
@@ -5650,7 +5662,7 @@ function errorPrefix(resourceName) {
56505662 return 'DS.inject(' + resourceName + ', attrs[, options]): ' ;
56515663}
56525664
5653- function _inject ( definition , resource , attrs ) {
5665+ function _inject ( definition , resource , attrs , options ) {
56545666 var DS = this ;
56555667 var $log = DS . $log ;
56565668
@@ -5707,7 +5719,7 @@ function _inject(definition, resource, attrs) {
57075719 if ( DS . utils . isArray ( attrs ) ) {
57085720 injected = [ ] ;
57095721 for ( var i = 0 ; i < attrs . length ; i ++ ) {
5710- injected . push ( _inject . call ( DS , definition , resource , attrs [ i ] ) ) ;
5722+ injected . push ( _inject . call ( DS , definition , resource , attrs [ i ] , options ) ) ;
57115723 }
57125724 } else {
57135725 // check if "idAttribute" is a computed property
@@ -5731,7 +5743,7 @@ function _inject(definition, resource, attrs) {
57315743 var item = DS . get ( definition . name , id ) ;
57325744
57335745 if ( ! item ) {
5734- if ( definition . methods || definition . useClass ) {
5746+ if ( options . useClass ) {
57355747 if ( attrs instanceof definition [ definition . class ] ) {
57365748 item = attrs ;
57375749 } else {
@@ -5862,6 +5874,7 @@ function _injectRelations(definition, injected, options) {
58625874 * @param {object|array } attrs The item or collection of items to inject into the data store.
58635875 * @param {object= } options The item or collection of items to inject into the data store. Properties:
58645876 *
5877+ * - `{boolean=}` - `useClass` - Whether to wrap the injected item with the resource's instance constructor.
58655878 * - `{boolean=}` - `findBelongsTo` - Find and attach any existing "belongsTo" relationships to the newly injected item. Potentially expensive if enabled. Default: `false`.
58665879 * - `{boolean=}` - `findHasMany` - Find and attach any existing "hasMany" relationships to the newly injected item. Potentially expensive if enabled. Default: `false`.
58675880 * - `{boolean=}` - `findHasOne` - Find and attach any existing "hasOne" relationships to the newly injected item. Potentially expensive if enabled. Default: `false`.
@@ -5890,12 +5903,15 @@ function inject(resourceName, attrs, options) {
58905903 stack ++ ;
58915904
58925905 try {
5906+ if ( ! ( 'useClass' in options ) ) {
5907+ options . useClass = definition . useClass ;
5908+ }
58935909 if ( ! DS . $rootScope . $$phase ) {
58945910 DS . $rootScope . $apply ( function ( ) {
5895- injected = _inject . call ( DS , definition , resource , attrs ) ;
5911+ injected = _inject . call ( DS , definition , resource , attrs , options ) ;
58965912 } ) ;
58975913 } else {
5898- injected = _inject . call ( DS , definition , resource , attrs ) ;
5914+ injected = _inject . call ( DS , definition , resource , attrs , options ) ;
58995915 }
59005916 if ( definition . relations ) {
59015917 _injectRelations . call ( DS , definition , injected , options ) ;
0 commit comments