I could be wrong, but I think what you're doing below with your initEvents
function is misguided. The purpose of the jQuery $(document).ready() syntax
is for itself to fire at the proper time and to encapsulate the javascript
added to the page making it unobtrusive.

 

So, where are you calling initEvents() from? If it's from something like
<body onload=""> or really anything else I can think of I'm pretty sure
you're negating the whole purpose of using $(document).ready to begin with. 

 

http://docs.jquery.com/Tutorials:How_jQuery_Works#Launching_Code_on_Document
_Ready 

 

Also, why you would want to encapsulate your javascript in strings and then
spend time parsing them out again escapes me. What's the benefit of writing:

 

initEvents({'a.alert_hello_world:click': function(event){ 







      alert("Hello World!"); event.preventDefault(); 
   },

..

 

and then parsing it out again when you could just write:

 

$(document).ready(function(){

                $("a.alert_hello_world").click(function(event){alert("Hello
World!"); event.preventDefault();});

});

 

If you've been using jQuery for 5 minutes there's nothing particularly
difficult about what's going on in the line above, all your init code still
stays in the same place where it should be, is called at the proper time and
is faster to execute.

 

Again, I could be missing something really major about what you're
accomplishing. If I'm not, however, and I seem like I'm coming down hard
it's just that I don't want something like this to propagate as the 'right'
way to use jQuery.

 

Smiling,

 

Ian Serlin

 

From: [email protected] [mailto:[EMAIL PROTECTED] On Behalf Of
Hugh Bien
Sent: Wednesday, January 30, 2008 1:28 AM
To: [email protected]
Subject: [SDRuby] Re: tips and tricks

 

I've been using jquery and think it's awesome.  Here's a snippet that lets
you apply events unobtrusively like CSS (like lowpro):

var initEvents = function(behaviors){


    /* Add behaviors to the document when it's ready */






    $(document).ready(function(){


        var ruleAndEvent, rule, event, key, fn;


        for(key in behaviors){


            fn = behaviors[key];


            ruleAndEvent = key.split(":");


            rule = ruleAndEvent[0];






            event = ruleAndEvent[1];


            $(rule)[event](fn);


        }                     


    });                


}








You can use it like this:





initEvents({


  'a.alert_hello_world:click': function(event){ 






      alert("Hello World!"); event.preventDefault(); 


   },


  


  'p.hide_myself:click': function() { 


      // this refers to the paragraph being clicked


      $(this).hide();


   }


});












Also check out the 'livequery' plugin, which attaches events to updated DOM
elements.





Unobtrusive JS is a LOT easier to maintain with lots of JS code. It's kind
of like keeping all your


CSS in external files instead of doing them inline.  There are other
benefits too, like being easier






to degrade if the user doesn't have JS activated and browser caching.





- Hugh

 

On Jan 30, 2008 12:05 AM, Matt Aimonetti < <mailto:[EMAIL PROTECTED]>
[EMAIL PROTECTED]> wrote:

Some of you has some specific questions about RSpec and autotest, here are
some answers:

 <http://railsontherun.com/2008/1/30/misc-tips-and-tricks>
http://railsontherun.com/2008/1/30/misc-tips-and-tricks

FYI, Lowpro is being ported to JQuery, anyone using JQuery and wants to tell
us more about why and how? What about Unobtrusive Javascript, I know that
Ryan started doing some, anyone else?

-Matt







--~--~---------~--~----~------------~-------~--~----~
SD Ruby mailing list
[email protected]
http://groups.google.com/group/sdruby
-~----------~----~----~----~------~----~------~--~---

Reply via email to