Skip to content

Commit

Permalink
feat(record-api): makes $decode throw if encoded value is null or not…
Browse files Browse the repository at this point in the history
… an object.

Also makes update and create ignore response data if empty.
  • Loading branch information
iobaixas committed Oct 24, 2015
1 parent e8041b8 commit 28636f6
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 4 deletions.
8 changes: 6 additions & 2 deletions src/module/api/record-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,9 @@ RMModule.factory('RMRecordApi', ['RMUtils', function(Utils) {
* @return {RecordApi} this
*/
$decode: function(_raw, _mask) {

Utils.assert(_raw && typeof _raw == 'object', 'Record $decode expected an object');

// IDEA: let user override serializer
this.$type.decode(this, _raw, _mask || Utils.READ_MASK);
if(this.$isNew()) this.$pk = this.$type.inferKey(_raw); // TODO: warn if key changes
Expand Down Expand Up @@ -323,8 +326,9 @@ RMModule.factory('RMRecordApi', ['RMUtils', function(Utils) {
.$dispatch('before-update', [request, !!_patch])
.$dispatch('before-save', [request])
.$send(request, function(_response) {
if(_response.data) this.$unwrap(_response.data);

this
.$unwrap(_response.data)
.$dispatch('after-update', [_response, !!_patch])
.$dispatch('after-save', [_response]);
}, function(_response) {
Expand All @@ -342,7 +346,7 @@ RMModule.factory('RMRecordApi', ['RMUtils', function(Utils) {
.$dispatch('before-save', [request])
.$dispatch('before-create', [request])
.$send(request, function(_response) {
this.$unwrap(_response.data);
if(_response.data) this.$unwrap(_response.data);

// reveal item (if not yet positioned)
if(this.$scope.$isCollection && this.$position === undefined && !this.$preventReveal) {
Expand Down
3 changes: 2 additions & 1 deletion test/computed-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ describe('RMBuilderComputed', function() {
}
}
});
device = DeviceModel.$new().$decode();

device = DeviceModel.$new().$decode({});
});

it('calculates using given function', function() {
Expand Down
9 changes: 8 additions & 1 deletion test/model-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,13 @@ describe('Restmod model class:', function() {
$httpBackend.flush();
});

it('should ignore empty responses', function() {
expect(function() {
bike.$save();
$httpBackend.when('PUT', '/api/bikes/1').respond(200, '');
$httpBackend.flush();
}).not.toThrow();
});
});

it('should call callbacks in proper order when creating', function() {
Expand Down Expand Up @@ -336,7 +343,7 @@ describe('Restmod model class:', function() {
describe('$unwrap', function() {

it('should call the type\'s unpack method', function() {
var spy = jasmine.createSpy();
var spy = jasmine.createSpy().and.returnValue({});
var bike = restmod.model('/api/bikes', {
'Model.unpack': spy
}).$build();
Expand Down

0 comments on commit 28636f6

Please sign in to comment.