Using $rootScope for the Ionic list items array?


#1

hi, i was using $scope to declare and fill the arrays that will be used in the ionic list in my apps, i have multiple controllers and each one have its own $scope array that are connected to the required ionic list, i was thinking to centralize my update process , so i tried to use $rootScope rather than $scope and update all the controllers arrays in one controller ( the NavCtrl that is used for the side menu ) . u can see a simple code showing my new approach , so i want to ask is this way ok ? will this way improve performance ? or its not ok to use $rootScope in this way ?? i want to use this way because i want to do a sync with remote DB, so i thought to declare a $interval to a function that check for updates and push the update to the required array.

the codepen for this code


#2

I load all my app’s data from a global javascript object, and it works like a charm.

But I still use a service to retrieve it. You can start here. Services (and factories) are the correct Angular way to interact with data, because they’re singletons.

In a way, services and factories force you to write mini CRUD APIs, always handy, and make your controller much more readable. Also think separation of concerns. Your controller should only be here to pass the data to the view, and if required, update the data from the view (through API-like calls) to your service.

I would be careful not to fill $rootScope too much, I think you should use it mainly to store critical variables that have to pass through all the app, using $emit and $broadcast. But there is nothing that prevents you of storing kB of data into $rootScope. I’m not sure what the consequences would be, memory-wise.

You may use only one controller, and use the URL ($state.params) to identify what kind of business object you have to retrieve, in the controller. Then, you make a call to the service, from the controller. Using promises is good advice.
Once you retrieved your data :

  var productFamily = $state.params.productFamily; // 'books','mp3','blu-rays'... OR COULD BE AN ID.

  $scope.products = myBigFatFactory.getAllProducts(productFamily); 

collection-repeat is ready to fetch stuff in your ionic list.

You can still use $interval, and periodically query your service from your controller.


#3

thanks @bonatoque for your reply, am already using teh factory and services in my app, but i was calling the services and factories in every controller to fetch the updates from the remote Mysql server and store updates in Sqlite database and then push the new data to the $scope array that are linked to the list, what am thinking to do now is to put the overall fetch and update process ( using services and factories ) in one controller that fetch the updates from the remote Mysql server and store updates in Sqlite and then push the updated data ro $rootScope arraies that are linked to multiple views ionic lists, that the overall method, and keep this in a $interval like every 5 seconds to get some fast updates … so i will fill some Kb in every $rootScope array… so do u think this method is better than the old one ??


#4

I think this Stackoverflow’s answer covers your issue : http://stackoverflow.com/questions/17006054/angularjs-globalctrl-vs-rootscope-vs-service