Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I architect single page application using Knockout?

I have begun to play with Knockout recently and I must say it is truly awesome and the power that it can bring to web applications.

However now I want to do something real world and architect my solution using Knockout. Eg. How should my viewmodels be initialized. Where should they go? I am mainly targeting single page application. So, I am interested in architecting single page applications mainly.

like image 447
Tim Tom Avatar asked Jun 01 '12 13:06

Tim Tom


People also ask

Is knockout js single page application?

What is Knockout? Knockout is a JavaScript library to develop Single Page Application and provides the facility of declarative data binding and automatic UI update when the underlying data Model changes.

How do I test a single page application?

A Single Page Application is often seen in two flavors when page interaction or navigation occurs. One flavor is that the URL will have a # in it which the contents behind the # character change upon interactions.

How do I make a single page application without framework?

Let's start by defining a function that uses the History API to navigate to a given path. const navigateTo = url => { history. pushState(null, null, url); router(); }; Next, we can enable all links with the data-link attribute to make use of this function.


2 Answers

There are a lot of pieces to the puzzle, but here is a short list for me.

DISCLAIMER: I'll make some assumptions about your app too, so some of it will vary. Also, this is just one way to do it. There are may good ways. But this should be a good starting point for you.

Assuming your app is a set of abut 5 main views in a SPA:

  1. Create a master/shell html page to house the app
  2. Create a view / partial page (html) for each view. Each view is hidden til you navigate/route to it.
  3. Create a viewmodel that can be bound to each view Create a bootstrapper.js that kickstarts everything.
  4. Bootstrapper should crank up any routing engine you use (sammy, history, etc)
  5. Bootstrapper will bind the views to the viewmodels Bootstrapper can also crank up any seed data and state for your SPA
  6. Tip: Use SoC. Have your viewmodels be models for views. Don;t have them do routing, ajax calls, ui manipulation, etc. Have separate objects for other functions. DRY, KIS, SoC ... all good stuff :-)

I use the Revealing Module Pattern for creation of my viewmodels, though standard Module is perfectly fine too.

If you ave a more specific question, happy to answer that. I tried to keep this short cuz in truth it's not a 5 minute answer. In fact, I'm writing a course for Pluralsight on one way to do this coming in August 2012 :-)

like image 129
John Papa Avatar answered Oct 29 '22 13:10

John Papa


For those who need an updated alternative (2015)... Another option (and a very good one) is to use Yeoman to scaffold a single-page-app for you as described in Steven Sanderson's blog here

It does all the architecturing you need for you so you can concentrate on writing code. Try best to harness the re-usability of the knew knockout component feature - it's amazingly described by Steve Sanderson so I won't take away the glory of his well done work.

like image 34
Salil Junior Avatar answered Oct 29 '22 14:10

Salil Junior