1

I want to access methods related to modal in another js file. So I created a global object called app and created methods like so:

//global variables    
app = {};

//modal components
app.modal = function ( ele ) {
  return {
    "closeModal": function() {
      $('.modal.fade').removeClass("in");
      $(".browser .overlay").removeClass("show");
    }
  }
}();

Then to access the closeModal method, I basically call app.modal.closeModal() in a *.js file. But I am getting the following error message:

Exception in setTimeout callback: TypeError: app.modal.closeModal is not a function

I have been staring at this for a while but cannot seem to see where the issue is. I bet I am doing something really stupid.

Thanks for your help.

ramesh
  • 2,296
  • 4
  • 19
  • 29

2 Answers2

1

app.modal is defined as a function, not as an object, so you would have to write app.modal().closeModal().

But you should define modal as an object like this :

app = {
  modal: {
    closeModal: function(){
      // whatever
    }
  }
};
saimeunt
  • 22,666
  • 2
  • 56
  • 61
  • I tried as you suggested but I still seem to get the following error : Exception in setTimeout callback: TypeError: app.modal.closeModal is not a function... – ramesh Sep 02 '15 at 16:39
0

As saimeunt stated, you probably want to use something like:

app = {};

app.modal = {
  closeModal: function() {
    $('.modal.fade').removeClass("in");
    $(".browser .overlay").removeClass("show");
  }
};

Also, you'll need to place this file (containing #closeModal) in a location that will be loaded before the file that calls it is loaded. This is assuming that you're calling it automatically (appears that you are from within a setTimeout()).

For details on loading order, see the answer: https://stackoverflow.com/a/10741082/4044373.

Community
  • 1
  • 1
osxi
  • 278
  • 1
  • 2
  • 8