0

I am creating a mobile app in AngularJS. I call a resource that calls an API to give me values. Everything works fine, but with slow connections or 3G $ scope not cool me, and therefore when browsing the list of items is old.

SERVICES.JS

 .factory('Exercises', function($resource) {

    // localhost: Local
    // 79.148.230.240: server
    return $resource('http://79.148.230.240:3000/wodapp/users/:idUser/exercises/:idExercise', {
        idUser: '55357c898aa778b657adafb4',
        idExercise: '@_id'
    }, {
        update: {
            method: 'PUT'
        }
    });
});

CONTROLLERS

.controller('ExerciseController', function($q, $scope, $state, Exercises) {

         // reload exercises every time  when we enter in the controller
         Exercises.query(function(data) {
             $scope.exercises = data;
         });

         // refresh the list of exercises
         $scope.doRefresh = function() {

             // reload exercises
             Exercises.query().$promise.then(function(data) {
                 $scope.exercises = data;
             }, function(error) {
                 console.log('error');
             });

             // control refresh element 
             $scope.$broadcast('scroll.refreshComplete');
             $scope.$apply();
         }

         // create a new execersie template
         $scope.newExercise = function() {
             $state.go('newExercise');
         };

         // delete a exercise
         $scope.deleteExercise = function(i) {

             // we access to the element using index param
             var exerciseDelete = $scope.exercises[i];

             // delete exercise calling Rest API and later remove to the scope
             exerciseDelete.$delete(function() {
                 $scope.exercises.splice(i, 1);
             });
         };
     })

APP.js

angular.module('wodapp', ['ionic', 'ngResource', 'wodapp.controllers','wodapp.services'])

// Run
.run(function($ionicPlatform) {
  $ionicPlatform.ready(function() {
    // ionic is loaded
  });
})

// Config
.config(function($stateProvider, $urlRouterProvider, $ionicConfigProvider) {
  $stateProvider
    .state('slide', {
      url: '/',
      templateUrl: 'templates/slides.html',
      controller: 'SlideController'
    })
    .state('login', {
      url: '/login',
      templateUrl: 'templates/login.html',
      controller: 'LoginController'
    })
    .state('dashboard', {
      url: '/dashboard',
      templateUrl: 'templates/dashboard.html',
      controller: 'DashboardController'
    })
    .state('exercise', {
      url: '/exercise',
      templateUrl: 'templates/exercises.html',
      controller: 'ExerciseController'
    })
    .state('newExercise',{
      url: '/newExercise',
      templateUrl: 'templates/newExercise.html',
      controller: 'NewExerciseController'
    });
  $urlRouterProvider.otherwise('/');
});
Tomislav Stankovic
  • 3,080
  • 17
  • 35
  • 42
  • Your error is not clear to me : but with slow connections or 3G $ scope not cool me and therefore when browsing the list of items is old. Where is the query function of your Factory :Exercises.query ?? – aorfevre May 18 '15 at 16:28
  • the query function is called automatically when using angular Resources – user1686786 May 18 '15 at 16:47

1 Answers1

0

If you want to reload a part of your controller logic, every time the view is activated:

.controller('ExerciseController', function(
  $q, 
  $scope, 
  $state, 
  Exercises,
  $ionicView
) {
   // reload exercises every time  when we enter in the controller
   $ionicView.enter(function(){
     // This gets executed regardless of ionicCache
     Exercises.query(function(data) {
       $scope.exercises = data;
     });;
   });
});

Else, you can use the reload option on .state()

InfinitePrime
  • 1,718
  • 15
  • 18