Skip to main content

Making a simple modular single paged website using ASP .Net MVC and knockout.js

This post will be about how to make a simple website that supports multiple modules which are displayed in a single page.

By default unlike a lot of other javascript frameworks, knockout.js doesn't really support modules. But fortunately we can implement this functionality using knockout.js itself.

We are also going to use bootstrap because it already has builtin support for tabs and other useful features.

The structure of the website will be based on tabs. On the left of the tab header bar we will have a menu from which we can select a module to open just by clicking on it. Once we open a module, a tab header will appear up in the tab header bar with the name of the module. And down in the tab content pane, the content of the current tab will appear.

Bellow we can see a sketch of the website:

We will have a javascript object type which will hold the name, id and functions to load and unload a module from the module tab content area.

It looks like this:




To create a module, we create some objects of this type and pass them to the tab manager javascript object. This javascript object is responsible for managing the tabs which means to create them, destroy them or show them. Only one instance of this object type should be allowed.

It looks like this:




It contains the necessary javascript functions to display an opened module, to hide an opened module, to open a new module and to close an opened module.

And finally we have another javascript object to represent an opened module and to provide data to the user interface from the module object mentioned earlier. This includes the ids of the html elements for the module tab header and content. Using these ids we can link the tab header to display the tab content when it is opened.

Its definition is found bellow:




When we open a module, a new instance instance of the object above is created and inserted into the active tabs collection of the tab manager. It also calls the load method of the module to load its content. In this case I used a dedicated ASP .Net MVC controller for modules to load their data from the server once they are opened. 

The controller is called "ModulesController" and it looks like bellow:





To hide or display tabs, we will use a functionality of bootstrap. It already has support for tabs. We just provide the proper ids, href, data-toggle and css classes for the tab header and tab content. Once these are hooked up the tabs will work.

This is the html code and knockout.js template to generate the tabs:




The tab header has a class and some special properties called href and data-toggle. The values of these properties tell the html element what other element to trigger when we click on it. In this case we trigger the tab content element. In the href field we actually set the tab content element id to know what element to trigger.

When we open a module and we call the module's load function, we can create some new knockout.js models and apply bindings inside the tab content html element. 

But we have to be careful when we close the module, to remove the knockout.js bindings and delete any remaining javascript objects. This is not really ideal.

We should provide each module with its own space to create javascript objects. Then we can forcefully destroy its space when we close the module. I think this is why some javascript frameworks similar to knockout have some sort of scope for modules, especially Angular 1.5.

There is a demo attached at the address: https://1drv.ms/u/s!AkDfK07lnj1xi-o5GkMUBCVLlFr7vQ

The demo is a bit incomplete. For a real life website you need some kind of authorization and authentication support. You can't really allow anyone to access any module of the website. The inaccessible modules can be hidden from the user when the main page is rendered but also for proper safety when a module is loaded from the "ModuleController" on the server side the permissions should be checked for the logged user.

Also, in this example I just showed the modules in some tabs. But you can really split the screen estate into any way you want. You can save the loaded models for a user to customize his page. When he enters the page, we check the saved loaded modules for him and load them again. You just need something to manage them, load them, hide them, allocate screen estate and html to show them and so on.

Comments

Popular posts from this blog

Some software development common sense ideas

 I haven't really written here in a long time so it's time to write some new things. These are just some random common sense things that seemed to short to write about individually but seem really interesting and useful to me or other people 1. There is nothing fixed in software development, all things vary by circumstances and time Remember the documentation that didn't seem that important when you started the project, well after a couple of years one the application has grown and become really complicated, no one actually knows everything about the application anymore. So now you really need that documentation. What happens if you suddenly need much more people to develop the application because of some explosive growth? Without documentation, new developers will just look at the application like they look at a painting. This actually happened to me. Maybe in the beginning of a project, a technology really helped you a lot but as the project grew, it started making things

My thoughts as an experienced developer in this field.

To be honest, I would have never guessed that      I would end up saying these these things, especially jn the beginning of my career. As you become more experienced, you kind of get to see everything and you become less impressed by new things. Honestly there is some sort of repetitiveness in this field and we kind of like to reinvent the wheel quite often, because well, we become a bit too easily bored. Take for example how the web looks now, a lot of it has been borrowed from other places, mostly desktop development, especially the parts about making custom reusable components.  Or from older frameworks, like state management in the UI which actually was first properly implemented in Asp .Net Webforms, as far as I know, something like 20 years ago, where you used to have the dreaded view state.  But most likely something similar existed before that too. I was not that surprised when React adopted initially this idea. Or even less surprised when hooks where introduced that seemed a b

Some things which are often blindly applied and followed by developers which are not always good

This is probably one of the most controversial things that I have written so far but I am just tired of hearing about these things and discussing these things. Other developers that I know share part of my feelings. I would rather hear more about how people built things, overcame challenges or what new interesting ideas and concepts they implemented. Those things are really interesting and innovative, not hearing about the same theoretical things over and over again. I can just read and learn those things from 100 sources on the internet. Firstly, one of the most discussed and promoted things is agile/scrum development. I think I have been through 5-8 workshops about agile development methodology. And each time, some things differed. There is no 100% standard approach to this. Everyone uses their own version of this development methodology and seem to argue a lot that their approach is right and everyone else is doing it wrong. You go to an interview, this will be one of the first 10 t