AngularJS - When does a service retrieve data? -


when service / factory retrieve data http request?

when factory created in service, curious when http request sent, , how processed after app running time.

i'm writing code using ionic framework. once initialize app, , stays open day or two, json data refreshed @ interval? or refresh data once app closed, , opened once again?

my requirement http request, is updated every day @ 00:01 am.

i suppose general question is: how http request fetch data? , how service work in angularjs.

here code retrieve json package:

angular.module('starter.services', [])  .factory('menujson', function ($http) {     return {      : function() {        return $http({          url: 'http://middmenuapi.herokuapp.com/',          method: 'get'        })      }    } }); 

calls $http() (or of aliases, such $http.get() etc) invoke web request (barring manipulation inspectors or third-party components). analogous issuing xmlhttprequest or jsonp request in other frameworks, jquery.

services created singletons, created once when first requested, , same instance injected point on. service entirely you, angular deals instantiating it, resolving of dependencies, , injecting anywhere it's requested.

if have application running long periods of time , needs updated data, you'll need architect appropriately. don't know full requirements or particulars of spec, can give hints maybe going.

high level, sounds need function operate on timer (using $timeout service), , if meet or exceed time window, invoke $http request retrieve latest data , route along various components need it. then, should mark time frame should next operate, set timeout again can wake further down road , see if it's time work again.

the first thing think should functionality live? if need happen in controller, can there in controller using $timeout , $http. on other hand, if need reuse data in multiple places, you'll want use service. if use service, likely, need figure out best way changes various parts of app need it.

my recommendation use angular events on $rootscope $broadcast service when $http request has updated data. then, various controllers, services, , directives consume data can subscribe event using $scope.$on , react appropriately. keeps service decoupled things use , allow them react changes easily.

all service set timeout, when lapses check data, if has data, broadcast data in event on $rootscope, , set timeout. clients listens , updates local scope new data when receives event service.

this plunk contains silly example. you'd want change schedule work @ time of day or whatever see fit, , have make $http request rather send current date.

angular.module("demo", [])   .service('myservice', ['$rootscope', '$timeout', '$http', function($rootscope, $timeout, $http) {       var state = { timeout: null, next: null };        function work() {         var = date.now();          if (now >= state.next) { // can replace own logic schedule when should occur (like specific time of day). in example, poll every second, work every 5.            // $http service can work, $http.get(...).success(function(data) { $rootscope.$broadcast('myservice.data', data); });           $rootscope.$broadcast('myservice.data', new date());           state.next = + 5000; // work every 5 seconds         }          state.timeout = $timeout(work, 1000); // poll every second       }        return {         start: function() {           if (state.timeout) $timeout.cancel(state.timeout); // cancel pending timeout           work(); // first time schedule work done in future         },         stop: function() {           if (state.timeout) $timeout.cancel(state.timeout); // cancel pending timeout         }       };     }])   .controller('democtrl', ['$scope', function($scope) {       $scope.title = "hello, world";        // here, controller subscribes event, , when occurs, copies event data local scope item       $scope.$on('myservice.data', function(evt, data) {         $scope.$apply(function() {           $scope.myservicedata = data;         });       });     }])     .run(['myservice', function(myservice) {       myservice.start(); // starts service when app runs     }]); 

Comments

Popular posts from this blog

Android layout hidden on keyboard show -

google app engine - 403 Forbidden POST - Flask WTForms -

c - Why would PK11_GenerateRandom() return an error -8023? -