Skip to content

Commit 0dc4009

Browse files
committed
refactor(record): improves behaviour in patch operations for nested properties.
Now listing an object containing property will include all sub properties. Closes #217
1 parent 09b1f35 commit 0dc4009

File tree

3 files changed

+41
-4
lines changed

3 files changed

+41
-4
lines changed

README.md

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -257,10 +257,26 @@ To patch an object, just modify the properties and call `$save` passing an array
257257
bike = Bike.$find(1);
258258
bike.brand = 'Trek';
259259
bike.model = 'Slash';
260-
bike.$save(['brand']);
260+
bike.dim = { width: 10.0, height: 10.0 };
261+
bike.$save(['brand', 'dim']); // will only send brand and dim (every sub property)
261262
```
262263

263-
<!-- it: $httpBackend.expectPATCH('/bikes/1').respond(200, '{}'); $httpBackend.flush(); -->
264+
<!-- it: $httpBackend.expectPATCH('/bikes/1', { brand: 'Trek', dim: { width: 10.0, height: 10.0 } }).respond(200, '{}'); $httpBackend.flush(); -->
265+
<!-- end -->
266+
267+
<!-- section: $save patch nested -->
268+
269+
To specify a single subproperty to be sent in patch, use dot notation:
270+
271+
```javascript
272+
bike = Bike.$find(1);
273+
bike.brand = 'Trek';
274+
bike.model = 'Slash';
275+
bike.dim = { width: 10.0, height: 10.0 };
276+
bike.$save(['dim.height']); // will only send dim.height
277+
```
278+
279+
<!-- it: $httpBackend.expectPATCH('/bikes/1', { dim: { height: 10.0 } }).respond(200, '{}'); $httpBackend.flush(); -->
264280
<!-- end -->
265281

266282
<!-- section: $create on type -->

src/module/api/record-api.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,16 @@ RMModule.factory('RMRecordApi', ['RMUtils', function(Utils) {
292292
method: this.$type.getProperty('patchMethod', 'PATCH'), // allow user to override patch method
293293
url: url,
294294
// Use special mask for patches, mask everything that is not in the patch list.
295-
data: this.$wrap(function(_name) { return _patch.indexOf(_name) === -1; })
295+
data: this.$wrap(function(_name) {
296+
for(var i = 0, l = _patch.length; i < l; i++) {
297+
if(_name === _patch[i] ||
298+
_name.indexOf(_patch[i] + '.') === 0 ||
299+
_patch[i].indexOf(_name + '.') === 0
300+
) { return false; }
301+
}
302+
303+
return true;
304+
})
296305
};
297306
} else {
298307
request = { method: 'PUT', url: url, data: this.$wrap(Utils.UPDATE_MASK) };

test/model-spec.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ describe('Restmod model class:', function() {
163163
var bike;
164164

165165
beforeEach(function() {
166-
bike = Bike.$new(1).$extend({ brand: 'Santa Cruz', model: 'Bronson', wheelSz: 27.5 });
166+
bike = Bike.$new(1).$extend({ brand: 'Santa Cruz', model: 'Bronson', wheelSz: 27.5, dim: { height: 10, width: 10 } });
167167
});
168168

169169
it('should begin a PATCH action', function() {
@@ -185,6 +185,18 @@ describe('Restmod model class:', function() {
185185
$httpBackend.flush();
186186
});
187187

188+
it('should include explicitly specified subproperties', function() {
189+
bike.$save(['brand', 'dim.width']);
190+
$httpBackend.expectPATCH('/api/bikes/1', { brand: 'Santa Cruz', dim: { width: 10 } }).respond(200, {});
191+
$httpBackend.flush();
192+
});
193+
194+
it('should include every sub property in a property if parent name is given', function() {
195+
bike.$save(['brand', 'dim']);
196+
$httpBackend.expectPATCH('/api/bikes/1', { brand: 'Santa Cruz', dim: { height: 10, width: 10 } }).respond(200, {});
197+
$httpBackend.flush();
198+
});
199+
188200
});
189201

190202
it('should call callbacks in proper order when creating', function() {

0 commit comments

Comments
 (0)