270 likes | 402 Views
Building a Single Page App the Easy Way With Durandal , Knockout and FluentKnockoutHelpers. John Culviner github.com/ johnculviner @ johnculviner johnculviner.com. DEMO CODE: http://github.com/johnculviner/FluentKnockoutHelpers. About Me. Overview. ASP.NET MVC, Theory & Reality
E N D
Building a Single Page App the Easy Way With Durandal, Knockout and FluentKnockoutHelpers John Culviner github.com/johnculviner @johnculviner johnculviner.com DEMO CODE: http://github.com/johnculviner/FluentKnockoutHelpers
Overview • ASP.NET MVC, Theory & Reality • Single Page App? • What is Durandal, Knockout.js? • Other Terms and Frameworks • CODE & DEMO!
What should I get from this? • Understanding of some ASP.NET MVC shortfalls • Some basic pros and cons of SPAs • What problems Durandal/Knockout solves • What problems FluentKnockoutHelpers solves • How to build your own SPA on Durandaleasily without much code OR confusion!
What MVC is good at • Web Sites vs • Web Applications A web site
The Problem Interactive application platforms Web applications
Clients: The Other Problem No Problem! Oh crap In six months? Building a web
ASP.NET MVC Reality Building a webapplication on ASP.NET MVC with limited time and budget. Tomorrow’s is hell today
What is a Single Page App? • AJAX, instead of DOM reload • Possibly use an API w/ JSON • Possible use of client templating • Hash change {{ }} #
Benefits of a Single Page App • State maintained on client • User experience • More interactive • Less network activity and waiting • Developer experience • Better (if you use a framework!) • No constant DOM refresh • Rely on a ‘thick’ client for caching etc. • Vs MVC
Disadvantages of a Single Page App • Building an efficient SPA framework from scratch is very difficult/time consuming • SEO can be problematic • No more DOM refreshes to clean up your messes! • Lots of JavaScript • No compiler help • Lots of “magic strings”
Solution: Use a Framework + Covered Today
What is & ?
Knockout.js • MVVM for JavaScript and HTML
What it’s NOT • Durango
Also what its NOT • A delicious snack (or dinner?)
Durandal.js • New, couple months old. In Hot Towel SPA • http://nuget.org/packages/Durandal.StarterKit/ • Built on top of jQuery, Knockout & RequireJS • jQ Promises, MVVM, AMDs built in • Also does Navigation, Routing, Screen State Management • JS & HTML Modularity • Provides a FRAMEWORK to ORGANIZE your Controllers, ViewModels, and Views • Simple, Effective App Lifecycle Events • Modals, Message Boxes, etc.
Other Frameworks, Terms & Concepts FluentKnockoutHelpers ko.mapping KoLite Knockout.validation ASP.NET WebAPI
Require.js (an AMD loader) AMDs • Think: a combination of dependency injection & namespaces for JavaScript • Shows clear dependencies for USER code w/o polluting the global namespace • Global Namespace • jQuery.js • Knockout.js • Etc. Car.js Engine.js Wheels.js Pistons.js
FluentKnockoutHelpers • Premise: Doing what's simple in ASP.NET MVC should be simple in Durandal and Knockout • Brings validation based on .NET data types AND [DataAnnotation]s to the client for free • Provides C#, strongly typed, compiled, fluent lambda helpers instead magic strings to generate Knockout syntax. (Similar to MVC’s but fluent and ‘better’ ) • Assumes and takes advantage of views being composed of C# models resulting from API calls • Client side type factory for creating new instances of C# types in the client
Durandal router finds VM, invokes activate { //person.js person: null, activate : function(){ save : function() {… cancel : function() {… } User requests URL“…/#/person/1” 2 1 Within activate function { FirstName: ko.observable(), LastName: ko.observable() } 3 GET person/1 API ko.mapping { FirstName: “John”, LastName: “Culviner” } 5 4 {//person.js person: , activate : function(){ save: function(){… cancel: function(){…. } After active promise returns Durandal locates the view by convention DONE! Bound View / View Model appear Save Cancel 7 COMPOSITION Person.cshtml (or HTML) 9 8
DEMO +CODE TIME! Things to note: ALL JSON DOMManipulation All views are straight Razor (NO ASP.NET MVC!) <form></form>
John Culviner GitHub: github.com/johnculviner Blog: johnculviner.com Twitter: @johnculviner Email: john@johnculviner.com DEMO CODE: http://github.com/johnculviner/FluentKnockoutHelpers