From 07325b8ed05ae9069d8296cb6c0f92521adc7f38 Mon Sep 17 00:00:00 2001 From: Tankred Hase Date: Mon, 10 Jun 2013 20:10:39 +0200 Subject: [PATCH] refactored cloudstorage dao --- src/js/dao/cloudstorage-dao.js | 62 +++++++++++++++++++--------------- 1 file changed, 34 insertions(+), 28 deletions(-) diff --git a/src/js/dao/cloudstorage-dao.js b/src/js/dao/cloudstorage-dao.js index 3d0ccda..c22c797 100644 --- a/src/js/dao/cloudstorage-dao.js +++ b/src/js/dao/cloudstorage-dao.js @@ -2,9 +2,11 @@ * High level storage api for handling syncing of data to * and from the cloud. */ -app.dao.CloudStorage = function(window, $) { +define(['jquery'], function($) { 'use strict'; + var self = {}; + // // Generic Ajax helper functions // @@ -12,7 +14,7 @@ app.dao.CloudStorage = function(window, $) { /** * GET (read) request */ - this.get = function(uri, callback) { + self.get = function(uri, callback) { $.ajax({ url: uri, type: 'GET', @@ -22,7 +24,8 @@ app.dao.CloudStorage = function(window, $) { }, error: function(xhr, textStatus, err) { callback({ - errMsg: xhr.status + ': ' + xhr.statusText + errMsg: xhr.status + ': ' + xhr.statusText, + err: err }); } }); @@ -31,7 +34,7 @@ app.dao.CloudStorage = function(window, $) { /** * PUT (update) request */ - this.put = function(item, uri, callback) { + self.put = function(item, uri, callback) { $.ajax({ url: uri, type: 'PUT', @@ -42,7 +45,8 @@ app.dao.CloudStorage = function(window, $) { }, error: function(xhr, textStatus, err) { callback({ - errMsg: xhr.status + ': ' + xhr.statusText + errMsg: xhr.status + ': ' + xhr.statusText, + err: err }); } }); @@ -51,7 +55,7 @@ app.dao.CloudStorage = function(window, $) { /** * DELETE (remove) request */ - this.remove = function(uri, callback) { + self.remove = function(uri, callback) { $.ajax({ url: uri, type: 'DELETE', @@ -60,7 +64,8 @@ app.dao.CloudStorage = function(window, $) { }, error: function(xhr, textStatus, err) { callback({ - errMsg: xhr.status + ': ' + xhr.statusText + errMsg: xhr.status + ': ' + xhr.statusText, + err: err }); } }); @@ -74,18 +79,18 @@ app.dao.CloudStorage = function(window, $) { * Pushes an encrypted item to the user's cloud storage * @param type [String] The type of item e.g. 'email' */ - this.putEncryptedItem = function(item, type, emailAddress, folderName, callback) { + self.putEncryptedItem = function(item, type, emailAddress, folderName, callback) { var uri = app.config.cloudUrl + '/' + type + '/user/' + emailAddress + '/folder/' + folderName + '/' + item.id; - this.put(item, uri, callback); + self.put(item, uri, callback); }; /** * Delete an encrypted item from the cloud * @param type [String] The type of item e.g. 'email' */ - this.deleteEncryptedItem = function(id, type, emailAddress, folderName, callback) { + self.deleteEncryptedItem = function(id, type, emailAddress, folderName, callback) { var uri = app.config.cloudUrl + '/' + type + '/user/' + emailAddress + '/folder/' + folderName + '/' + id; - this.remove(uri, callback); + self.remove(uri, callback); }; /** @@ -94,9 +99,9 @@ app.dao.CloudStorage = function(window, $) { * @param offset [Number] The offset of items to fetch (0 is the last stored item) * @param num [Number] The number of items to fetch (null means fetch all) */ - this.listEncryptedItems = function(type, emailAddress, folderName, callback) { + self.listEncryptedItems = function(type, emailAddress, folderName, callback) { var uri = app.config.cloudUrl + '/' + type + '/user/' + emailAddress + '/folder/' + folderName; - this.get(uri, callback); + self.get(uri, callback); }; // @@ -106,10 +111,10 @@ app.dao.CloudStorage = function(window, $) { /** * Find the user's corresponding public key */ - this.getPublicKey = function(keyId, callback) { + self.getPublicKey = function(keyId, callback) { var uri = app.config.cloudUrl + '/publickey/key/' + keyId; - this.get(uri, function(err, key) { + self.get(uri, function(err, key) { if (err) { callback(err); return; @@ -129,10 +134,10 @@ app.dao.CloudStorage = function(window, $) { /** * Find the user's corresponding public key by email */ - this.getPublicKeyByUserId = function(userId, callback) { + self.getPublicKeyByUserId = function(userId, callback) { var uri = app.config.cloudUrl + '/publickey/user/' + userId; - this.get(uri, function(err, keys) { + self.get(uri, function(err, keys) { if (err) { callback(err); return; @@ -159,17 +164,17 @@ app.dao.CloudStorage = function(window, $) { /** * Persist the user's publc key */ - this.putPublicKey = function(pubkey, callback) { + self.putPublicKey = function(pubkey, callback) { var uri = app.config.cloudUrl + '/publickey/user/' + pubkey.userId + '/key/' + pubkey._id; - this.put(pubkey, uri, callback); + self.put(pubkey, uri, callback); }; /** * Delete the public key from the cloud storage service */ - this.removePublicKey = function(keyId, callback) { + self.removePublicKey = function(keyId, callback) { var uri = app.config.cloudUrl + '/publickey/key/' + keyId; - this.remove(uri, callback); + self.remove(uri, callback); }; // @@ -179,9 +184,9 @@ app.dao.CloudStorage = function(window, $) { /** * Fetch private key by id */ - this.getPrivateKey = function(keyId, callback) { + self.getPrivateKey = function(keyId, callback) { var uri = app.config.cloudUrl + '/privatekey/key/' + keyId; - this.get(uri, function(err, key) { + self.get(uri, function(err, key) { if (err) { callback(err); return; @@ -201,17 +206,18 @@ app.dao.CloudStorage = function(window, $) { /** * Persist encrypted private key to cloud service */ - this.putPrivateKey = function(privkey, callback) { + self.putPrivateKey = function(privkey, callback) { var uri = app.config.cloudUrl + '/privatekey/user/' + privkey.userId + '/key/' + privkey._id; - this.put(privkey, uri, callback); + self.put(privkey, uri, callback); }; /** * Delete the private key from the cloud storage service */ - this.removePrivateKey = function(keyId, callback) { + self.removePrivateKey = function(keyId, callback) { var uri = app.config.cloudUrl + '/privatekey/key/' + keyId; - this.remove(uri, callback); + self.remove(uri, callback); }; -}; \ No newline at end of file + return self; +}); \ No newline at end of file