Skip to content

Commit 83a6065

Browse files
committed
Merge pull request #249 from leeyeh/feat/save-options
implement AV.Object save options
2 parents 505d7af + 54f5131 commit 83a6065

File tree

4 files changed

+63
-10
lines changed

4 files changed

+63
-10
lines changed

src/file.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -323,8 +323,8 @@ module.exports = function(AV) {
323323
* if (fileUploadControl.files.length > 0) {
324324
* var file = fileUploadControl.files[0];
325325
* var name = "photo.jpg";
326-
* var parseFile = new AV.File(name, file);
327-
* parseFile.save().then(function() {
326+
* var file = new AV.File(name, file);
327+
* file.save().then(function() {
328328
* // The file has been saved to AV.
329329
* }, function(error) {
330330
* // The file either could not be read, or could not be saved to AV.

src/object.js

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,9 +129,12 @@ module.exports = function(AV) {
129129
* Set whether to enable fetchWhenSave option when updating object.
130130
* When set true, SDK would fetch the latest object after saving.
131131
* Default is false.
132+
*
133+
* @deprecated use AV.Object#save with options.fetchWhenSave instead
132134
* @param {boolean} enable true to enable fetchWhenSave option.
133135
*/
134136
fetchWhenSave: function(enable){
137+
console.warn('AV.Object#fetchWhenSave is deprecated, use AV.Object#save with options.fetchWhenSave instead.');
135138
if (!_.isBoolean(enable)) {
136139
throw "Expect boolean value for fetchWhenSave";
137140
}
@@ -832,7 +835,9 @@ module.exports = function(AV) {
832835
* }, function(error) {
833836
* // The save failed. Error is an instance of AV.Error.
834837
* });</pre>
835-
*
838+
* @param {Object} options Optional Backbone-like options object to be passed in to set.
839+
* @param {Boolean} options.fetchWhenSave fetch and update object after save succeeded
840+
* @param {AV.Query} options.query Save object only when it matches the query
836841
* @return {AV.Promise} A promise that is fulfilled when the save
837842
* completes.
838843
* @see AV.Error
@@ -918,6 +923,23 @@ module.exports = function(AV) {
918923
json._fetchWhenSave = true;
919924
}
920925

926+
if (options.fetchWhenSave) {
927+
json._fetchWhenSave = true;
928+
}
929+
if (options.query) {
930+
var queryJSON;
931+
if (typeof options.query.toJSON === 'function') {
932+
queryJSON = options.query.toJSON();
933+
if (queryJSON) {
934+
json._where = queryJSON.where;
935+
}
936+
}
937+
if (!json._where) {
938+
var error = new Error('options.query is not an AV.Query');
939+
return AV.Promise.error(error)._thenRunCallbacks(options, model);
940+
}
941+
}
942+
921943
var route = "classes";
922944
var className = model.className;
923945
if (model.className === "_User" && !model.id) {

src/utils.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -378,9 +378,16 @@ const init = (AV) => {
378378
if (objectId) {
379379
apiURL += "/" + objectId;
380380
}
381-
if ((route ==='users' || route === 'classes') && dataObject && dataObject._fetchWhenSave){
382-
delete dataObject._fetchWhenSave;
383-
apiURL += '?new=true';
381+
if ((route ==='users' || route === 'classes') && dataObject) {
382+
apiURL += '?';
383+
if (dataObject._fetchWhenSave) {
384+
delete dataObject._fetchWhenSave;
385+
apiURL += '&new=true';
386+
}
387+
if (dataObject._where) {
388+
apiURL += ('&where=' + encodeURIComponent(JSON.stringify(dataObject._where)));
389+
delete dataObject._where;
390+
}
384391
}
385392

386393
dataObject = _.clone(dataObject || {});

test/object.js

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,28 @@ describe('Objects', function(){
119119
}
120120
});
121121
});
122+
it('should not update prop when query not match',function(done){
123+
gameScore.set('score', 10000);
124+
gameScore.save(null, {
125+
query: new AV.Query(GameScore).equalTo('score', -1)
126+
}).then(function(result) {
127+
done(new Error('should not success'));
128+
}, function(error) {
129+
expect(error.code).to.be.eql(305);
130+
done();
131+
});
132+
});
133+
it('should update prop when query match',function(done){
134+
gameScore.set('score', 10000);
135+
gameScore.save(null, {
136+
query: new AV.Query(GameScore).notEqualTo('score', -1),
137+
fetchWhenSave: true
138+
}).then(function(result) {
139+
done();
140+
}, function(error) {
141+
done(error);
142+
});
143+
});
122144
});
123145

124146
describe("Deleting Objects",function(){
@@ -300,9 +322,10 @@ describe('Objects', function(){
300322
person2.set('age', 0);
301323
person2.increment('age',9);
302324
person2.save().then(function(person){
303-
person.fetchWhenSave(true);
304325
person.increment('age', 10);
305-
person.save().then(function(p){
326+
person.save(null, {
327+
fetchWhenSave: true
328+
}).then(function(p){
306329
expect(p.get('age')).to.be(19);
307330
done();
308331
},function(err){
@@ -315,9 +338,10 @@ describe('Objects', function(){
315338

316339
it("should fetch when save when creating new object.", function(done){
317340
var p= new Person();
318-
p.fetchWhenSave(true);
319341
p.set('pname', 'dennis');
320-
p.save().then(function(person) {
342+
p.save(null, {
343+
fetchWhenSave: true
344+
}).then(function(person) {
321345
expect(person.get('company')).to.be('leancloud');
322346
done();
323347
}).catch(function(err) {

0 commit comments

Comments
 (0)