From c9d9f7b47a6101de8f11762a0620401576f5b4ed Mon Sep 17 00:00:00 2001 From: Francis Lachapelle Date: Thu, 27 Aug 2015 15:55:56 -0400 Subject: [PATCH] (js) Add caching of cards --- .../js/Contacts/AddressBook.service.js | 19 ++++++++++- .../js/Contacts/Card.service.js | 32 ++++++++----------- 2 files changed, 31 insertions(+), 20 deletions(-) diff --git a/UI/WebServerResources/js/Contacts/AddressBook.service.js b/UI/WebServerResources/js/Contacts/AddressBook.service.js index d47ff9f4f..3e88988a7 100644 --- a/UI/WebServerResources/js/Contacts/AddressBook.service.js +++ b/UI/WebServerResources/js/Contacts/AddressBook.service.js @@ -471,8 +471,25 @@ * @returns a promise of the HTTP operation */ AddressBook.prototype.$getCard = function(cardId) { + var _this = this; + return this.$id().then(function(addressbookId) { - return AddressBook.$Card.$find(addressbookId, cardId); + var fullCard, + cachedCard = _.find(_this.cards, function(data) { + return cardId == data.id; + }); + + if (cachedCard && cachedCard.$futureCardData) + // Full card is available + return cachedCard; + + fullCard = AddressBook.$Card.$find(addressbookId, cardId); + fullCard.$id().then(function(cardId) { + // Extend the Card object of the addressbook list with the full card description + if (cachedCard) + angular.extend(cachedCard, fullCard); + }); + return fullCard; }); }; diff --git a/UI/WebServerResources/js/Contacts/Card.service.js b/UI/WebServerResources/js/Contacts/Card.service.js index 2254c9b87..7ea90a23d 100644 --- a/UI/WebServerResources/js/Contacts/Card.service.js +++ b/UI/WebServerResources/js/Contacts/Card.service.js @@ -447,26 +447,20 @@ var _this = this; // Expose the promise - this.$futureCardData = futureCardData; - - // Resolve the promise - this.$futureCardData.then(function(data) { - // Calling $timeout will force Angular to refresh the view - return Card.$timeout(function() { - _this.init(data); - // Instanciate Card objects for list members - angular.forEach(_this.refs, function(o, i) { - if (o.email) o.emails = [{value: o.email}]; - o.id = o.reference; - _this.refs[i] = new Card(o); - }); - if (_this.birthday) { - _this.birthday = new Date(_this.birthday * 1000); - } - // Make a copy of the data for an eventual reset - _this.$shadowData = _this.$omit(true); - return _this; + this.$futureCardData = futureCardData.then(function(data) { + _this.init(data); + // Instanciate Card objects for list members + angular.forEach(_this.refs, function(o, i) { + if (o.email) o.emails = [{value: o.email}]; + o.id = o.reference; + _this.refs[i] = new Card(o); }); + if (_this.birthday) { + _this.birthday = new Date(_this.birthday * 1000); + } + // Make a copy of the data for an eventual reset + _this.$shadowData = _this.$omit(true); + return _this; }); };