Skip to content

Commit

Permalink
Explicit attachMulti() on CouchDoc
Browse files Browse the repository at this point in the history
  • Loading branch information
Jochen Eddelbüttel committed Dec 1, 2013
1 parent e7adc16 commit b8167e8
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 15 deletions.
20 changes: 15 additions & 5 deletions angular-cornercouch.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,24 +85,34 @@ factory('cornercouch', ['$http', function($http) {
};

// Requires File-API 'file', sorry IE9
CouchDoc.prototype.attach = function(file, nameOrCallback) {
CouchDoc.prototype.attach = function(file, name, reloadCB) {

var isF = ng.isFunction(nameOrCallback);
var doc = this;
if (ng.isFunction(name)) { reloadCB = name; name = null; }

return $http({
method: "PUT",
url: encodeUri(dbUri, doc._id, !isF && nameOrCallback || file.name),
url: encodeUri(dbUri, doc._id, name || file.name),
params: { rev: doc._rev },
headers: { "Content-Type": file.type },
data: file
})
.success(function () {
// Reload document for local consistency
doc.load().success(isF ? nameOrCallback : ng.noop);
doc.load().success(reloadCB || ng.noop);
});
};


CouchDoc.prototype.attachMulti = function(files, successCB) {
var doc = this;
var idx = 0;
function loopCB() {
if (idx < files.length)
doc.attach(files[idx], ++idx < files.length ? loopCB : successCB);
};
loopCB();
}

CouchDoc.prototype.detach = function(name) {

var doc = this;
Expand Down
13 changes: 3 additions & 10 deletions guestbook.html
Original file line number Diff line number Diff line change
Expand Up @@ -140,17 +140,10 @@ <h4>{{detail.alias}} wrote at {{detail.utc}} UTC</h4>
};

$scope.attachClick = function() {

var fileInput = document.getElementById("upload");
var idx = 0;

function callback() {
if (idx < fileInput.files.length)
$scope.detail.attach(fileInput.files[idx++], callback).error(setError);
else
fileInput.value = "";
};
callback();
$scope.detail.attachMulti(fileInput.files, function () {
fileInput.value = "";
});
};

$scope.detachClick = function(name) {
Expand Down

0 comments on commit b8167e8

Please sign in to comment.