Title

Wednesday, 4 February 2015

Angular.js Yeoman app.js file not running custom function


I have a angular.js installation from yeoman generator. For express.js, I was under the impression that when the node.js server starts(with grunt serve), the app.js file is used/read to run functions. However, in my app/scripts/app.js my function rest.runApi(); is not running. Why is this? How can I make the function run as it is supposed to in app.js? There are no errors on output when server starts:

one@demo ~/node_stuff $ cat app/scripts/app.js

'use strict';    var rest = require('./memory.js');    rest.runApi();    var myappApp = angular   .module('myappApp', [   'ngCookies', 'duScroll', 'ngRoute'   ]).config(function($routeProvider) {   $routeProvider.when('/home', {   templateUrl: 'views/home.html',   controller: 'HomeController'   });     $routeProvider.when('/first', {   templateUrl: 'views/first.html',   controller: 'FirstController'   });         $routeProvider.otherwise({ redirectTo: '/home'});   });    myappApp.controller('Scroller', function($scope, $location, $document, $anchorScroll) {   $scope.scrollTo = function(id) {   var el = angular.element(document.getElementById(id));   $document.scrollTo(el, 2000);   }  });    myappApp.controller('HomeController', function($scope, $location, $anchorScroll) {   $scope.scrollTo = function(id) {   $location.hash(id);   $anchorScroll();   }  });    myappApp.controller('FirstController', function($scope, $location, $anchorScroll) {   $scope.scrollTo = function(id) {   $location.hash(id);   $anchorScroll();   }  });

one@demo ~/node_stuff $ cat app/scripts/memory.js

var rest = require('restler');    CHECK='chMNWXNki7';  URL='https://monitoring.api.rackspacecloud.com/v1.0/1324/';  SLUG='entities/enij5mKIvs/checks/';  MODE='/test';    url = CHECK+URL+SLUG+CHECK+MODE  headers = { headers:   { "X-Auth-Token": "377c" }  }    function runApi() {   rest.post(url, headers).on('complete', function(data, response) {   console.log(JSON.stringify(data, "\n", 2));   console.log("--------------------------------------------------------");   console.log(response.statusCode);   });  }    module.exports.runApi = runApi;  one@demo ~/node_stuff $ 

one@demo ~/node_stuff $ grunt serve

Running "serve" task    Running "clean:server" (clean) task  Cleaning .tmp...OK    Running "bowerInstall:app" (bowerInstall) task    Running "concurrent:server" (concurrent) task     Running "copy:styles" (copy) task   Copied 1 files     Done, without errors.       Execution Time (2014-04-22 01:20:32 UTC)   loading tasks 4ms âââââââââââââ 27%   copy:styles 10ms ââââââââââââââââââââââââââââââââ 67%   Total 15ms    Running "autoprefixer:dist" (autoprefixer) task  Prefixed file ".tmp/styles/main.css" created.    Running "connect:livereload" (connect) task  Started connect web server on 0.0.0.0:9000.    Running "watch" task  Waiting...
Answer

When Angular application performs bootstrapping, it is only running the code within its framework. In order for this to be run at the very beginning of the load/bootstrap process, you can put it inside the run method of the application.

myappApp.run(function apprun() {   var rest = require('./memory.js');   rest.runApi();  });

Other place that you can potentially do this is within a factory. As factories are singletons in Angular, it will also be run during the bootstrap. The difference is that the order of factory initialization is indeterministic, but it is good in case if you want to keep the execution result to be used later.

myApp.factory('RestRunFactory', function factoryrun() {   var rest = require('./memory.js');   rest.runApi();  });

No comments:

Post a Comment