I have a Controller that is calling a function from my Service, and I only want to load the data from the web once, and presumably cache it (in rootScope? but that can wait...).
I've tried to wrap the function call in an init(), but it doesn't seem to be helping... I am seeing the function being called every time I load the partial.
Perhaps there is something naive about my routing ?
Here's some code:
app.js
'use strict';
var tab = angular.module('tab', ['ngRoute', 'tab.controllers', 'tab.services']);
tab.config(['$routeProvider', function($routeProvider) {
$routeProvider.
// a bunch of other logic here
when('/curate', {
templateUrl: '_partials/curate.html',
controller: 'CurateController'
}).
otherwise({
redirectTo: '/splash'
});
}]);
controllers.js
// other controllers etc
.controller('CurateController', function($scope, $routeParams, ParseService) {
// Here's where I'm trying to call the function once.
// Obviously, if I don't have this in the init() it also calls every time I route to it
$scope.init = function () {
ParseService.getArticles(function(results) {
console.log("Articles: " + results);
// do stuff with data here
});
};
})
services.js
angular.module('tab.services', []);
angular.module('tab.services')
/* PARSE */
.factory('ParseService', function() {
// Vars etc
var ParseService = {
name: "Parse",
// The function in question
getArticles : function getArticles(callback) {
var query = new Parse.Query(Article);
query.find({
success : function(results) {
callback(results);
},
error : function(error) {
alert("Error" + error.message);
}
});
},
};
return ParseService;
});
curate.html
<section>
<div class="container-fluid" ng-controller="CurateController" ng-init="init()">
<!-- STUFF -->
</div>
</section>