Browse Source

Level 5: more complex jQuery conversions.

Frederic G. MARAND 6 years ago
parent
commit
b79083140a
2 changed files with 79 additions and 0 deletions
  1. 50 0
      lib/5-01-jquery.js
  2. 29 0
      src/5-01-jquery.coffee

+ 50 - 0
lib/5-01-jquery.js

@@ -0,0 +1,50 @@
+// Based on the example in the jQuery Air course example.
+var filteredFlights, flight, showFlights;
+
+$("#tabs ul li a").bind({
+  click: changeTab,
+  mouseenter: showNumberOfFlights,
+  mouseleave: hideNumberOfFlights
+});
+
+showFlights = function(activeDiv) {
+  var fetchingFlights;
+  $("#tabs div").hide();
+  if (fetchingFlights) {
+    fetchingFlights.abort();
+  }
+  return fetchingFlights = $.ajax('/flights', {
+    data: {
+      date: activeDiv
+    },
+    cache: false,
+    error: function(result) {
+      if (result.statusText !== "abort") {
+        return $("#tabs #error").show();
+      }
+    }
+  });
+};
+
+filteredFlights = [];
+
+currentFlights.forEach(function(index, flight) {
+  if (stops === '2+' || flight.routing === 0) {
+    return filteredFlights.push(flight);
+  }
+});
+
+// Better, using a list comprehension
+filteredFlights = (function() {
+  var i, len, results;
+  results = [];
+  for (i = 0, len = currentFlights.length; i < len; i++) {
+    flight = currentFlights[i];
+    if (stops === '2+' || flight.routing === 0) {
+      results.push(flight);
+    }
+  }
+  return results;
+})();
+
+//# sourceMappingURL=5-01-jquery.js.map

+ 29 - 0
src/5-01-jquery.coffee

@@ -0,0 +1,29 @@
+# Based on the example in the jQuery Air course example.
+
+$("#tabs ul li a").bind
+  click: changeTab
+  mouseenter: showNumberOfFlights
+  mouseleave: hideNumberOfFlights
+
+showFlights = (activeDiv) ->
+  $("#tabs div").hide()
+
+  if fetchingFlights
+    fetchingFlights.abort()
+
+  fetchingFlights = $.ajax '/flights',
+    data:
+      date: activeDiv
+    cache: false
+    error: (result) ->
+      if result.statusText isnt "abort"
+        $("#tabs #error").show()
+
+filteredFlights = []
+
+currentFlights.forEach (index, flight) ->
+  if stops is '2+' or flight.routing is 0
+    filteredFlights.push flight
+
+# Better, using a list comprehension
+filteredFlights = (flight for flight in currentFlights when stops is '2+' or flight.routing is 0)