Skip to content

Commit

Permalink
The rest of the files
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeremy Brown committed Feb 19, 2015
1 parent c578625 commit 23fdf1e
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 54 deletions.
25 changes: 14 additions & 11 deletions addon/adapters/ajax.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export default Adapter.extend({
find: function( type, id, options, findOne ) {
var store = this.get( 'store' ),
model = store.modelFor( type ),
_this = this,
url,
results,
promise,
Expand Down Expand Up @@ -64,7 +65,7 @@ export default Adapter.extend({
response = model.callSerializerForEndpointAction( options.endpoint, 'get', response, store );

// Run the modelize mixin to map keys to models
response = this.modelize( response );
response = _this.modelize( response );

if ( results instanceof Ember.ArrayProxy ) {
tmpResult = [];
Expand All @@ -75,10 +76,10 @@ export default Adapter.extend({
tmpResult = store.createRecord( type, response );
}

this.runPostQueryHooks( tmpResult );
_this.runPostQueryHooks( tmpResult );

return tmpResult;
}.bind( this ), null, 'sl-ember--model.ajaxAdapter:find - then' );
}, null, 'sl-ember--model.ajaxAdapter:find - then' );

// Set the promise on the promiseProxy
results.set( 'promise', promise );
Expand All @@ -101,16 +102,17 @@ export default Adapter.extend({
type : 'DELETE',
data : JSON.stringify({ id: id }),
context : this
};
},
_this = this;

Ember.assert( 'A url is required to delete a model', url );

this.runPreQueryHooks( queryObj );

return icAjax.request( queryObj )
.then( function ajaxAdapterDeleteFinally( response ) {
this.runPostQueryHooks( response );
}.bind( this ) , 'sl-ember-store.ajaxAdapter:deleteRecord' );
_this.runPostQueryHooks( response );
} , 'sl-ember-store.ajaxAdapter:deleteRecord' );
},

/**
Expand All @@ -129,19 +131,20 @@ export default Adapter.extend({
type : 'POST',
data : JSON.stringify( content ),
context : this
};
},
_this = this;

Ember.assert( 'A url property is required to save a model', url );

this.runPreQueryHooks( queryObj );

promise = icAjax.request( queryObj )
.then( function ajaxAdapterSaveResponse( response ) {
var modelized = this.modelize( response );
var modelized = _this.modelize( response );
// run the modelize mixin to map keys to models
this.runPostQueryHooks( modelized );
_this.runPostQueryHooks( modelized );
return modelized;
}.bind( this ), null, 'sl-ember-store:save - then' )
}, null, 'sl-ember-store:save - then' )

.catch( function ajaxAdapterSaveCatch( jqxhr ) {
var errorData = {
Expand All @@ -153,7 +156,7 @@ export default Adapter.extend({

return errorData;

}.bind( this ), 'sl-ember-store:save - catch' );
}, 'sl-ember-store:save - catch' );

return promise;
}
Expand Down
47 changes: 25 additions & 22 deletions addon/adapters/localstorage.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ var LocalStorageAdapter = Adapter.extend({
find: function ( type, id, options, findOne ) {
var store = this.get( 'store' ),
model = store.modelFor( type ),
_this = this,
url,
results,
promise,
Expand Down Expand Up @@ -56,9 +57,9 @@ var LocalStorageAdapter = Adapter.extend({
response,
finalResult;

db = this._getDb();
db = _this._getDb();

records = this._getRecords( db, url );
records = _this._getRecords( db, url );

if ( options.data && options.data.id ) {
response = records.findBy( 'id', options.data.id );
Expand All @@ -79,7 +80,7 @@ var LocalStorageAdapter = Adapter.extend({

response = model.callSerializerForEndpointAction( options.endpoint, 'get', response, store );

response = this.modelize( response );
response = _this.modelize( response );

if ( results instanceof Ember.ArrayProxy ) {
finalResult = [];
Expand All @@ -92,12 +93,12 @@ var LocalStorageAdapter = Adapter.extend({

resolve( finalResult );

}.bind( this ), 'sl-ember-store.localstorageAdapter:find - Promise' )
}, 'sl-ember-store.localstorageAdapter:find - Promise' )

.then( function lsAdapterFindThen( response ) {
this.runPostQueryHooks( response );
_this.runPostQueryHooks( response );
return response;
}.bind( this ), 'sl-ember-store.localstorageAdapter:find - then' );
}, 'sl-ember-store.localstorageAdapter:find - then' );

//set the promise on the promiseProxy
results.set( 'promise', promise );
Expand All @@ -116,7 +117,8 @@ var LocalStorageAdapter = Adapter.extend({
* @returns {Ember.RSVP} Promise
*/
deleteRecord: function( url, id ) {
var promise;
var _this = this,
promise;

Ember.assert( 'A url is required to delete a model', url );

Expand All @@ -126,11 +128,11 @@ var LocalStorageAdapter = Adapter.extend({
recordIndex,
exception = {};

db = this._getDb();
db = _this._getDb();

records = this._getRecords( db, url );
records = _this._getRecords( db, url );

recordIndex = this._getRecordIndexById( records, id );
recordIndex = _this._getRecordIndexById( records, id );

if ( recordIndex >= 0 ) {
records.splice( recordIndex, 1 );
Expand All @@ -139,18 +141,18 @@ var LocalStorageAdapter = Adapter.extend({
reject( { textStatus: 'error', errorThrown: 'Not Found' } );
}

if ( !this._dbWrite( db, exception ) ) {
if ( !_this._dbWrite( db, exception ) ) {
reject( { textStatus: 'error', errorThrown: exception.msg } );
}

resolve();

}.bind( this ))
})

.then( function lsAdapterDeleteFinally( response ) {
this.runPostQueryHooks( response );
_this.runPostQueryHooks( response );
return response;
}.bind( this ) , 'sl-ember-store.localstorageAdapter:deleteRecord - always' );
}, 'sl-ember-store.localstorageAdapter:deleteRecord - always' );

return promise;
},
Expand All @@ -164,7 +166,8 @@ var LocalStorageAdapter = Adapter.extend({
* @returns {Ember.RSVP} Promise
*/
save: function( url, content ) {
var promise;
var _this = this,
promise;

Ember.assert( 'A url is required to save a model', url );

Expand All @@ -174,29 +177,29 @@ var LocalStorageAdapter = Adapter.extend({
recordIndex,
exception = {};

db = this._getDb();
db = _this._getDb();

records = this._getRecords( db, url );
records = _this._getRecords( db, url );

recordIndex = this._getRecordIndexById( records, content.id );
recordIndex = _this._getRecordIndexById( records, content.id );

if ( recordIndex >= 0 ) {
records.splice( recordIndex, 1 );
}

records.push( content );

if( ! this._dbWrite( db, exception ) ) {
if( ! _this._dbWrite( db, exception ) ) {
reject( { textStatus: 'error', errorThrown: exception.msg } );
}

resolve( content );

}.bind( this ))
})
.then( function lsAdapterSaveFinally( response ) {
this.runPostQueryHooks( response );
_this.runPostQueryHooks( response );
return response;
}.bind( this ) , 'sl-ember-store.localstorageAdapter:saveRecord - always' );
} , 'sl-ember-store.localstorageAdapter:saveRecord - always' );

return promise;
},
Expand Down
36 changes: 22 additions & 14 deletions addon/cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -203,15 +203,17 @@ export default Ember.Object.extend({
* @returns {Ember.Object} ObjectProxy or PromiseProxyMixin
*/
addPromise: function( type, id, promise ) {
var _this = this;

this._getPromises( type ).set( 'ids.' + id, promise );

promise.then( function( record ) {
this.addRecord( type, record );
delete this._getPromises( type ).get( 'ids' )[ id ];
}.bind( this ) )
_this.addRecord( type, record );
delete _this._getPromises( type ).get( 'ids' )[ id ];
})
.catch( function() {
delete this._getPromises( type ).get( 'ids' )[ id ];
}.bind( this ) );
delete _this._getPromises( type ).get( 'ids' )[ id ];
});

return promise;
},
Expand All @@ -225,15 +227,17 @@ export default Ember.Object.extend({
* @returns {Ember.Array} ArrayProxy or PromiseProxyMixin
*/
addManyPromise: function( type, promise ) {
var _this = this;

this._getPromises( type ).get( 'many' ).addObject( promise );

promise.then( function( records ) {
this.addManyRecords( type, records );
this._getPromises( type ).get( 'many' ).removeObject( promise );
}.bind(this))
_this.addManyRecords( type, records );
_this._getPromises( type ).get( 'many' ).removeObject( promise );
})
.catch( function() {
this._getPromises( type ).get( 'many' ).removeObject( promise );
}.bind(this));
_this._getPromises( type ).get( 'many' ).removeObject( promise );
});

return promise;
},
Expand Down Expand Up @@ -268,9 +272,11 @@ export default Ember.Object.extend({
* @returns {void}
*/
addRecords: function( type, records ) {
var _this = this;

records.forEach( function( record ) {
this.addRecord( type, record );
}.bind( this ) );
_this.addRecord( type, record );
});
},

/**
Expand Down Expand Up @@ -314,9 +320,11 @@ export default Ember.Object.extend({
* @returns {void}
*/
removeRecords: function( type, records ) {
var _this = this;

records.map( function( record ) {
this.removeRecord( type, record );
}.bind( this ) );
_this.removeRecord( type, record );
});
},

/**
Expand Down
8 changes: 4 additions & 4 deletions addon/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ var Model = Ember.ObjectProxy.extend({
Ember.assert( 'Endpoint must be configured on ' + this.toString() + ' before calling save.', endpoint );

return this.container.lookup( 'adapter:' + this.constructor.adapter ).save( endpoint, data )
.then( function( response ) {
.then( Ember.run.bind( this, function( response ) {
this.set( 'content', response );
return this;
}.bind( this ), null, 'sl-ember-store.model:save' );
}), null, 'sl-ember-store.model:save' );
},

/**
Expand All @@ -49,9 +49,9 @@ var Model = Ember.ObjectProxy.extend({
Ember.assert( 'Enpoint must be configured on ' + this.toString() + ' before calling deleteRecord.', endpoint );

return this.container.lookup( 'adapter:'+this.constructor.adapter ).deleteRecord( endpoint, this.get( 'id' ) )
.then( function() {
.then( Ember.run.bind( this, function() {
Ember.run( this, 'destroy' );
}.bind( this ), null, 'sl-ember-store.model:deleteRecord' );
}), null, 'sl-ember-store.model:deleteRecord' );
}
});

Expand Down
3 changes: 1 addition & 2 deletions bower.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@
"ember-qunit": "0.2.8",
"ember-qunit-notifications": "0.0.7",
"qunit": "~1.17.1",
"pretender": "0.1.0",
"es5-shim": "^4.0.5"
"pretender": "0.1.0"
},
"devDependencies": {
"sinonjs": "~1.10.2",
Expand Down
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
"ember-cli-app-version": "0.3.1",
"ember-cli-babel": "^4.0.0",
"ember-cli-dependency-checker": "0.0.7",
"ember-cli-es5-shim": "~0.1.0",
"ember-cli-ic-ajax": "0.1.1",
"ember-cli-inject-live-reload": "^1.3.0",
"ember-cli-pretender": "^0.3.1",
Expand Down

0 comments on commit 23fdf1e

Please sign in to comment.