gigi
gigi

Reputation: 3936

Module pattern context issue

How to access private variable "state" in the delete confirmation function. The "this" keyword points me to the window object.

var usersController = (function() {
  var state = {},

    init = function(defaultState) {
      state = defaultState;

      $(".btn-delete-row").on("click", function() {
        var recordId = $(this).attr("data-record-id");
        showDeleteConfirmation(recordId);
      });
    },

    showDeleteConfirmation = function(recordId) {
      //how to access state private variable here???
    };

  return {
    init: init
  };
}());

and I call it like this:

$(function() {
  usersController.init({
    urls: {
      deleteRecord: "...."
    }
  });
});

Upvotes: 1

Views: 53

Answers (1)

charlietfl
charlietfl

Reputation: 171679

The variable state is available anywhere inside usersController

Try:

showDeleteConfirmation = function(recordId) {
      console.log(state);
};

DEMO

Upvotes: 1

Related Questions