ELABORATION Event driven programming The programming model in which operations

Elaboration event driven programming the programming

This preview shows page 230 - 232 out of 517 pages.

ELABORATION: Event-driven programming
Image of page 230
The programming model in which operations specify a completion callback rather than waiting for completion to occur is called event-driven programming . As you might conclude from the number of handlers and callbacks in this simple example, event-driven programs are considered harder to write and debug than task-parallel programs such as Rails apps, in which separate machinery in the app server effectively creates multiple copies of our app to handle multiple simultaneous users. Of course, behind the scenes, the operating system is switching among those tasks just as programmers do manually in JavaScript: when one user’s “copy” of the app is blocked waiting for a response from the database, for example, another user’s copy is allowed to make progress, and the first copy gets “called back” when the database response arrives. In this sense, event-driven and task-parallel programming are duals, and emerging standards such as WebWorkers enable task parallelism in JavaScript by allowing different copies of a JavaScript program to run simultaneously on different operating system threads. However, JavaScript itself lacks concurrency abstractions such as Java’s synchronized and inter-thread communication, so concurrency must be managed explicitly by the application. Self-Check 6.6.1. In line 13 of Figure 6.15 , why did we write MoviePopup.showMovieInfo instead of MoviePopup.showMovieInfo() ? The former is the actual function, which is what ajax expects as its success property, whereas the latter is a call to the function. Self-Check 6.6.2. In line 33 of Figure 6.15 , why did we write $(MoviePopup.setup) rather than $(’MoviePopup.setup’) or $(MoviePopup.setup()) ? We need to pass the actual function to $() , not its name or the result of calling it. Self-Check 6.6.3. Continuing Self-Check 6.6.2 , if we had accidentally called $(’MoviePopup.setup’) , would the result be a syntax error or legal but unintended behavior? Recall that $() is overloaded, and when called with a string, it tries to interpret the string as HTML markup if it contains any angle brackets or a CSS selector otherwise. The latter applies in this case, so it would return an empty collection, since there are no elements whose tag is MoviePopup and whose CSS class is setup . 6.7 Testing JavaScript and AJAX Even our simple AJAX example has many moving parts. In this section we show how to test it using Jasmine, an open-source JavaScript TDD framework developed by Pivotal Labs. Jasmine is designed to mimic RSpec and support the same TDD practices RSpec supports. The rest of this section assumes you’ve read Chapter 8 or are otherwise proficient with TDD and RSpec; as Figure 6.17 shows, we will reuse all those TDD concepts in Jasmine. What RSpec/Ruby Jasmine/JavaScript Libraries rspec, rspec-rails gems jasmine gem, jasmine-jquery add-on Setup rails generate rspec:install rails generate jasmine:install Test files spec/models/, spec/controllers/, spec/helpers spec/javascripts/ Naming conventions spec/models/movie_spec.rb contains tests for app/models/movie.rb spec/javascripts/movie_popup_spec.js
Image of page 231
Image of page 232

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