0

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(){

  };
});
2
  • 1
    Looks like that should be: $scope.mySingleton.getInstance().publicMethod() Commented Jul 4, 2018 at 3:57
  • mySingleton returns an object with one method, getInstance(), but you are calling publicMethod() on that object. Where do you call getInstance? Commented Jul 4, 2018 at 4:04

1 Answer 1

0

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().

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.