knockout.js with asp.net web api

20
Knockout.js Knockout.js Hands on introduction Ioan Antoniu December 10, 2013

Upload: ioan-antoniu

Post on 16-Apr-2017

484 views

Category:

Technology


1 download

TRANSCRIPT

Page 1: Knockout.js with ASP.Net Web API

Knockout.jsKnockout.jsHands on introduction

Ioan AntoniuDecember 10, 2013

Page 2: Knockout.js with ASP.Net Web API

What is Knockout.js?What is Knockout.js?• Javascript MVVM library

• Declarative data binding

• TemplatingTemplating• Automatic UI refresh• Dependency tracking• Extensible

Page 3: Knockout.js with ASP.Net Web API

Bottom Line UpfrontBottom Line Upfront• What is KnockoutWhat is KnockoutMVVM framework for JavascriptMVVM framework for Javascript

Observable View ModelsObservable View Models

Declarative and Templated ViewsDeclarative and Templated Views

Enables simpler, reusable, frontend and cleaner communication with backendEnables simpler, reusable, frontend and cleaner communication with backend

• GoalGoalScratch the surface of knockoutScratch the surface of knockout

Page 4: Knockout.js with ASP.Net Web API

Consists of three parts

Model: Defines the datastructure

ViewModel: Adapts the model for presentation

View: Presents the data in the UI

MVVM Pattern

Page 5: Knockout.js with ASP.Net Web API
Page 6: Knockout.js with ASP.Net Web API
Page 7: Knockout.js with ASP.Net Web API
Page 8: Knockout.js with ASP.Net Web API
Page 9: Knockout.js with ASP.Net Web API

Model serialized as HTML

Model serialized as Form / Query string

Dictionary

Must know how model

was serialized

Page 10: Knockout.js with ASP.Net Web API

Interface is now

consistently JSON

Javascript is now

responsible for

presenting JSON and communicating JSON back to server

Page 11: Knockout.js with ASP.Net Web API

Server Model JSON ModelView Model

View

MVVMMVVM

Page 12: Knockout.js with ASP.Net Web API

Declarative BindingsUses data-binding attribute to bind to model

Bindings for presentational use:

visible, text, html, css, style, attr, template

Bindings for user interface elements

click, event, submit, enable, disable, value, checked, options, selectionOptions,

uniqueName

Page 13: Knockout.js with ASP.Net Web API

Working with Observable ArraysKnockout provides helper functions

push, pop, remove, removeAll, unshift, shift, reverse, sort, indexOf, splice, slice

Supports native JavaScript Array functions

myObservableArray.push(1); indexof, remove

Page 14: Knockout.js with ASP.Net Web API

The View ModelThe View Model consists of observable properties, variables and/or functions

var viewModel = {

<property> : <observableType>,

<property> : <variable>,

<property> : <function>

}

Page 15: Knockout.js with ASP.Net Web API

Templating

Uses jQuery Template syntax

Has shorthands for more efficient rendering

foreach

Define templates in script tags with

type = text/html

id = the name of the template

Page 16: Knockout.js with ASP.Net Web API

ExtensiblePossible to create your own bindings

Possible to change the templating engine

Page 17: Knockout.js with ASP.Net Web API

How to create an application?

1. Import neccessary scripts2. Define a model3. Define a viewmodel4. Apply bindings

Page 18: Knockout.js with ASP.Net Web API

DemoDemo• PrototypePrototype• Baby stepsBaby stepsObservableObservable

View modelView model

ModelModel

ArrayArray

• Ugh, where’s the View (look at prototype)Ugh, where’s the View (look at prototype)ViewView

View ArrayView Array ( (clean upclean up entry should be ViewModel) entry should be ViewModel)

Its all connectedIts all connected

Computed (Computed ( simplesimple))

More bindings (More bindings ( csscss, , if and valueif and value , , value optionsvalue options, , clickclick, , eventevent, , more clickmore click))

• ActionsActionsRoot ViewModel (Root ViewModel ( with / rootwith / root ))

Strategy for dealing with server (Strategy for dealing with server ( getget ))

• ASP.NETASP.NETJSONJSON

WEB APIWEB API

BundlesBundles

Page 19: Knockout.js with ASP.Net Web API

Questions?

Give me feedback so I can get better

Write me to [email protected] for the demo app and resources for this presentation, other questions etc...

Page 20: Knockout.js with ASP.Net Web API

ResourcesResources• Will post slides / codeWill post slides / code• http://jsfiddle.net/http://jsfiddle.net/Decompose a problem using this sandbox Decompose a problem using this sandbox

• http://knockoutjs.com/http://knockoutjs.com/Interactive tutorials – experience themInteractive tutorials – experience them

• http://www.knockmeout.net/http://www.knockmeout.net/Tons of experience captured in this blogTons of experience captured in this blog

• http://channel9.msdn.com/Events/MIX/http://channel9.msdn.com/Events/MIX/MIX11/FRM08MIX11/FRM08

Great video from early days of KnockoutGreat video from early days of Knockout

• http://channel9.msdn.com/Events/Techhttp://channel9.msdn.com/Events/TechDays/TechDays-2012-Belgium/199Days/TechDays-2012-Belgium/199

Recent video of Knockout + Web Api + Upshot = Amazing!Recent video of Knockout + Web Api + Upshot = Amazing!