From b8167e89c560cdf9215ce92542310cb7a3080942 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jochen=20Eddelb=C3=BCttel?= Date: Sun, 1 Dec 2013 19:26:26 +0100 Subject: [PATCH] Explicit attachMulti() on CouchDoc --- angular-cornercouch.js | 20 +++++++++++++++----- guestbook.html | 13 +++---------- 2 files changed, 18 insertions(+), 15 deletions(-) diff --git a/angular-cornercouch.js b/angular-cornercouch.js index c9e17e3..762a538 100644 --- a/angular-cornercouch.js +++ b/angular-cornercouch.js @@ -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; diff --git a/guestbook.html b/guestbook.html index dda4ec2..719566d 100644 --- a/guestbook.html +++ b/guestbook.html @@ -140,17 +140,10 @@

{{detail.alias}} wrote at {{detail.utc}} UTC

}; $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) {