ELABORATION Custom events Most of jQuerys events are based on the built in

Elaboration custom events most of jquerys events are

This preview shows page 224 - 226 out of 517 pages.

ELABORATION: Custom events Most of jQuery’s events are based on the built-in events recognized by browsers, but you can also define your own custom events and use trigger to trigger them. For example, you might enclose menus for month and day in a single outer element such as a div and then define a custom update event on the div that checks that the month and day are compatible. You could isolate the checking code in a separate event handler for update , and use trigger to call it from within the change handlers for the individual month and day menus. This is one way that custom handlers help DRY out your JavaScript code. ELABORATION: JavaScript and Rails view helpers In Section 4.8 we used the Rails link_to helper with :method=>:delete to create a clickable link that would trigger the delete
Image of page 224
controller method. We noted that the unusual HTML generated by the helper looked something like this: 1 <a href="/movies/1" data-method="delete" rel="nofollow">Delete</a> Rails’ conventional way to handle a delete operation is using an HTTP POST operation that submits a form with the additional argument _method=”delete” , since most browsers cannot issue HTTP DELETE requests directly. Given the knowledge you’ve gained in this section, Figure 6.13 shows how link_to actually works by annotating code excerpts from the file jquery_ujs.js , which is part of the jquery-rails gem that every Rails app uses by default. Since Web crawlers don’t usually execute JavaScript, the attribute rel=”nofollow” is a request to the crawler or other client not to follow the link, but there’s no guarantee the client will respect this request. That’s why it’s important that your routes only allow the “destructive” controller actions to be called with non- GET methods. 1 // from file jquery_ujs.js in jquery-rails 3.0.4 gem 2 // (Line numbers may differ if you have a different gem version) 3 // line 23: 4 $.rails = rails = { 5 // Link elements bound by jquery-ujs 6 linkClickSelector: ’a[data-confirm], a[data-method], a[data-remote], a[data- disable-with]’, 7 // line 160: 8 handleMethod: function(link) { 9 // ...code elided... 10 form = $(’<form method="post" action="’ + href + ’"></form>’), 11 metadata_input = ’<input name="_method" value="’ + method + ’" type="hidd 12 // ...code elided... 13 form.hide().append(metadata_input).appendTo(’body’); 14 form.submit(); 15 } Figure 6.13: When jquery_ujs is loaded, <a> elements having any of the attributes data-confirm , data-method , data-remote , or data- disable-with are bound to a handler handleMethod that executes when the link is clicked. If the link has a data-method attribute, the handler constructs an ephemeral <form> passing the value of data-method as the hidden _method attribute, hides the form (so it doesn’t appear on the page when constructed), and submits it. In Rails 2 and earlier, the link_to helper generated inline (obtrusive) JavaScript; Rails 3 changed the behavior to more beautiful unobtrusive JavaScript.
Image of page 225
Image of page 226

You've reached the end of your free preview.

Want to read all 517 pages?

  • Spring '19
  • Dr.Marcos

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern

Stuck? We have tutors online 24/7 who can help you get unstuck.
A+ icon
Ask Expert Tutors You can ask You can ask You can ask (will expire )
Answers in as fast as 15 minutes
A+ icon
Ask Expert Tutors