Skip to content

Commit 5f5670e

Browse files
author
Vlad Balin
committed
Revert "Fixed applyPatch problems"
This reverts commit 318f0fc.
1 parent 318f0fc commit 5f5670e

File tree

8 files changed

+39
-59
lines changed

8 files changed

+39
-59
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: any, patch: object, options?: IOOptions): any;
14+
savePatch(this: IORecord, patch: object, options?: IOOptions): IOPromise<any>;
1515
fetch(options?: IOOptions): IOPromise<any>;
1616
destroy(options?: IOOptions): IOPromise<any>;
1717
};

lib/record/io-mixin.js

Lines changed: 10 additions & 17 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: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -29,22 +29,20 @@ export const IORecordMixin = {
2929
);
3030
},
3131

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

4036
const endpoint = this.getEndpoint();
4137

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

41+
const json = this.toJSON();
42+
4543
return startIO(
4644
this,
47-
endpoint.patch( this.id, patchToJson( patch, this.toJSON() ), options, this ),
45+
endpoint.patch( this.id, patchToJson( patch, json ), options, this ),
4846
options,
4947

5048
update => {
@@ -82,22 +80,18 @@ export const IORecordMixin = {
8280
}
8381
}
8482

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 ){
83+
function patchToJson( patch, json ){
9784
const res = {};
9885

9986
for( let name in patch ){
100-
res[ name ] = deepGet( json, name );
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+
}
10195
}
10296

10397
return res;

tests/typescript/dist/index.js

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2101,15 +2101,12 @@ var IORecordMixin = {
21012101
savePatch: function (patch, options) {
21022102
var _this = this;
21032103
if (options === void 0) { options = {}; }
2104-
this.transaction(function () {
2105-
for (var key in patch) {
2106-
_this.deepSet(key, patch[key]);
2107-
}
2108-
});
2104+
this.set(patch);
21092105
var endpoint = this.getEndpoint();
21102106
if (this.isNew() || !endpoint.patch)
21112107
return this.save();
2112-
return startIO(this, endpoint.patch(this.id, patchToJson(patch, this.toJSON()), options, this), options, function (update) {
2108+
var json = this.toJSON();
2109+
return startIO(this, endpoint.patch(this.id, patchToJson(patch, json), options, this), options, function (update) {
21132110
_this.set(update, __assign({ parse: true }, options));
21142111
});
21152112
},
@@ -2132,20 +2129,16 @@ var IORecordMixin = {
21322129
});
21332130
}
21342131
};
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-
}
21452132
function patchToJson(patch, json) {
21462133
var res = {};
21472134
for (var name_1 in patch) {
2148-
res[name_1] = deepGet(json, name_1);
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+
}
21492142
}
21502143
return res;
21512144
}

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)