As the SPA concept and the modern web technology behind it have grown, so have the available frameworks to support its development. A SPA is a great way to create a smooth client experience similar to an phone or desktop application – especially for lightweight and interactive websites who need very little compatibility with older browsers.
While there are plenty of great SPA supporting javascript frameworks available, I will cover how to get the Kendo SPA framework up and running inside an MVC.NET project.
Setting up server-side
Let’s cover the various MVC5 components and how we will use them to delegate control to the SPA. There are quite a few ways you can handle this step but I will cover the one that is likely going to be the simplest.
Step 1: Create a base MVC controller with a default action that is responsible for loading the initial data or your SPA as well as rendering the View that houses your spa template.
Step 2: Create your controllers and actions for the pages of your SPA. This is necessary if your SPA is still url-driven. A lot of SPA applications don’t care to maintain page urls in their app but if you do you can still use basic MVC actions to control any page-entry specific logic as well as return the SPA template. The client router won’t call these actions but they will server as an entry point for the app at that URL.
At this point your MVC controllers should be returning the View that will be responsible for housing the SPA template.
Setting up client-side
Now you are returning a view which is likely just a basic HTML page with kendo loaded to it. From here you will need to configure the necessary components for your SPA.
Step 1: Set up the router
The router is responsible for your page routing locally. It understands the client state of your SPA and is used to facilitate client side navigation.
var router = new kendo.Router({ pushState: true, //use HTML pushstate instead of hashbangs routeMissing: function(e){ //think of this like a local 404 }, change: function(e){ //fires any time the route changes } });
Step 2: Create the layout
We will need to create a layout that acts similar to a master view for our individual views in the next step. We could do this with a template or just as simply as the example here.
var layout = new kendo.Layout("<div id='view-wrapper'></div>");
Step 3: Create your first view
A kendo view is more than just a “page” it can be used in multiple ways, however, for this basic example it will be treated as if it is a “page” of the SPA. The markup for the view goes inside a script tag with the x-kendo-template type, a kendo template.
<script id="home-page" type="text/x-kendo-template"> <div data-bind="text: content"></div> </script>
The ID if the template is the first parameter in the javascript used to wire up your view.
var home = new kendo.View("home-page", { init: function(e){},//When the view is initialized show: function(e){},//When the view is presented hide: function(e){},//When the view is being moved away from model: new kendo.observable({ content: "This is the home page" }) });
In the view you see we set up the model as a kendo observable and defined an object called “content” that holds some text. In the mark up in the template above we bind the text of the div to this value.
There are a few other useful configurations you can make to the kendo view and I would suggest reading over the documentation here.
Step 4: Define the route
Now that we have our router set up and a basic view configured we need to configure the route. The route itself tells the router that we need to match our view with a specific url. Here we are binding the root url “/”.
//Home Page : / router.route("/", function(){ layout.showIn("#view-wrapper", home); });
Now that we have our route configured we need a way to navigate to it. The base of navigation is done via router.navigate();
router.navigate("/")
Tips and Tricks:
- When adding more views make sure that there is a server-side controller that matches the URL of your kendo route or that your server is aware that all urls might be valid and to return a default view regardless. The router won’t contact the server but if you want additional urls to act as entry points into the app then the server needs to know these urls exist.
- You can use jQuery to override the default click event for the anchor tag. This useful if you want to keep urls on your page for both inside the spa and external without the internal links reloading the app. In the example below all internal links are tagged with a data-internal=’true’ attribute.
$("body").delegate("a", "click", function(e){ var href = $(this).attr("href"); var internal = $(this).data("internal"); if(internal == "true" || internal == true){ router.navigate(href); return false; } });
The post Creating a SPA using Telerik Kendo UI appeared first on Falafel Software Blog.