Last active
August 29, 2015 14:10
-
-
Save afitterling/9b1783fcab861a95e111 to your computer and use it in GitHub Desktop.
A simple but safe appStore to be able to resolve in angular-router or ui-router when switching between states/pages
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
// build a global AppStore with Promises | |
angular.module('famousAngular') | |
.factory('AppStore', ['$rootScope', '$q', function ($rootScope, $q) { | |
var self = this; | |
self.appStore = $rootScope.$new(); | |
// promises go here | |
self.appStore.q = {}; | |
// the deferred if called with null obj goes here (pe-initialization, to keep identical promises if we not yet know the data) | |
self.appStore.deferred = {}; | |
return { | |
// we can create promises beforehand to be able to resolve on them at state switching | |
// set latter called with data, the identical promise gets returned | |
set: function (key, data) { | |
var deferred; | |
// if data set null but called with key (initialization) | |
if (!self.appStore.q.hasOwnProperty(key)) { | |
// create the defer call | |
deferred = $q.defer(); | |
// save it to pick it up when called with real data | |
self.appStore.deferred[key] = deferred; | |
// save the deferred promise | |
self.appStore.q[key] = deferred.promise; | |
} else { | |
// the deferred object has been stored before | |
deferred = self.appStore.deferred[key]; | |
} | |
// if data resolve deferred | |
if (data) { | |
deferred.resolve(data); | |
} | |
}, | |
get: function (key) { | |
if (self.appStore.q[key]) { | |
return self.appStore.q[key]; | |
} | |
return null; | |
}, | |
reset: function () { | |
self.appStore = $rootScope.$new(); | |
self.appStore.q = {}; | |
self.appStore.deferred = {}; | |
} | |
}; | |
}]); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
in angular.run or onLogin initialize your AppStore with Objects you're going to fill, but you don't have yet
AppStore.set('items', null)
as of this line you're able to resolve
the above code will resolve when you run your own promises and fill the data there within:
ItemService.get...........then.(function(data){ AppStore.set('items', data) })
on logout call AppStore.reset()