TerrificJS is a nice little JavaScript framework – based on jQuery – that helps you to modularize your JavaScript code in a very natural way.
The slides illustrate the concepts and features of TerrificJS to see how it helps you to reduce frontend complexity.
The Terrific Composer is a Frontend Development Framework – based on Symfony2 – that aims to make your life easier. It takes the modularization approach of Terrific and provides you a project structure that enables you to start right away.
9. What’s a module?
web application module (n)
1 : an independent unit of functionality that is part of the total
structure of a web application
http://www.slideshare.net/nzakas/scalable-javascript-application-architecture
Remo Brunschwiler 14. August 2012 # 9
21. Components
‣ Modules (the real stars of Terrific)
‣ Skins (extend your modules on demand)
‣ Connectors (let your modules stay in touch with each other)
Remo Brunschwiler 14. August 2012 # 16
22. Modules
The real stars of Terrific.
Almost all of your code will find its place in one of the modules.
Remo Brunschwiler 14. August 2012 # 17
25. HTML
Simply mark your modules by giving them a module class
<div class="mod mod-hero">
<!-- here comes your mr. terrific markup -->
</div>
<div class="mod mod-hero">
<!-- here comes your batman markup -->
</div>
<div class="mod mod-hero">
<!-- here comes your spiderman markup -->
</div>
Remo Brunschwiler 14. August 2012 # 19
26. CSS
Prefix your module CSS rules – in OOCSS style – to prevent yourself
from strange side-effects
/* hero.css */
.mod-hero {
...
}
.mod-hero pre {
...
}
.mod-hero .bubble {
...
}
...
Remo Brunschwiler 14. August 2012 # 20
27. TerrificJS
(function($) {
Tc.Module.Hero = Tc.Module.extend({
/**
* Initializes the Hero module.
*/
init: function($ctx, sandbox, modId) {
this._super($ctx, sandbox, modId);
},
/**
* Hook function to do all of your module stuff.
*/
on: function(callback) {
...
callback();
},
/**
* Hook function to trigger your events.
*/
after: function() {
...
}
});
})(Tc.$);
Remo Brunschwiler 14. August 2012 # 21
28. on(callback)
Here goes all of your jQuery code
on: function(callback) {
var $ctx = this.$ctx,
self = this;
...
// bind the submit event on the form
$('form', $ctx).on('submit', function() {
...
});
callback();
}
‣ $ctx = module DOM node (.mod) -> ensures encapsulation
‣ callback() -> indicates that the module is ready for the
«after» hook
Remo Brunschwiler 14. August 2012 # 22
29. after()
Trigger the default state of your modules
after: function() {
var $ctx = this.$ctx;
// trigger the first submit to write the default message in the bubble
$('form', $ctx).trigger('submit');
}
‣ synchronized among modules, i.e. the «on» hook of all modules
on the page are finished -> relevant for connectors
Remo Brunschwiler 14. August 2012 # 23
30. Skins
Extends your modules on demand – so you can apply different
behaviours on the same module.
Remo Brunschwiler 14. August 2012 # 24
32. HTML
Simply give your module one or more skin classes
<div class="mod mod-hero skin-hero-stealth">
<!-- your stealth hero markup goes here -->
</div>
<div class="mod mod-news skin-hero-stealth skin-hero-xyz">
<!-- your stealth xyz hero markup goes here -->
</div>
Remo Brunschwiler 14. August 2012 # 26
33. CSS
Prefix your skin CSS rules
.skin-hero-stealth pre {…}
.skin-hero-xyz .mode {…}
‣ skin CSS rules must be included after the module CSS rules
(specificity)
‣ inherits all styles of the module
‣ extends / overwrites them where needed
Remo Brunschwiler 14. August 2012 # 27
34. TerrificJS
TerrificJS skins are simply JavaScript decorators
(function($) {
Tc.Module.Hero.Stealth = function(parent) {
/**
* override the appropriate methods from the decorated module.
* the former/original method may be called via parent.<method>()
*/
this.on = function(callback) {
var $ctx = this.$ctx;
// binding the stealth mode on / off events
$('.on', $ctx).on('click', function() {
...
});
...
parent.on(callback);
};
};
})(Tc.$);
Remo Brunschwiler 14. August 2012 # 28
35. Connectors
Lets your modules stay in touch with each other while remaining
loosely coupled.
Remo Brunschwiler 14. August 2012 # 29
37. HTML
Simply specify your communication channels talk channel
<div class="mod mod-hero mod-hero-stealth" data-connectors="talk">
<!-- here comes your talking mr. terrific markup -->
</div>
<div class="mod mod-hero mod-hero-stealth" data-connectors="talk">
<!-- here comes your talking batman markup… but also secret -->
</div>
<div class="mod mod-hero mod-hero-stealth" data-connectors="talk >
<!-- here comes your talking spiderman markup… but also secret -->
</div>
Remo Brunschwiler 14. August 2012 # 31
39. TerrificJS
The modules fire() method is your talking device
/**
* Notifies all attached connectors about changes.
*
* @method fire
* @param {String} event The event name
* @param {Object} data The data to provide to your connected modules (optional)
* @param {Array} channels A list containing the channel ids to send
* the event to (optional)
* @param {Function} defaultAction The default action to perform (optional)
*/
fire: function(event, [data], [channels], [defaultAction]) {
Remo Brunschwiler 14. August 2012 # 33
40. fire()
In your «talking» module
on: function(callback) {
var self = this,
$ctx = this.$ctx;
...
// bind the submit event on the form
$('form', $ctx).on('submit', function () {
var name = $('pre', $ctx).data('name'),
message = $('.message', $ctx).val();
// write the current message in the bubble and notify the others
self.fire('message', { name: name, message: message }, ['talk'], function () {
$('.bubble', $ctx).text(message);
});
return false;
});
event data channels defaultAction
callback();
}
Remo Brunschwiler 14. August 2012 # 34
41. onEvent()
In your «receiving» module(s)
/**
* Handles the incoming messages from the other superheroes
*/
onMessage:function (data) {
var $ctx = this.$ctx;
onEvent data
data = data || {};
if (data.name && data.message) {
$('.bubble', $ctx).text(data.name + ' said: ' + data.message);
}
}
Remo Brunschwiler 14. August 2012 # 35
42. What we learned so far…
Remo Brunschwiler 14. August 2012 # 36
43. What we learned so far…
But how does the stuff get applied?
Remo Brunschwiler 14. August 2012 # 36
46. Bootstrap
Kickstarts the engine of your Application
Basic bootstrap
(function($) {
$(document).ready(function() {
var $page = $('body');
var application = new Tc.Application($page);
application.registerModules();
application.start();
});
})(Tc.$); here is where the
magic happens
Remo Brunschwiler 14. August 2012 # 39
48. registerModules()
Registers all modules within your application
‣ Finds all modules in the DOM (.mod)
‣ Instantiates the appropriate modules accordingly to the naming
conventions
(e.g. .mod-hero instantiates Tc.Module.Hero)
‣ Decorates the module instances with the given skins
(e.g. .skin-hero-stealth decorates the Hero instance with the
Stealth decorator)
‣ Connects the modules with each other accordingly to the
specified Connector communication channels
(e.g. all modules with the data-connectors="talk" attribute
are connected)
Remo Brunschwiler 14. August 2012 # 41
49. start()
Starts all registered Modules
‣ Now the turn is at the modules (hooks)
Remo Brunschwiler 14. August 2012 # 42
51. Sandbox
Gives your Modules the power to communicate with the Application
‣ Add / remove modules on the fly (very useful for AJAX stuff)
‣ Subscribe / unsubscribe from connectors programmatically
‣ Access to config params
‣ Access to other module instances
Remo Brunschwiler 14. August 2012 # 44
53. Terrific Composer
Frontend Development Framework
‣ Designed for building frontends / applications based on the
Terrific concept
‣ Integrates TerrificJS
‣ Based on Symfony2
‣ … still very young
Remo Brunschwiler 14. August 2012 # 46
54. Terrific Composer
Frontend Development Framework
‣ Designed for building frontends / applications based on the
Terrific concept
‣ Integrates TerrificJS
‣ Based on Symfony2
‣ … still very young
… melts dozens of best practices!
Remo Brunschwiler 14. August 2012 # 46
55. Create
Add new Modules & Skins to your project.
The Skeleton is generated for you so that
you can start right away.
Remo Brunschwiler 14. August 2012 # 47
56. Open
The Open dialog provides you fast access to
all of your Modules and Pages.
Remo Brunschwiler 14. August 2012 # 48
57. Inspect
The inspect mode shows you which modules
are in use on the current page.
Remo Brunschwiler 14. August 2012 # 49
58. … Let’s see it in action
Remo Brunschwiler 14. August 2012 # 50