Skip to content

Commit 28636f6

Browse files
committed
feat(record-api): makes $decode throw if encoded value is null or not an object.
Also makes update and create ignore response data if empty.
1 parent e8041b8 commit 28636f6

File tree

3 files changed

+16
-4
lines changed

3 files changed

+16
-4
lines changed

src/module/api/record-api.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,9 @@ RMModule.factory('RMRecordApi', ['RMUtils', function(Utils) {
188188
* @return {RecordApi} this
189189
*/
190190
$decode: function(_raw, _mask) {
191+
192+
Utils.assert(_raw && typeof _raw == 'object', 'Record $decode expected an object');
193+
191194
// IDEA: let user override serializer
192195
this.$type.decode(this, _raw, _mask || Utils.READ_MASK);
193196
if(this.$isNew()) this.$pk = this.$type.inferKey(_raw); // TODO: warn if key changes
@@ -323,8 +326,9 @@ RMModule.factory('RMRecordApi', ['RMUtils', function(Utils) {
323326
.$dispatch('before-update', [request, !!_patch])
324327
.$dispatch('before-save', [request])
325328
.$send(request, function(_response) {
329+
if(_response.data) this.$unwrap(_response.data);
330+
326331
this
327-
.$unwrap(_response.data)
328332
.$dispatch('after-update', [_response, !!_patch])
329333
.$dispatch('after-save', [_response]);
330334
}, function(_response) {
@@ -342,7 +346,7 @@ RMModule.factory('RMRecordApi', ['RMUtils', function(Utils) {
342346
.$dispatch('before-save', [request])
343347
.$dispatch('before-create', [request])
344348
.$send(request, function(_response) {
345-
this.$unwrap(_response.data);
349+
if(_response.data) this.$unwrap(_response.data);
346350

347351
// reveal item (if not yet positioned)
348352
if(this.$scope.$isCollection && this.$position === undefined && !this.$preventReveal) {

test/computed-spec.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ describe('RMBuilderComputed', function() {
4242
}
4343
}
4444
});
45-
device = DeviceModel.$new().$decode();
45+
46+
device = DeviceModel.$new().$decode({});
4647
});
4748

4849
it('calculates using given function', function() {

test/model-spec.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,13 @@ describe('Restmod model class:', function() {
230230
$httpBackend.flush();
231231
});
232232

233+
it('should ignore empty responses', function() {
234+
expect(function() {
235+
bike.$save();
236+
$httpBackend.when('PUT', '/api/bikes/1').respond(200, '');
237+
$httpBackend.flush();
238+
}).not.toThrow();
239+
});
233240
});
234241

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

338345
it('should call the type\'s unpack method', function() {
339-
var spy = jasmine.createSpy();
346+
var spy = jasmine.createSpy().and.returnValue({});
340347
var bike = restmod.model('/api/bikes', {
341348
'Model.unpack': spy
342349
}).$build();

0 commit comments

Comments
 (0)