Skip to content

Instantly share code, notes, and snippets.

@frequent
Created February 11, 2014 15:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save frequent/8937596 to your computer and use it in GitHub Desktop.
Save frequent/8937596 to your computer and use it in GitHub Desktop.
pour cedric
DropboxStorage.prototype.allDocs = function (command, param, options) {
var list_url, result, my_storage;
my_storage = this;
result = [];
list_url = 'https://api.dropbox.com/1/metadata/sandbox/' + "?list=true" +
'&access_token=' + this._access_token;
// Fetch all documents
jIO.util.ajax({
"type": "POST",
"url": list_url
}).then(function (response) {
var data, count, promise_list, item, i, item_id;
data = JSON.parse(response.target.responseText);
count = data.contents.length;
// Loop aver all documents
for (i = 0; i < count; i += 1) {
item = data.contents[i];
// exclude folders
if (!item.is_dir) {
// NOTE: strip '/' leading slashes
item_id = item.path[0] === '/' ? item.path.substr(1) : item.path;
// prepare promise if include_docs is set
if (options.include_docs === true) {
promise_list.push(my_storage._get(item_id));
}
// add document to result list
result.push({
id: item_id,
key: item_id,
value: {}
});
}
}
// NOTE: if promise_list is empty, success is triggered directly
// else it fetch all documents and add them to the result
return RSVP.all(promise_list);
}).then(function (response_list) {
var i, len;
len = response_list.length;
for (i = 0; i < len; i += 1) {
result[i].doc = JSON.parse(response_list[i].target.response);
}
command.success({
"data": {
"rows": result,
"total_rows": result.length
}
});
}).fail(function (error) {
command.error(
"error",
"did not work as expected",
"Unable to call allDocs"
);
});
};
DropboxStorage.prototype.allDocs = function (command, param, options) {
var list_url = '',
result = [],
my_storage = this;
// Too specific, should be less storage dependent
list_url = 'https://api.dropbox.com/1/metadata/sandbox/' + "?list=true"
+ '&access_token=' + this._access_token;
// We get a list of all documents
jIO.util.ajax({
"type": "POST",
"url": list_url
}).then(function (response) {
var data = JSON.parse(response.target.responseText),
count = data.contents.length,
promise_list = [],
item,
i,
item_id;
// We loop aver all documents
for (i = 0; i < count; i += 1) {
item = data.contents[i];
// If the element is a folder it is not included (storage specific)
if (!item.is_dir) {
// Note: the '/' at the begining of the path is stripped
item_id = item.path[0] === '/' ? item.path.substr(1) : item.path;
// Prepare promise_list to fetch document in case of include_docs
if (options.include_docs === true) {
promise_list.push(my_storage._get(item_id));
}
// Document is added to the result list
result.push({
id: item_id,
key: item_id,
value: {}
});
}
}
// Here if promise_list is empty, success is triggered directly
// else it fetch all documents and add them to the result
return RSVP.all(promise_list);
}).then(function (response_list) {
var i;
for (i = 0; i < response_list.length; i += 1) {
result[i].doc = JSON.parse(response_list[i].target.response);
}
command.success({
"data": {
"rows": result,
"total_rows": result.length
}
});
}).fail(function (error) {
command.error(
"error",
"did not work as expected",
"Unable to call allDocs"
);
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment