Skip to content

Commit a4a68b5

Browse files
committed
[feat] Throw when trying to save a fetched file.
1 parent bbcd1c2 commit a4a68b5

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

lib/file.js

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -571,6 +571,9 @@ module.exports = function(AV) {
571571
* @return {AV.Promise} Promise that is resolved when the save finishes.
572572
*/
573573
save: function() {
574+
if (this.id) {
575+
throw new Error('File already saved. If you want to manipulate a file, use AV.Query to get it.');
576+
}
574577
var options = null;
575578
var saveOptions = {};
576579
if(arguments.length === 1) {
@@ -637,6 +640,16 @@ module.exports = function(AV) {
637640
return self._previousSave._thenRunCallbacks(options);
638641
},
639642

643+
/**
644+
* fetch the file from server. If the server's representation of the
645+
* model differs from its current attributes, they will be overriden,
646+
* @param {Object} fetchOptions Optional options to set 'keys' and
647+
* 'include' option.
648+
* @param {Object} options Optional Backbone-like options object to be
649+
* passed in to set.
650+
* @return {AV.Promise} A promise that is fulfilled when the fetch
651+
* completes.
652+
*/
640653
fetch: function() {
641654
var options = null;
642655
var fetchOptions = {};
@@ -650,8 +663,8 @@ module.exports = function(AV) {
650663
var self = this;
651664
var request = AV._request('files', null, this.id, 'GET',
652665
fetchOptions);
653-
return request.then(function(response, status, xhr) {
654-
var value = AV.Object.prototype.parse(response, status, xhr);
666+
return request.then(function(response) {
667+
var value = AV.Object.prototype.parse(response);
655668
value._metaData = value.metaData || {};
656669
value._url = value.url;
657670
value._name = value.name;

test/file.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,12 @@ describe("files", function() {
178178
expect(file).to.be.a(AV.File);
179179
expect(file.id).to.be(fileId);
180180
});
181+
it('save a fetched file should throw', function(){
182+
var file = AV.File.createWithoutData(fileId);
183+
expect(function saveFetchedFile(){
184+
file.save();
185+
}).to.throwError(/File already saved\./);
186+
});
181187
it('fetch() should retrieve all data', function(done){
182188
var file = AV.File.createWithoutData(fileId);
183189
file.fetch().then(function(file){

0 commit comments

Comments
 (0)