AngularJs with ocLazyLoad and webpack

Recently i got chance to work with 2 technologies. One is the most popular framework and another is the latest version of the later one. Its AngularJS and Angular2. Most interesting thing was, i had to write the entire code in ES6. Intresting?

Scenario The scenario is interesting. Most SPA in AngularJS face a big challenge. As the project grows it will become slow to load. The modules, libararies. Everything need to be loaded in the initial stage. In this scenario lazy loading comes in to handy. The modules and libraries only be loaded whenever they were required and it's dynamically added to the application. Though Angular2 supports lazy loading by dafault, AngularJS doesn't support lazy loading by default.  And also angular need to register the directives, controllers etc. It will create so many requests for a single module. If we could bundle all the required templates, js ,css into a single file we could reduce the number of requests send to the server for retrieving the f…

RequireJS: More with templating - Dynamic data

Find the previous episode here.

Checkout the part 3 branch
git checkout -b part4.0 origin/part4.0
Lets change the templating a bit different so that we could add data to the template. Now if we click on a link other than home everything will be gone.

So we will move the link list to the index before maincontent so that it will always remain there. Next we will replace the modules/home/Home.html with some dummy text.
The index.html wil looks like this
<div>   <ul>     <li><a href="#home">Home</a></li>     <li><a href="#employee">Employees</a></li>     <li><a href="#project">Projects</a></li>   </ul> </div> <div id="maincontent">   </div>
Next we will add some more dummy content to other template html file in projects and employees. And also change the controllers to load and how the content. And also we need to change the router.js like…

RequireJS: More with modules - Templates

Find the previous episode here.

Checkout the part 3 branch
git checkout -b part3.0 origin/part3.0
We have created the routing and added a test module to the application. Lets try to add more to module.

        Am going to use RequireJS "Text" plugin for templating along with  Underscore.js and Jquery
    An AMD loader plugin for requireJS to load text resouces. We will load html using text.
    "Underscore is a JavaScript library that provides a whole mess of useful functional programming helpers without extending any built-in objects."
underscore provides a powerfull templating option. You could try lodash also which is a fork of UnderscoreJS

Let's do some coding now. First we need to create a template in html.
We will extract this code from index.html and will create a new html in modules/home/templates/home.html

<div> <ul> <li><a href="#home">Home</a></li> <li><a href="#em…

RequireJS: Application routing

Find the previous episode here.

Checkout the part2.0 branch
git checkout -b part2.0 origin/part2.0

In this section we will look into how a routing can be done. For this we are using another lightweight routing framework Page.js.  First install it using bower.

bower install visionmedia/page.js We already had added the page in config.js. Now we need to create a router.js which will be in router folder.   define(['page'], function(page) { }); We will create a blank module with loading the page framework. Here we use the Module ID for the module which we defined in config.js. You could use the url also like "bower_components/page/page" But it would be better if a module id us used.

NOTE: In requireJS the .JS extension will not be used.

Next we will require the router module in app.js. define(['config'], function(config) { "use strict"; require(['router/router']); }); This will load the router module after loading the config module.
Now we will…

RequireJS: Getting Started

Modular programming helps to divide the application code based on the module. These programming style helps to manage as well develop the application easily. Requirejs is lightweight framework used in javascript to develop applications using the modular programming.

RequireJS RequireJS is lightweight javascript framework which enables AMD(Asynchronous Module Loader). RequireJS helps to load javascript file and modules asynchronously. Apart from using in browser , it’s also be used alongside in Node projects.

Why RequireJS? It’s lightweight, scalable, comes with an excellent optimising tool. Apart from these the use of  RequireJS comes handy when you need to divide and load javascript files and modules whenever they need. Suppose you had an application which have a home page, employees, and projects module. And had 3 javascript files like home.js, employees.js and projects.js. Normally in single page application you need to load these files when the page loads. Like
Suppose you need a framew…