Skip to content

Commit 318f0fc

Browse files
author
Vlad Balin
committed
Fixed applyPatch problems
1 parent 97785f1 commit 318f0fc

File tree

8 files changed

+59
-39
lines changed

8 files changed

+59
-39
lines changed

dist/index.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/index.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/record/io-mixin.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export interface IORecord extends IONode {
1111
}
1212
export declare const IORecordMixin: {
1313
save(this: IORecord, options?: IOOptions): IOPromise<any>;
14-
savePatch(this: IORecord, patch: object, options?: IOOptions): IOPromise<any>;
14+
savePatch(this: any, patch: object, options?: IOOptions): any;
1515
fetch(options?: IOOptions): IOPromise<any>;
1616
destroy(options?: IOOptions): IOPromise<any>;
1717
};

lib/record/io-mixin.js

Lines changed: 17 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/record/io-mixin.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/record/io-mixin.ts

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -29,20 +29,22 @@ export const IORecordMixin = {
2929
);
3030
},
3131

32-
savePatch( this : IORecord, patch : object, options : IOOptions = {} ){
32+
savePatch( this : any, patch : object, options : IOOptions = {} ){
3333
// Apply patch to the record...
34-
( this as any ).set( patch );
34+
this.transaction( () => {
35+
for( let key in patch ){
36+
this.deepSet( key, patch[ key ] );
37+
}
38+
});
3539

3640
const endpoint = this.getEndpoint();
3741

3842
// Perform full save for the new models or if patch method is not supported...
3943
if( this.isNew() || !endpoint.patch ) return this.save();
4044

41-
const json = this.toJSON();
42-
4345
return startIO(
4446
this,
45-
endpoint.patch( this.id, patchToJson( patch, json ), options, this ),
47+
endpoint.patch( this.id, patchToJson( patch, this.toJSON() ), options, this ),
4648
options,
4749

4850
update => {
@@ -80,18 +82,22 @@ export const IORecordMixin = {
8082
}
8183
}
8284

83-
function patchToJson( patch, json ){
85+
function deepGet( obj : object, path : string ){
86+
let current = obj;
87+
88+
for( let key of path.split( '.' ) ){
89+
current = current[ key ];
90+
if( !current ) break;
91+
}
92+
93+
return current;
94+
}
95+
96+
function patchToJson( patch : object, json : object ){
8497
const res = {};
8598

8699
for( let name in patch ){
87-
const patchValue = patch[ name ];
88-
89-
if( patchValue && Object.getPrototypeOf( patchValue ) === Object.prototype ){
90-
res[ name ] = patchToJson( patchValue, json[ name ] );
91-
}
92-
else{
93-
res[ name ] = json[ name ];
94-
}
100+
res[ name ] = deepGet( json, name );
95101
}
96102

97103
return res;

tests/typescript/dist/index.js

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2101,12 +2101,15 @@ var IORecordMixin = {
21012101
savePatch: function (patch, options) {
21022102
var _this = this;
21032103
if (options === void 0) { options = {}; }
2104-
this.set(patch);
2104+
this.transaction(function () {
2105+
for (var key in patch) {
2106+
_this.deepSet(key, patch[key]);
2107+
}
2108+
});
21052109
var endpoint = this.getEndpoint();
21062110
if (this.isNew() || !endpoint.patch)
21072111
return this.save();
2108-
var json = this.toJSON();
2109-
return startIO(this, endpoint.patch(this.id, patchToJson(patch, json), options, this), options, function (update) {
2112+
return startIO(this, endpoint.patch(this.id, patchToJson(patch, this.toJSON()), options, this), options, function (update) {
21102113
_this.set(update, __assign({ parse: true }, options));
21112114
});
21122115
},
@@ -2129,16 +2132,20 @@ var IORecordMixin = {
21292132
});
21302133
}
21312134
};
2135+
function deepGet(obj, path) {
2136+
var current = obj;
2137+
for (var _i = 0, _a = path.split('.'); _i < _a.length; _i++) {
2138+
var key = _a[_i];
2139+
current = current[key];
2140+
if (!current)
2141+
break;
2142+
}
2143+
return current;
2144+
}
21322145
function patchToJson(patch, json) {
21332146
var res = {};
21342147
for (var name_1 in patch) {
2135-
var patchValue = patch[name_1];
2136-
if (patchValue && Object.getPrototypeOf(patchValue) === Object.prototype) {
2137-
res[name_1] = patchToJson(patchValue, json[name_1]);
2138-
}
2139-
else {
2140-
res[name_1] = json[name_1];
2141-
}
2148+
res[name_1] = deepGet(json, name_1);
21422149
}
21432150
return res;
21442151
}

tests/typescript/dist/index.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)