Search code examples
angularjsangularjs-factory

Update ng-class with function later ng-click asyncronous


Update ng-class with function later ng-click asyncronous. I can not get the call to the function $scope.addedProgram refresh the view. Any suggestions?

In the view I have an icon that is updated depending on whether the event is already created on the calendar

// View
<ons-icon icon="ion-bookmark" size="30px" ng-click="program(paper)" ng-class="addedProgram(paper) ? 'active_bookmark' : 'inactive_bookmark'"></ons-icon>

The controller makes two calls to the factory for the ng-click and ng-class

 // Controller
    app.controller('AuthorController', function($scope, ProgramService) {

        // Program function()
        $scope.program = function(paper) {
            ProgramService.saveProgram(paper);
        }

        // Added Program function()
        $scope.addedProgram = function(paper) {
            return ProgramService.addedProgram(paper);
        }

    });

// Factory
app.factory('ProgramService', function($filter, $q) {
    var functions = {};

    functions.findProgramDevice = function(paper) {
        var deferred = $q.defer();

        var startDate = new Date(2015, 3, 7, 18, 30, 0, 0, 0);
        var endDate = new Date(2015, 3, 7, 19, 30, 0, 0, 0);
        var title = "My nice event";
        var eventLocation = "Home";
        var notes = "Some notes about this event.";
        var success = function(message) {
            if(message.length > 0){
                deferred.resolve(true); 
            }else{ 
                deferred.resolve(false); 
            } 
        };
        var error = function(message) { deferred.resolve(false); };

        window.plugins.calendar.findEvent(title, eventLocation, notes, startDate, endDate, success, error);

        return deferred.promise;
    }

    functions.saveProgramDevice = function(paper) {
        var startDate = new Date(2015, 3, 7, 18, 30, 0, 0, 0);
        var endDate = new Date(2015, 3, 7, 19, 30, 0, 0, 0);
        var title = "My nice event";
        var eventLocation = "Home";
        var notes = "Some notes about this event.";
        var success = function(message) { alert("Success 2: " + JSON.stringify(message)); };
        var error = function(message) { alert("Error 2: " + message); };

        functions.findProgramDevice(paper).then(function(result) {
            if(result){
                window.plugins.calendar.deleteEvent(title,eventLocation,notes,startDate,endDate,success,error);
            }else{
                window.plugins.calendar.createEvent(title,eventLocation,notes,startDate,endDate,success,error);
            }
        });
    }

    functions.saveProgram = function(paper) {
        var option = 0;
        functions.findProgramDevice(paper).then(function(result) {
            var program_exist_device = result;

            // Here other code

        });
    }

    functions.addedProgram = function(paper) {
        functions.findProgramDevice(paper).then(function(result) {
            var program_exist_device = result;
            return program_exist_device;
        });
    }
    return functions;
});

Solution

  • You must trigger the function in ng-init and use $scope variable to update the classes.

    <ons-icon ng-init="addedProgram(paper)" icon="ion-bookmark" size="30px" ng-class="updateClass ? 'active_bookmark' : 'inactive_bookmark'">Heya</ons-icon>
    

    In your controller:

    $scope.addedProgram = function(paper) {
         return $timeout(function() {
           $scope.updateClass = true;
         }, 5000);
         // on success of that asyn call update the $scope.updateClass variable.
       }
    

    DEMO