Code Monkey home page Code Monkey logo

angular-global-resolve's Introduction

What

Resolve common dependencies for routes

Install

$ npm install --save angular-global-resolve

Usage

angular
  .module('app', [
    require('angular-global-resolve')
  ])
angular
  .module('expenses', [])
  .config(routes);


function routes($stateProvider, globalResolveProvider) {
  globalResolveProvider.addGlobalDependenciesTo($stateProvider, {
    user: function (userService) {
      'ngInject';
      return userService.getUser();
    }
  });
}

NOTICE:

You can use either $stateProvider (ui-router) and $routeProvider (ngRoute)

angular-global-resolve's People

Stargazers

Hans Zhang avatar

Watchers

James Cloos avatar Bartosz Chojnacki avatar

angular-global-resolve's Issues

Initialization runs twice

This is almost working perfectly for me. The only problem I'm having is the initialization function runs twice at almost the same time when the app starts.
Here is the relevant code:

    app.config(function($stateProvider, $urlRouterProvider, $locationProvider, globalResolveProvider){
    
      $urlRouterProvider.otherwise('/venues/feed');
    
    
      globalResolveProvider.addGlobalDependenciesTo($stateProvider, {
        getGlobalDependacies: function ($http, $rootScope, $cookies) {
          'ngInject';
          if ($rootScope.apiUrl){
            return
          }
          console.trace('do stuff')
            return $http.get('/__/env.json')
            .then(function(response) {
              $rootScope.apiUrl = response.data.apiUrl;
              $rootScope.googleMapsApiKey = response.data.googleMapsApiKey;
              $rootScope.currentLocationLat = 40.7589;
              $rootScope.currentLocationLng = 73.9851;
            })
            .then(function(){
              hotelId = ''
              if ($cookies.get('hotel') === undefined){
                $http.get($rootScope.apiUrl + '/hotels')
                .then(function(dbHotels){
                  hotelId = dbHotels.data[0]._id
                  $cookies.put('hotelId', hotelId)
                })
              }
              if ($cookies.get('userId') === undefined){
                $http.get($rootScope.apiUrl + '/users')
                .then(function(dbUsers){
                  index = dbUsers.data.length - 1
                  userId = dbUsers.data[index]._id
                  $cookies.put('userId', userId)
                  $rootScope.$broadcast('update-itinerary-icon')
                })
              }
            })
        }
      })
    
      $stateProvider
      .state('venues', {
        url: '/venues',
        abstract: true,
        template:'<div ui-view></div>'
    
      ....more states....
    
      $locationProvider.html5Mode(true);
    });

Any Idea on why this might be running twice?

Docs on npm are out of date

The docs on npm have still have globalResolveProvider.addGlobalDependencies instead of globalResolveProvider.addGlobalDependenciesTo

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.