How to synchronously bootstrap an angularjs app -
how synchronously bootstrap angularjs app
i define couple constant values on app object. of these values need set via call service , these calls need complete before of controllers instantiated.
in example below, define array of values on object named config. need set value named postindexmenuid prior of controllers being instantiated. how do that?
i have tried manually bootstrapping (removing ng-app html). not using routing.
ideally not have learn, download, install, configure, test, , maintain framework accomplish this.
(function() { angular.module('app', []); var app = angular.module('app'); app.controller('controller', ['$scope', 'config', '$http', controller]); app.service('menusservice', ['$http', 'config', menusservice]); // create config object. values set in app.run app.value('config', { siteid: 100, webroot: '', apiroot: '/api', imageroot: '/content/images', postindexmenuid: 0 }); app.run(['$http', 'config','menusservice', function ($http, config, menusservice) { menusservice.getmenuidbyname("postindex", function (data) { config.postindexmenuid = data; // need complete before getposts on controller called }); }]); function controller($scope, config, $http) { var vm = this; vm.posts = 0; function getposts() { // in prod call service here posts based on config.postindexmenuid // example return postindexmenuid. vm.posts = config.postindexmenuid; }; getposts(); // need delay calling until after postindexmenuid set }; function menusservice($http, config) { this.getmenuidbyname = function (menuname, callback) { var uri = config.apiroot + '/menu/getmenubyname?menuname=' + menuname + '&siteid=' + config.siteid; // use timeout simulate slow service example , return dummy value var menuid = 99; settimeout(function () { callback(menuid); }, 2000); }; }; })() // html
<!doctype html> <html xmlns="http://www.w3.org/1999/xhtml" ng-app="app" > <head> <title></title> </head> <body > <div ng-controller = "controller vm"> <p>postindexmenuid {{ vm.posts }}</p> </div> <script src="scripts/jquery-1.8.2.js"></script> <script src="scripts/angular.js"></script> <script src="scripts/angular-route.js"></script> <script src="app/app.js"></script> </body> </html>
there quite nifty trick in angular.js whereby can defer changing of route until promises have been resolved. may have restructure application little bit cater approach, use myself , works treat!
$rootscope.$on('$routechangestart', function $routechangestart(event, next) { // extend resolution of route promise wait user authentication // process determine if there's valid session available. next.resolve = angular.extend( next.resolve || {}, { authenticating: api.issession }); }); by using next.resolve you're extending angular's sequence of promises resolved before route change considered success. in above case, long api.issession returns promise work wonderfully.
as api.issession promise has been resolved, route change succeed, , in case, controller loaded.
Comments
Post a Comment