Members: Join   Log In
Conv Javascript, window.onload and namespacing
belongs to Apps Team  
by Marc Schriftman Rank_member on Mar 28, 2008 - 01:53 PM read 2732 times
 

Recently, I was working on fixing some tinyMCE issues dealing with loading a rich text editor via Ajax. The issues with tinyMCE had to do with the larger problem of loading dynamic content after the page has loaded. There are often times where some sort of control or widget requires some core additions to the environment created by a page load. The essence of the issue is that when the script tries to access elements on the page, they may or may not be completely loaded.

This is an essential facet of Javascript that has brought about Ajax. This asynchronous nature has made dealing with page loading and understanding of the DOM much more critical. Specifically, this document load has been progressively getting solved in a reliable cross browser fashion. Just as libraries such as Prototype, jQuery, Dojo, YUI, etc. have done wonders for working with the DOM, the document onload has gotten some help. 

Dean Edwards made the original effort to provide an all encompassing solution for knowing when the DOM was loaded. This was later updated with input by John Resig, the lead jQuery creator. These helpful articles describe the problem and describe their solutions. Both Dean Edwards and John Resig have many good articles describing helpful best practices for Javascript to help ensure compatibility and interoperability. 

John Resig gave a presentation at google that provides an excellent primer into how jQuery was written and compares its tenants to that of Prototype and MochiKit. One point he makes is the use of namespaces to avoid conflicts with other libraries and to enable easier browser enhancements.

Tying this together, what I think would be beneficial is to implement a BSG namespace of library functions that gets loaded on the window.load. This would enable us to be more confident in how our functions are called and used. Specifically, in the case of tinyMCE, we would have the opportunity to test our code in something of a sandbox knowing that the DOM is fully loaded and there is little chance for a name collision. This also could be beneficial in regard to more organized Javascript as we could make an effort to keep code organized according to its template and/or partial.  

  • Conv Mike Roeder - at lone star ruby conf for the day
    Rank_guide
    re: Javascript, window.onload and namespacing
    Icon-thread a reply to Javascript, window.onload and namespacing
    by Mike Roeder on Mar 31, 2008 - 02:27 PM read 188 times
     
    Hey Eric - as we discussed, I think our best bet is to use Prototype for the event loading (Event.observe(window, 'load')) and perhaps the Prototype extend or Class creation for namespacing. Our issues involve IE compatability (which I think you've solved with the prototype Class creation), where all this javascript lives, and, do we load this BSG name-spaced script on every page? Or do we manually load it where needed? How does sub name-spacing work? For example, I have a bunch of scripts related to wiki versioning (for example). But I don't want to load ALL of the other BSG scripts - is there a way to do this? It's likely that it doesn't matter, I'm just curious.
    • Conv re: re: Javascript, window.onload and namespacing
      Icon-thread a reply to re: Javascript, window.onload and namespacing
       Icon-thread in a conversation thread started here

      by Marc Schriftman Rank_member on Apr 01, 2008 - 05:07 PM read 106 times
       

      So, I've found a slightly nicer wrapping around the Dean Edwards solution that was actually included in a few plugins floating around for Rails. I've attached a simple test set to see how it works. The idea is essentially to create a BsgClass that gets all our methods as we need it. New scripts can attache more methods or objects to the class and we'll trust our FastInit (the lib I mentioned above) to load up our instance. In theory we could probably then figure out an import/require scheme to help reduce loading too much, but honestly, we aren't at that point in in terms of size or anything so I'm not too worried.

       


      javascript_namespaces.zip

Featured

Project LIM


Project ITM

Wiki Archive

Concours Archive

Author Profile

Marc Schriftman  

Profile and writings

Feed_small Marc Schriftman Secure_feed

Member Rank_member

Subscribe

Feed for nGenera Community:
Feed_small Public Secure_feed Secure

Why subscribe? What is RSS?