intro to knockout

24
Intro to Knockout John Maxwell

Upload: john-maxwell

Post on 11-May-2015

253 views

Category:

Technology


0 download

DESCRIPTION

The last portion of the presentation contains a walkthrough of a todo code project. The repo for that code can be found: https://github.com/johnmaxwell/knockout-todo

TRANSCRIPT

Page 1: Intro to Knockout

Intro to KnockoutJohn Maxwell

Page 2: Intro to Knockout

jQuery Spaghetti$usaMap = $("#usa-map") $worldMap = $("#world-map") activeMap = $worldMapsetActiveMap = (map) -> activeMap = mapanimateSwitchMaps = ($toShow, $toHide, animation) -> $toHide.hide(animation).promise().done -> $toShow.show(animation) switchMaps = ($toShow, $toHide) -> animateSwitchMaps($toShow, $toHide, 'puff') setActiveMap($toShow) # other stuffshowWorldMap = -> switchMaps $worldMap, $usaMapshowUsaMap = -> switchMaps $usaMap, $worldMap$usaMap.on 'click', '.international', showWorldMap$worldMap.on 'click', '.region.usa', showUsaMap

Page 3: Intro to Knockout

Where are we going?

• Knockout value proposition and comparison

• Knockout Basics

• Example walkthrough

Page 4: Intro to Knockout

Where are we going?

• Knockout value proposition and comparison!

• Knockout Basics

• Example walkthrough

Page 5: Intro to Knockout

If work with traditional web apps and want to want to add “islands of interactivity”, Knockout is a great option.

Page 6: Intro to Knockout
Page 7: Intro to Knockout

Knockout sales pitch• Declarative Bindings • Automatic UI Refresh • Dependency Tracking • Templating • Supports old browsers

Page 8: Intro to Knockout

What it doesn’t do

• Client-side routing / URL management

• Server synchronization

• Rigid conventions

Page 9: Intro to Knockout

Where are we going?

• Knockout value proposition and comparison!

• Knockout Basics

• Example walkthrough

Page 10: Intro to Knockout

Where are we going?

• Knockout value proposition and comparison

• Knockout Basics!

• Example walkthrough

Page 11: Intro to Knockout

MVVM Framework

• Model • View • ViewModel

Page 12: Intro to Knockout

The ViewModel

function AppViewModel() { this.firstName = "John"; this.lastName = "Maxwell"; } ko.applyBindings(new AppViewModel());

Page 13: Intro to Knockout

<p> First name: <strong data-bind="text: firstName"></strong></p><p> Last name: <strong data-bind="text: lastName"></strong></p>

The View

Page 14: Intro to Knockout

Two-way Binding<p> First name: <strong data-bind="text: firstName"></strong></p><p> Last name: <strong data-bind="text: lastName"></strong></p><p> First name: <input data-bind="value: firstName" /></p><p> Last name: <input data-bind="value: lastName" /></p>

Page 15: Intro to Knockout

View Model Revisited

function AppViewModel() { this.firstName = "John"; this.lastName = "Maxwell"; } ko.applyBindings(new AppViewModel());

Page 16: Intro to Knockout

Observables

function AppViewModel() { this.firstName = ko.observable("John"); this.lastName = ko.observable("Maxwell"); } ko.applyBindings(new AppViewModel());

Page 17: Intro to Knockout

Observable interactionfunction AppViewModel() { this.firstName = ko.observable("John"); this.lastName = ko.observable("Maxwell");} var viewModel = new AppViewModel();viewModel.firstName(); // "John"viewModel.firstName("Silver Hammer");viewModel.firstName(); // "Silver Hammer"

Page 18: Intro to Knockout

Two-way Binding<p> First name: <strong data-bind="text: firstName"></strong></p><p> Last name: <strong data-bind="text: lastName"></strong></p><p> First name: <input data-bind="value: firstName" /></p><p> Last name: <input data-bind="value: lastName" /></p>

Page 19: Intro to Knockout

Where are we going?

• Knockout value proposition and comparison

• Knockout Basics!

• Example walkthrough

Page 20: Intro to Knockout

Where are we going?

• Knockout value proposition and comparison

• Knockout Basics

• Example walkthrough

Page 21: Intro to Knockout

Todo list application• Load existing todos from localStorage

• Edit existing todos

• Add new todos

• Save todos to localStorage

• Remove individual todos

• Clear all todos

Page 22: Intro to Knockout

Walkthrough Primer• TodoList is our ViewModel for managing an array of

Todo Models

• ObservableArrays are like Observables, with some extra support for managing collections

• foreach is a binding that iterates over ObservableArrays

• foreach shifts the binding context to elements of the ObservableArray

• Binding values can be arbitrary JavaScript expressions

Page 23: Intro to Knockout

Other useful bindings

• css: Adds/removes class on element

• attr: Adds/removes attributes of an element

• disable/enable: Disables/enables form inputs

• options: Adds/removes options in select box

Page 24: Intro to Knockout

Extra resources

• http://learn.knockoutjs.com/

• https://github.com/johnmaxwell/knockout-todo

• Twitter: @greenagain

• http://agileleague.com/