0

I have a Protoype in my project as follows:

(function($) {

    $.fn.observerPages = function(pageNo) {
      return new ObserverPage($(this), pageNo);
    };

    function ObserverPage(Parent, pageNo) {
      try {
        this.parent = Parent;
        this.isInitialized = false;
        this.timer = -1;
        this.highChart = [];
      } catch (e) {

      }
    }
    ObserverPage.prototype = {
      initialize: function(url) {
        isInitialized = true;
      },
      update: function(controllerurl) {
        if (isInitialized == true) {
            //Update
          }
        },
        startTimer: function(url, interval) {},
        stopTimer: function() {}
      };
    })(jQuery);

This initiates a page that is hooked with an observer and gets data from an ajax service at a defined interval.

What I require is to put an event in this where the user can put a function when this.isInitialized becomes true.

Currently I am using it like this.

var page = $("liveDiv").observerPages(2);
page.initialize("http://localhost:5549/GetLatestNews");
page.update("http://localhost:5549/GetLatestNewsData");

I want an event that the user can handle when isInitialized gets true and can be used like this

page.onInitialize(function(){
   //User writes his function
});

How can I do that?

progrAmmar
  • 2,606
  • 4
  • 29
  • 58
  • This could be done using [events](https://developer.mozilla.org/en-US/docs/Web/Guide/Events/Creating_and_triggering_events) and using [functions as parameters](http://stackoverflow.com/questions/13286233/pass-a-javascript-function-as-parameter). – fsacer Jun 03 '15 at 07:34

1 Answers1

0

Solution might be similar to this:

var event = new Event('pageinit');
ObserverPage.prototype = {
  initialize: function(url) {
    isInitialized = true;
  },
  update: function(controllerurl) {
    if (isInitialized == true) {
      document.dispatchEvent(event);
    }
  },
  startTimer: function(url, interval) {},
  stopTimer: function() {},
  onInitialize: function(callback)
  {
    document.addEventListener('pageinit', callback, false);
  }
};

EDIT: this->document

fsacer
  • 1,382
  • 1
  • 15
  • 23