yeoman - Integrating the MobileServiceClient with AngularJS -
i'm trying use windowsazure.mobileserviceclient within angular single sign on , crud operations. being angular noob, i'm trying figure out best way this:
- instantiate in $rootscope in .run , call functions there?
- create service or factory , make instantiation of mobileserviceclient , of function calls in that? currentuser , other information lost when service/factory isn't being used?
- just spool mobileserviceclient in controllers need it? seems me if way, currentuser info lost?
i've tried using of above methods i'm running problems:
- calling login method shown in azure docs works, other times doesn't show popup window authentication provider should. logged off of authentication provider popup window should shown isn't,
- no matter try do, mobileserviceclient currentuser coming null, when popup shown , correctly entered credentials.
any ideas of can make work smoothly? examples can follow somewhere? documentation seems sketchy.
i'm using yeoman , angular generator along grunt work, if makes difference.
i able figure out. created new service handle of mobile services code. since methods client async, i'm using callbacks in methods. use cookie store save user's credential object (currentuser) , pull out again when needed. currentuser seems lose user credential object between calls, store in cookie , push client when has lost it.
'use strict'; angular.module('{appname}') .factory('azuremobileclient', ['$cookiestore', function ($cookiestore) { var azuremobileclient = {}; azuremobileclient.isloggedin = false; azuremobileclient.azureerror = ""; azuremobileclient.azuremsc = new windowsazure.mobileserviceclient('{app url azure}', '{app key azure}'); azuremobileclient.login = function(callback, socialmediaservice) { azuremobileclient.azuremsc.login(socialmediaservice).then(function(user) { azuremobileclient.isloggedin = user != null; $cookiestore.put("azureuser", user); callback(azuremobileclient.isloggedin); } , function(error){ alert(error); azuremobileclient.azureerror = error; }); }; azuremobileclient.logout = function() { azuremobileclient.getuser(); azuremobileclient.azuremsc.logout(); $cookiestore.remove("azureuser"); }; azuremobileclient.getstuff = function(callback) { azuremobileclient.getuser(); var stufftable = azuremobileclient.azuremsc.gettable("stuff"); stufftable.read().then(function(items) { callback(items); }); }; azuremobileclient.addstuff = function(scope) { azuremobileclient.getuser(); var stufftable = azuremobileclient.azuremsc.gettable("stuff"); stufftable.insert({ stuffname: scope.stuffname }); }; azuremobileclient.getuser = function() { if (azuremobileclient.azuremsc.currentuser === null) { azuremobileclient.azuremsc.currentuser = $cookiestore.get("azureuser"); } }; return azuremobileclient; }]);
in controller login , logout, this:
'use strict'; angular.module('{appname}') .controller('mainctrl', function ($scope, $window, azuremobileclient) { $scope.authenticate = function (socialservice) { azuremobileclient.login(function(isloggedin) { if (isloggedin) { $window.location.href = "/#/play"; } }, socialservice); }; $scope.signout = function() { azuremobileclient.logout(); } });
the view login/logout controller looks this:
<button ng-click="signout()">sign out</button> <div class="span4"> <img src="images/facebooklogin.png" ng-click="authenticate('facebook')" /> <img src="images/twitterlogin.png" ng-click="authenticate('twitter')" /> <img src="images/googlelogin.png" ng-click="authenticate('google')" /> </div>
and in controller gets data, this:
'use strict'; angular.module('{appname}') .controller('stuffctrl', ['$scope', '$window', 'azuremobileclient', function ($scope, $window, azuremobileclient) { azuremobileclient.getstuff(function(items) { if (items.length == 0) { $window.location.href = "/#/stuff/new"; } else { $scope.$apply($scope.items = items); } }); }]);
Comments
Post a Comment