user3499271
user3499271

Reputation: 41

Angular: Resetting Factory or Service

I have a factory in angular that is a complex object and looks something like this:

app.factory("mainFcty", function(){
    return {
        a:"",
        b:"",
        c:""
    }
});

As the user to continues the process from the app, they fill out the above form. For example say they are making a booking. They get to the end of the process, add this to a shopping cart and begin the process of filling out the mainFcty again. The problem is when they add the mainFcty to the shopping cart, which is an array I need reset the mainFcty back to it original state of a, b and c all being empty strings. For some reason once I have started the process again the app resets it to the original state, but somewhere in the process the bindings to the old mainFcty re-appear and overwrite all the data I have collected the second time around.

Anyone have any solid ideas of how to reset the bindings and remove all traces of the old one before continuing the process?

Help is gratefully appreciated!

Upvotes: 1

Views: 5555

Answers (2)

Dagan Bog-Computers
Dagan Bog-Computers

Reputation: 608

I have found it more easy to declare the variable at start of the factory, call reset, and then use reset whenever I want to reset the variable.

  .factory('sharedDataUserFlowService', function () {
        var serviceVariables;
        resetData();
        return {
            resetData: resetData,
            triggerFormCheck: triggerFormCheck,

        };

        function resetData() {
             serviceVariables = {
                 triggerFormValidateCheck: false,
                 triggerBlockStageVariable: null,
                 isFormValid: false,
                 blockedStage: null,
                 isProcessEnd: false
             }
        }

        function triggerFormCheck() {
            serviceVariables.triggerFormValidateCheck = !serviceVariables.triggerFormValidateCheck;
        }

can be called from anywhere it is injected with sharedDataUserFlowService.resetData();

good luck.

Upvotes: 0

George Houpis
George Houpis

Reputation: 1729

Create a default values object, and copy the values to the returnable factory object with a method to overwrite the current values with default values.

For example: https://jsfiddle.net/xmtkg5tz/1/

var app = angular.module('myApp',[])
.factory("mainFcty", [function() { 
    var current_data = {}; 
    var default_values = { 
        a:"", 
        b:"I am a default that is not-empty", 
        c:"",
        resetData: function() { 
            console.log( 'Resetting')
            return current_data = angular.extend( current_data, default_values ); 
       } 
    }; 
    default_values.resetData();
    return current_data;
}])
.controller('exampleCtrl',['$scope', 'mainFcty', function($scope,mainFcty) {
    $scope.fcty = mainFcty;
    $scope.submit = function() { 
        console.log( 'reset?', $scope.fcty.a, $scope.fcty.b, $scope.fcty.c ); 
        console.log( 'Before:', mainFcty );
        mainFcty.resetData(); 
        console.log( 'After:', mainFcty ); 
    };
}]);

Upvotes: 2

Related Questions