Search code examples
javascriptnamespacesonready

Javascript Onready function to call the Namespace function


i am removing the global variables in the Javascript that is used in my Rails/views/show.html.erb

The javascript that i am using is

var App = {};
App.UserSnapShot = function () {
  var _body, _detailEl, _selectedLinkEl;

  var _init = function () {
    _body = $$("body")[0];

    $$(".user-link").each(function (el) {
      el.observe("mouseover", function (event) {
        _selectedLinkEl = el;
        _detailEl = event.element().next();
        _detailEl.show();
        _body.observe("mouseover", _bodyMouseOverFunction);
      });
    });
  };

  var _bodyMouseOverFunction = function (event) {
    if (event.element() != _selectedLinkEl && 
        event.element() != _detailEl && 
        !event.element().ancestors().include(_detailEl)) {
      _detailEl.hide();
      _body.stopObserving("mouseover", _bodyMouseOverFunction);
    }
  };

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

I need to call my function App.UserSnapShot.init();through onready function of the same .. , So that to get this to work .. How can i add a onReady function here.

Pls give some suggestions


Solution

  • You mean?

    App.onReady = function() {
        App.UserSnapShot.init();
    };
    
    document.observe("dom:loaded", App.onReady);