Skip to content

Commit 8eadb4f

Browse files
authored
fix(user): reimplement #dissociate with unset op (#526)
also refactor `Object#_applyOpSet` and `Object#set` to support operations like `object.set('a.b', 1)`
1 parent 5c6963e commit 8eadb4f

File tree

4 files changed

+47
-14
lines changed

4 files changed

+47
-14
lines changed

src/object.js

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ const {
77
isNullOrUndefined,
88
ensureArray,
99
transformFetchOptions,
10+
setValue,
11+
findValue,
1012
} = require('./utils');
1113

1214
const RESERVED_KEYS = ['objectId', 'createdAt', 'updatedAt'];
@@ -525,9 +527,10 @@ module.exports = function(AV) {
525527
_applyOpSet: function(opSet, target) {
526528
var self = this;
527529
AV._objectEach(opSet, function(change, key) {
528-
target[key] = change._estimate(target[key], self, key);
529-
if (target[key] === AV.Op._UNSET) {
530-
delete target[key];
530+
const [value, actualTarget, actualKey] = findValue(target, key);
531+
setValue(target, key, change._estimate(value, self, key));
532+
if (actualTarget && actualTarget[actualKey] === AV.Op._UNSET) {
533+
delete actualTarget[actualKey];
531534
}
532535
});
533536
},
@@ -569,9 +572,10 @@ module.exports = function(AV) {
569572
AV._arrayEach(this._opSetQueue, function(opSet) {
570573
var op = opSet[key];
571574
if (op) {
572-
self.attributes[key] = op._estimate(self.attributes[key], self, key);
573-
if (self.attributes[key] === AV.Op._UNSET) {
574-
delete self.attributes[key];
575+
const [value, actualTarget, actualKey] = findValue(self.attributes, key);
576+
setValue(self.attributes, key, op._estimate(value, self, key));
577+
if (actualTarget && actualTarget[actualKey] === AV.Op._UNSET) {
578+
delete actualTarget[actualKey];
575579
} else {
576580
self._resetCacheForKey(key);
577581
}

src/user.js

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -197,13 +197,8 @@ module.exports = function(AV) {
197197
* @since 3.3.0
198198
*/
199199
dissociateAuthData(provider) {
200-
if (_.isString(provider)) {
201-
provider = AV.User._authProviders[provider];
202-
}
203-
return this._linkWith(provider, null).then(model => {
204-
this._synchronizeAuthData(provider);
205-
return model;
206-
});
200+
this.unset(`authData.${provider}`);
201+
return this.save().then(model => model._handleSaveResult(true).then(() => model));
207202
},
208203

209204
/**
@@ -832,7 +827,7 @@ module.exports = function(AV) {
832827
},
833828

834829
associateWithAuthData(userObj, platform, authData) {
835-
console.warn('DEPRECATED: User.associateWithAuthData 已废弃,请使用 User#dissociateAuthData 代替');
830+
console.warn('DEPRECATED: User.associateWithAuthData 已废弃,请使用 User#associateWithAuthData 代替');
836831
return userObj._linkWith(platform, authData);
837832
},
838833
/**

src/utils/index.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,33 @@ function parseDate(iso8601) {
156156
return new Date(Date.UTC(year, month, day, hour, minute, second, milli));
157157
}
158158

159+
const setValue = (target, key, value) => {
160+
// '.' is not allowed in Class keys, escaping is not in concern now.
161+
const segs = key.split('.');
162+
const lastSeg = segs.pop();
163+
let currentTarget = target;
164+
segs.forEach((seg) => {
165+
if (currentTarget[seg] === undefined) currentTarget[seg] = {};
166+
currentTarget = currentTarget[seg];
167+
});
168+
currentTarget[lastSeg] = value;
169+
return target;
170+
};
171+
172+
const findValue = (target, key) => {
173+
const segs = key.split('.');
174+
const lastSeg = segs.pop();
175+
let currentTarget = target;
176+
for (let i = 0; i < segs.length; i++) {
177+
currentTarget = currentTarget[segs[i]];
178+
if (currentTarget === undefined) {
179+
return [undefined, undefined, lastSeg];
180+
}
181+
}
182+
const value = currentTarget[lastSeg];
183+
return [value, currentTarget, lastSeg];
184+
};
185+
159186
module.exports = {
160187
ajax,
161188
isNullOrUndefined,
@@ -165,4 +192,6 @@ module.exports = {
165192
tap,
166193
inherits,
167194
parseDate,
195+
setValue,
196+
findValue,
168197
};

test/user.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,11 @@ describe("User", function() {
202202
access_token: 'a123123aaabbbbcccc',
203203
expires_in: 1382686496,
204204
});
205+
}).then(user => {
206+
user.get('authData').should.have.property('weixin');
207+
return user.dissociateAuthData('weixin');
208+
}).then(user => {
209+
user.get('authData').should.not.have.property('weixin');
205210
});
206211
});
207212
});

0 commit comments

Comments
 (0)