So here I want explain a different approach which I am not going to claim is best optimal solution for all applications but I think it does work for majority of the applications.
Application is a TypeScript class which is the main entry point to client side script, this class is responsible for initializing page component. This is done by reading
data-js-component attribute on body, then import the component dynamically and initialize.
In order to load the component dynamically I am using a module loader called SystemJS, line 15 shows dynamically importing a page component. Once the component is imported it’s
initialize() method is called after creating an instance of that component. One of the main advantage of using a module loader here is it will manage the dependencies which means any dependent modules will be imported automatically.
Every page will have a corresponding page component, name of this page level component is set as a
data-js-component attribute on the body tag. Page component name is generated based on a convention - it will be same as the controller name. e.g. component name for
CustomerController will be
Customer. All page level components resides in a separate directory called Pages.
I have created a Html helper extension method which will return controller name, it also provide flexibility where an action method could change the page component name using a view data
All page level components derive from a base class
Component, this base class implements the core initialization logic. Component can have child components as well, so initializing parent component will initialize child components as well. When
Application class initializes page component, then any child components will also get initialized.
OnReady method on the component is called after initialization is completed.
Component is associated with an HTML element on which the component is going to act up on. This container element is passed as a constructor argument. Page level component is initialized with body as associated element.
Home page component
A page will have many child components as well. To create these child component element associated with that component should have a
Below code shows
Popover component which initializes the bootstrap popover
In order apply this in the razor view, I will be setting
data-js-component attribute like below
##Connecting the dots
Finally to wire up everything -
Application class needs to initialized. For that import
Application module in _Layout.cshtml and initialize it.
Source code used in this article can be found here https://github.com/cvrajeesh/myblogsamples/tree/master/MVC/PageScripts