Skip to content

Commit f7b32b6

Browse files
committed
Merge pull request #166 from leeyeh/fix/_handleSaveResult
[fix] 某些登录 API 没有更新 currentUser 的问题
2 parents 7efb8e3 + 7d9da38 commit f7b32b6

File tree

2 files changed

+37
-41
lines changed

2 files changed

+37
-41
lines changed

lib/user.js

Lines changed: 34 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -126,17 +126,12 @@ module.exports = function(AV) {
126126
var authData = this.get('authData') || {};
127127
authData[authType] = options.authData;
128128
this.set('authData', authData);
129-
130-
// Overridden so that the user can be made the current user.
131-
var newOptions = _.clone(options) || {};
132-
newOptions.success = function(model) {
133-
model._handleSaveResult(true).then(function() {
134-
if (options.success) {
135-
options.success.apply(this, arguments);
136-
}
137-
});
138-
};
139-
return this.save({'authData': authData}, newOptions);
129+
return this.save({'authData': authData}, filterOutCallbacks(options))
130+
.then(function(model) {
131+
return model._handleSaveResult(true).then(function() {
132+
return model;
133+
});
134+
})._thenRunCallbacks(options);
140135
} else {
141136
var self = this;
142137
var promise = new AV.Promise();
@@ -270,7 +265,7 @@ module.exports = function(AV) {
270265
return AV.Promise.error(error);
271266
}
272267

273-
return this.save(attrs).then(function(model) {
268+
return this.save(attrs, filterOutCallbacks(options)).then(function(model) {
274269
return model._handleSaveResult(true).then(function() {
275270
return model;
276271
});
@@ -324,21 +319,17 @@ module.exports = function(AV) {
324319
return AV.Promise.error(error);
325320
}
326321

327-
// Overridden so that the user can be made the current user.
328-
var newOptions = _.clone(options);
322+
var newOptions = filterOutCallbacks(options);
329323
newOptions._makeRequest = function(route, className, id, method, json) {
330324
return AV._request('usersByMobilePhone', null, null, "POST", json);
331325
};
332-
newOptions.success = function(model) {
333-
model._handleSaveResult(true).then(function() {
334-
delete model.attributes.smsCode;
335-
delete model._serverData.smsCode;
336-
if (options.success) {
337-
options.success.apply(this, arguments);
338-
}
326+
return this.save(attrs, newOptions).then(function(model) {
327+
delete model.attributes.smsCode;
328+
delete model._serverData.smsCode;
329+
return model._handleSaveResult(true).then(function() {
330+
return model;
339331
});
340-
};
341-
return this.save(attrs, newOptions);
332+
})._thenRunCallbacks(options);
342333
},
343334

344335
/**
@@ -384,15 +375,13 @@ module.exports = function(AV) {
384375
}
385376
options = options || {};
386377

387-
var newOptions = _.clone(options);
388-
newOptions.success = function(model) {
389-
model._handleSaveResult(false).then(function() {
390-
if (options.success) {
391-
options.success.apply(this, arguments);
392-
}
393-
});
394-
};
395-
return AV.Object.prototype.save.call(this, attrs, newOptions);
378+
return AV.Object.prototype.save
379+
.call(this, attrs, filterOutCallbacks(options))
380+
.then(function(model) {
381+
return model._handleSaveResult(false).then(function() {
382+
return model;
383+
});
384+
})._thenRunCallbacks(options);
396385
},
397386

398387
/**
@@ -465,15 +454,12 @@ module.exports = function(AV) {
465454
* @see AV.Object#fetch
466455
*/
467456
fetch: function(options) {
468-
var newOptions = options ? _.clone(options) : {};
469-
newOptions.success = function(model) {
470-
model._handleSaveResult(false).then(function() {
471-
if (options && options.success) {
472-
options.success.apply(this, arguments);
473-
}
474-
});
475-
};
476-
return AV.Object.prototype.fetch.call(this, newOptions);
457+
return AV.Object.prototype.fetch.call(this, filterOutCallbacks(options))
458+
.then(function(model) {
459+
return model._handleSaveResult(false).then(function() {
460+
return model;
461+
});
462+
})._thenRunCallbacks(options);
477463
},
478464

479465
/**
@@ -1050,3 +1036,10 @@ module.exports = function(AV) {
10501036

10511037
});
10521038
};
1039+
1040+
function filterOutCallbacks(options) {
1041+
var newOptions = _.clone(options) || {};
1042+
delete newOptions.success;
1043+
delete newOptions.error;
1044+
return newOptions;
1045+
}

test/user.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ var email="u" + Date.now() + "@test.com";
33
var password="password1";
44
describe("User",function(){
55

6+
describe("User.signUp", function(){
67
it("should sign up",function(done){
78
var user = new AV.User();
89
user.set("username", username);
@@ -214,3 +215,5 @@ describe("Follow/unfollow users",function(){
214215
});
215216
});
216217
});
218+
219+
});

0 commit comments

Comments
 (0)