Widget Factory is a widget builder object based on Widget Router routing.
npm install widget-factoryjavascript
var WidgetFactory = require('widget-factory');
var containerId = '#widgetFactoryContainer'; // (Mandatory) Id to the Div Container of all widget pages
var renderAfterRoute = true;
var routerConfiguration = { // Widget Router Configuration
afterRouteInitController: true,
pageIdPrefix: 'widget_router_',
usingTemplates: true, // This is set to true because i'm using a TemplateRenderer (JSRender). In this way, the uncompiled and unrendered template will not be showed to the client, and you will need always to render the template to show the content inside the page.
routes: [
{
name: "main",
template: '{{:title}}
{{:description}}
',
controller: function(routeResult) {
// this controller will return a promise, just for fun...
var dfd = new Promise ((resolve, reject) => {
routeResult.routeScope.title = 'Main Page';
routeResult.routeScope.description = 'Main page description';
// If you don't set 'renderAfterRoute' parameter to true, this template will not be rendered.
// If you set it to false, then you will need to render the page like this:
//routeResult.controllerHelper.render(routeResult.routeName, routeResult.routeScope);
resolve();
});
return dfd;
}
},
{
name: "secondary",
template: 'Secondary Page
Secondary page description
',
controller: function (routeResult) {
// If you don't set 'renderAfterRoute' parameter to true, this template will show a blank page, because "usingTemplates" is set to true in routerConfiguration
// If you set it to false, then you will need to render the page like this:
//routeResult.controllerHelper.render(routeResult.routeName, routeResult.routeScope);
}
}
]
}
window.WidgetFactory = new WidgetFactory.WidgetFactory(containerId, routerConfiguration, null, null, renderAfterRoute);
WidgetFactory.start();
`
##Events "beforeroute" and "afterroute"
If you want to inject some functions executions before or after the router is executed. Then you can do it this way:
`javascript
WidgetFactory.on('beforeroute', (event, sender) => {
return new Promise(function(resolve, reject) {
setTimeout(() => {
console.log('event ' + event + ' will be executed after this async method');
resolve();
}, 100);
});
});
WidgetFactory.on('beforeroute', (event, sender) => {
console.log('do something before route event gets executed');
});
WidgetFactory.on('afterroute', (event, sender) => {
console.log('do something after route event have finished');
});
`
##Events "beforestart" and "afterstart"
If you want to inject some functions executions before or after the WidgetFactory start method is executed. Then you can do it this way:
`javascript
WidgetFactory.on('beforestart', (event, sender) => {
return new Promise(function (resolve, reject) {
setTimeout(() => {
console.log('event ' + event + ' will be executed after this async method');
resolve();
}, 100);
});
});
WidgetFactory.on('afterstart', (event, sender) => {
console.log('event ' + event + ' was executed');
});
`
Notes
There is an IPageController and a default PageController object implemententation of that interface exported in this library, you can extend from it and use its default render function.
If you are going to use this library with webpack, please add this aliases inside your webpack.config.js:
`javascript
alias: {
"widget-router": "widget-router/dist/widget-router",
"widget-factory": "widget-factory/dist/widget-factory",
"event-handler-manager": "event-handler-manager/dist/event-handler-manager"
}
``