A simple module initializer by creating view models. The main attitude of this module binding system is inspired by Nicholas Zakas's presentation here : http://www.slideshare.net/nzakas/scalable-javascript-application-architecture
var app = new Booklet('app', {someOption : 'some options'});
var page = app.createView('page', {somePageOption : 'some page options'});
page.register('testModule', () => {
return {
init: () => {
console.log('init invoked');
this.testFunc();
},
testFunc: () => {
console.log('testFunc invoked.');
}
}
}
});
page.register('testModule', (page) => {
return {
init: () => {
console.log('init invoked');
var config = page.getConfig();
console.dir(config.somePageOption); // logs 'some page options'
},
testFunc: () => {
console.log('testFunc invoked.');
}
}
}
});
page.start('testModule');
page.startAll();
const elementCache = {
body: document.getElementsByTagName('body'),
footer: document.getElementsByTagName('footer')
}
page.register('testModule', {
init: () => {
this.bindEvents();
},
bindEvents: () => {
page.bindEvent(elementCache.body, 'click', () => {
console.log('clicked on body');
});
page.bindEvent(elementCache.footer, 'mouseout', () => {
console.log('mouse out on footer');
});
}
});
app.createService('testService', () => 'testService invoked');
page.register('menu', {
init: () => {
console.log(this.getServiceWorked()); //logs "testService invoked"
},
getServiceWorked: () => {
var testService = app.getService('testService');
return testService();
}
});
page.createService('testService', () => 'testService invoked');
page.register('menu', {
init: () => {
console.log(this.getServiceWorked()); //logs "testService invoked"
},
getServiceWorked: () => {
var testService = page.getService('testService');
return testService();
}
});
app.createService('testService', () => 'testService invoked');
page.register('menu', {
init: () => {
console.log(this.getServiceWorked()); //logs "testService invoked"
},
getServiceWorked: () => {
var testService = this.getService('testService');
return testService();
},
getService: (serviceName) => {
return app.getService(serviceName);
}
});
page.createService('testService', () => 'testService invoked');
page.register('menu', {
init: () => {
console.log(this.getServiceWorked()); //logs "testService invoked"
},
getServiceWorked: () => {
var testService = this.getService('testService');
return testService();
},
getService: serviceName => page.getService(serviceName)
});
var app = new Booklet('app', {
appOption : 1
});
var page = app.createView('page', {
pageOption : 2
})
page.register('testModule', {
init: () => {
this.logOptions();
},
logOptions: () => {
console.dir(app.defaults);
console.dir(page.defaults);
}
});
page.subscribe('testEvent', (data) => {
console.log(data);
});
app.publish('testEvent', {
testData : 'test data...'
}); // logs Object {testData: "test data..."}
- The subscribe/publish structure to be implemented into modules like page instances