Introducing structure and maintainability
@naugtur, careercon 06.2012(function(){
var privateVariable;
function privateMethod(){ /*...*/ }
function publicMethod(){ /*...*/ }
return {
public:publicMethod
}
})()
AMD module pattern (like in RequireJS)
define(['dependency1','dependency2'],function(Dep1,Dep2){
var privateVariable;
function privateMethod(){
Dep1.someMethod(); // use dependencies
}
function publicMethod(){ /*...*/ }
return {
public:publicMethod
}
})()
Implementation example: Underscore.js
var data={
foo:'123',
bar:{
a:'knock knock',
b:"who's there?"
}
}
//this creates a function
var precompiledTemplate = _.template('<p class="<%= foo %>"><%= bar.a %></p><p><%= bar.b %></p>');
$('#content').html(precompiledTemplate(data));
<script type="text/template" id="Texample">
<p class="<%= foo %>"><%= bar.a %></p>
<p><%= bar.b %></p>
</script>
//this creates a function
var precompiledTemplate = _.template($('#Texample').html());
$('#content').html(precompiledTemplate(data));
<script type="text/template" id="Texample">
<p class="{{ foo }}">{{ bar.a }}</p>
<p>{{ bar.b }}</p>
</script>
_.templateSettings = {
interpolate : /\{\{(.+?)\}\}/g //sets up the notation
};
//this creates a function
var precompiledTemplate = _.template($('#Texample').html());
$('#content').html(precompiledTemplate(data));
subscribe('newsTopic',function(){
console.log('hello1');
});
subscribe('newsTopic',function(){
console.log('hello2');
});
subscribe('otherTopic',function(){
console.log('good evening');
});
publish('newsTopic');
> hello1
> hello2
There is no popular mediator pattern implementation. Look for overlord.js on github
naugtur
naugtur.pl