Ever wondered how to get rid of that spaghetti, single-filed JavaScript code? Wouldn't it be nice if you could write maintainable modules, easily test them, port them to different projects, handle its library dependencies, and have them decoupled from other modules?
In this talk, we'll see how using the AMD API and an event-driven design will help taming an application's JavaScript code and scaling it to the future and beyond.
44. MyApp.components.photoGallery = (function (window, document, $) {
// Private properties
// ------------------
var element;
var thumbs;
var current = 0;
var timer = 5000;
// Private methods
// ---------------
var goToNext = function () {
// ...
};
var setupGallery = function () {
thumbs.on("click", function () {
// ...
});
setTimeout(function () {
goToNext();
}, timer);
}
return {
// Public methods
// --------------
init : function (el) {
element = $(el);
thumbs = element.find("[data-thumbs]");
setupGallery();
}
};
})(window, document, jQuery);
45. MyApp.components.photoGallery
// Private properties
// ------------------
var
var
var
var
// Private methods
// ---------------
var
};
var
thumbs.on(
});
setTimeout(
goToNext();
}, timer);
}
return
init
element
thumbs
setupGallery();
}
};
})(window
// Private properties
// ------------------
var element;
var thumbs;
var current = 0;
var timer = 5000;
// Private methods
// ---------------
var goToNext = function () {
// ...
};
var setupGallery = function () {
thumbs.on("click", function () {
// ...
});
setTimeout(function () {
goToNext();
}, timer);
}
46. MyApp.components.photoGallery
// Private properties
// ------------------
var
var
var
var
// Private methods
// ---------------
var
};
var
thumbs.on(
});
setTimeout(
goToNext();
}, timer);
}
return
init
element
thumbs
setupGallery();
}
};
})(window
// Public methods
// --------------
init : function (el) {
element = $(el);
thumbs = element.find("[data-thumbs]");
setupGallery();
}
51. B.wishlistMap.ListItem = function (element) {
this.element = $(el);
this.init();
};
B.wishlistMap.ListItem.prototype = {
init : function () {
// ...
},
expandCard : function () {
// ...
},
contractCard : function () {
// ...
}
};
52. var list = $("#wl-cards");
var items = list.find(".wl-card");
items.each(function () {
$(this).data("ListItem", new B.wishlistMap.ListItem(this));
});
// or
var itemsInstances = [];
items.each(function () {
itemsInstances.push(new B.wishlistMap.ListItem(this));
});
128. Yo, can you give me a
shout out whenever a new
GoT episode comes out?
Sure, my good sire!
129. YO
"THE RAINS OF
CASTAMERE" IS OUT GET
IT WHILE IT’S HOT
Cool, then I’ll download
it! It’ll surely by a very
happy episode <3
130. // Pirate observes torrent server
torrentServer.on("new-got-episode", function (name) {
this.download(name);
});
// Torrent server publishes that it has a new GoT episode
this.trigger("new-got-episode", "The Rains of Castamere");
137. MEDIATOR
Mediator, please tell me
when a new Mythbusters
episode comes out
Groovy.
Hey, I’ll want that
as well!
Jammin'.
138. MEDIATOR
Mediator, there’s a new
The Walking Dead episode!
Here’s the link!
Yo folks, there’s a new
The Walking Dead
episode! Here’s the link!
Oh yeah, I’ll get it
right now!
139. MEDIATOR
Mediator, there’s a new
Mythbusters episode!
Here’s the link!
Yo everyone, there’s a
new Mythbusters
episode! Here’s the link!
Cool, I’ll download
it!
Tnx yo!
140. // Pirate 1 subscribes to mediator
mediator.on("new-twd-episode", function (data) {
console.log("Downloading " + data.name + " from " + data.link);
});
mediator.on("new-mythbusters-episode", function (data) {
console.log("Downloading " + data.name + " from " + data.link);
});
// Pirate 2 subscribes to mediator
mediator.on("new-mythbusters-episode", function (data) {
console.log("Downloading " + data.name + " from " + data.link);
});
// Torrent server 1 publishes on mediator
mediator.trigger("new-twd-episode", {
link : "http://foo.bar",
name : "The Suicide King"
});
// Torrent server 2 publishes on mediator
mediator.trigger("new-mythbusters-episode", {
link : "http://theillegalbay.br",
name : "Hollywood Myths"
});
146. // MyApp.myModule now has the `on`, `off` and `trigger` methods
$.extend(MyApp.myModule, EventEmitter.prototype);
MyApp.components.myModule.trigger("my-event", "my-data");