javascriptsingletonpublic-method

javascript singleton public method "not a function"


I give up. I'm usually a c# developer, but I need javascript for this particular project.

I have a list that I want to be protected with some setters and getters (and also public methods vs private helper methods). To do this, I've implemented a singleton pattern following Addy Osmani's Singleton pattern as described in this post: http://robdodson.me/javascript-design-patterns-singleton/

However, when I try to access the public methods, I get the error "publicMethod is not a function".

I have a button hooked up to "addToList" and I just want to print out the message to start with.

Why can't it see my method?

angular
.module('bacnetui')
.controller('bacnetuiController', function($scope, devicesFactory,){

   devicesFactory.getDevices().then(function (response){
     $scope.devices = response.data;
   }, function(error) {
    console.log(error);
   });


   $scope.mySingleton = (function () {
    // Instance stores a reference to the Singleton
    var instance;

    function init() {
      // Singleton
      var list = [];

      // Private methods and variables
      function indexOfDevice(dev){
       ...
     }

     function hasBacnet(dev,obj,prop){
       ....
     }
     function newBacnet(obj,prop){
        ....
     }

      return {

        // Public methods and variables
        publicMethod: function () {
          console.log( "The public can see me!" );
        },

        publicProperty: "I am also public"
      };
    };

    return {
      // Get the Singleton instance if one exists
      // or create one if it doesn't
      getInstance: function () {
        if ( !instance ) {
          instance = init();
        }
        return instance;
      }
    };
  })();


  $scope.addToList = function(device,obj,prop) {
    console.log("found a function: " + $scope.mySingleton.publicMethod());

    //$scope.myList.addBacnet(device,obj,prop);
  };

  $scope.removeFromList = function(device,obj,prop) {};

  $scope.saveToFile = function(){

  };
});

Solution

  • You need to use $scope.mySingleton.getInstance().publicMethod() as @Robby pointed out in the comment.

    Following is the flow:

      $scope.mySingleton = (function () {
        ...
        function init() {
          ...
          return {
            publicMethod: function () {
              ...
            },
          };
        };
    
        return {
          // Get the Singleton instance if one exists
          // or create one if it doesn't
          getInstance: function () {
            if ( !instance ) {
              instance = init();
            }
            return instance;
          }
        };
      })();
    

    The above structure returns mySingleton with an object assigned to it as:

    {
       getInstance: function() {...}
    }
    

    Once you call that then you have access to what init() returned which is the target publicMethod().