SlideShare ist ein Scribd-Unternehmen logo
1 von 45
Downloaden Sie, um offline zu lesen
Amir Barylko MavenThought Inc.
AMIR BARYLKO
RICH UI WITH
KNOCKOUT.JS &
COFFEESCRIPT
Amir Barylko MavenThought Inc.
INTRO
About me
Your expectations
Overview
Amir Barylko MavenThought Inc.
WHO AM I?
• Software quality expert
• Architect
• Developer
• Mentor
• Great cook
• The one who’s entertaining you for the next hour!
Amir Barylko MavenThought Inc.
RESOURCES
• Email: amir@barylko.com
• Twitter: @abarylko
• Blog: http://www.orthocoders.com
• Materials: http://www.orthocoders.com/presentations
Amir Barylko MavenThought Inc.
YOUR EXPECTATIONS
• (your input here....)
Amir Barylko MavenThought Inc.
DISAPPOINTMENT
MANAGEMENT
• Define Rich / Responsive UI
• Intro to Coffeescript
• Intro to Knockout.js
• Intro to MVVM
• Why binding is a good idea
• Why binding to classes is even better
Amir Barylko MavenThought Inc.
RICH UI
What’s that?
Pros
Cons
Amir Barylko MavenThought Inc.
WHAT’S A RICH UI?
• (good question)
Amir Barylko MavenThought Inc.
PROS
Amir Barylko MavenThought Inc.
CONS
Amir Barylko MavenThought Inc.
COFFEESCRIPT
What is it?
Basic Constructs
Classes
Amir Barylko MavenThought Inc.
WHAT’S WRONG WITH JS
• Too verbose (too many { and } )
• GlobalVariables
• Lacks support for classes
• Hard to make inheritance
• Automatic type conversion between strings and numbers
• NaN is not a number, however it is a number
Amir Barylko MavenThought Inc.
WHAT IS IT?
“CoffeeScript is a little language that compiles
into JavaScript. Underneath all those awkward
braces and semicolons, JavaScript has always
had a gorgeous object model at its heart.
CoffeeScript is an attempt to expose the good
parts of JavaScript in a simple way.”
http://coffeescript.org/
Amir Barylko MavenThought Inc.
STRING INTERPOLATION
•You can concatenate inside a double quote string
using the “#” and “{ }”
"The result is #{3}" == "The result is 3"
•Or use any expression
"/movies/#{id}"
Amir Barylko MavenThought Inc.
FUNCTIONS
•The arrow/lambda defines functions
square = (x) -> x * x
•Parenthesis are optional when passing
parameters
storageDelete movieId, true
Amir Barylko MavenThought Inc.
FUNCTIONS II
•Implicit return
(the last expression is the return value)
•Multiple lines, indentation is important
deleteMovie = (e) ->
movieId = $(e.target)....
storageDelete(movieId)
Amir Barylko MavenThought Inc.
OBJECTS AS HASHES
•Declared using indentation
config =
local:
user: 'dev'
pwd: 'dev123'
remote:
user: 'superdev'
pwd: "impossibleToGuess"
Amir Barylko MavenThought Inc.
ARRAYS
•Arrays are declared with “[ ]”
deploy = ['local', 'remote', 'uat']
fib = [1, 3, 5, 8, 13, 21]
•Slicing
first = fib[0..3]
noLast = fib[0..-2]
Amir Barylko MavenThought Inc.
CLASSES
class MovieRepository
constructor: (@baseUrl) ->
newMovie: ->
$.ajax
url: "#{@baseUrl}/movies/create"
success: (data) -> $(id).append data
Amir Barylko MavenThought Inc.
INHERITANCE
• One class can extend another
class Shape
constructor: (@width) ->
class Square extends Shape
computeArea: -> Math.pow @width, 2
class Circle extends Shape
radius: -> @width / 2
computeArea: -> Math.PI * Math.pow @radius(), 2
Amir Barylko MavenThought Inc.
MODELVIEWVIEW-MODEL
Definition
Pros
Cons
Amir Barylko MavenThought Inc.
MODELVIEW CONTROLLER
Model
Controller
View
X
Amir Barylko MavenThought Inc.
RESPONSIBILITIES
•Model provides data
•View provides visualization
•Controllers provides behaviour
•Controller communicates one to the other
Amir Barylko MavenThought Inc.
CONS
•Hard to use
•Hard to maintain
•Lots of repetition
Amir Barylko MavenThought Inc.
MODELVIEWVIEWMODEL
•Model provides data
•View provides visualization
•ViewModel provides one-to-one what the view
needs
•By convention uses binding to update
Amir Barylko MavenThought Inc.
KNOCKOUT.JS
Intro
Bindings
Observables
Amir Barylko MavenThought Inc.
INTRO
Simplify	
  dynamic	
  Javascript	
  
UIs	
  by	
  applying	
  the	
  Model-­
View-­ViewModel	
  pattern
knockoutjs.com
Amir Barylko MavenThought Inc.
SOME FEATURES
• Free and open source (MIT License)
• Pure Javascript
• Small & lightweight
• No dependencies
• Supports all mainstream browsers
• Good documentation
Amir Barylko MavenThought Inc.
DESCRIPTIVE BINDINGS
• Use the html5 data-bind attribute
• Can have multiple functions
• They can be used for data, attributes, etc
Amir Barylko MavenThought Inc.
STEPS
• Write a view model class
• Write a view with special markup that references the view
model
• Apply the bindings in order to be parsed
• ko.appplyBindings(new MovieLibraryViewModel())
Amir Barylko MavenThought Inc.
TEXT BINDING (1)
• Value of the markup element
• data-bind=‘text: name’
Amir Barylko MavenThought Inc.
FOR EACH (2)
• Binds a collection to markup
• data-bind=‘foreach: movies’
Amir Barylko MavenThought Inc.
CLICK EVENTS (3)
• Indicate handlers for click
• data-bind=‘click: handlerOnVm’
Amir Barylko MavenThought Inc.
HIDE ELEMENTS (4)
• Visible binding hides markup
• data-bind=‘visible: editTitle’
• What happened? Does the input shows? Why?
Amir Barylko MavenThought Inc.
OBSERVABLES (5)
• Subscribe / Publish model
• Notifies when the value changes
• Updates markup
• Triggers notifications
Amir Barylko MavenThought Inc.
OBSERVABLE VALUES (6)
• ko.observable
• Behaves like a function
• If you want the value you need to “evaluate”
Amir Barylko MavenThought Inc.
VALUE
• Binds the value of the markup for inputs
• data-bind=‘value: title’
Amir Barylko MavenThought Inc.
COMPUTED (7)
• Calculate values based on dependencies
• ko.computed(fnToUse)
Amir Barylko MavenThought Inc.
OBSERVABLE ARRAYS (8)
• Same idea as values
• But are collections that notify events
• No need to track adding and removing
• The binding updates that for you
Amir Barylko MavenThought Inc.
SUBSCRIBETO EVENTS (9)
• Each observable also notifies events
• You can subscribe to trigger other calculations
• For example, clear the fields every time before edit a new
movie
Amir Barylko MavenThought Inc.
KNOCKOUT MAPPING
• Easy to transform JSON data into observables
• read from JSON with fromJS
• export to JSON with toJS
• customize your mapping to suit your needs
Amir Barylko MavenThought Inc.
SUMMARY
Amir Barylko MavenThought Inc.
BENEFITS
• Short learning curve compared to other frameworks
• Easy to implement two way binding
• Supports binding templates
• Custom bindings
• Lots of documentation
• Coffeescript adds OOP
Amir Barylko MavenThought Inc.
DRAWBACKS
• The HTML may be harder to read
• ViewModel scope can get out of hand
• No particular structure enforced (is it really a drawback?)
• Other?
Amir Barylko MavenThought Inc.
RESOURCES
• Contact me: amir@barylko.com, @abarylko
• Download: http://www.orthocoders.com/presentations
• coffescript.org
• knockoutjs.com

Weitere ähnliche Inhalte

Andere mochten auch

エフスタ会津 - フロントエンドエンジニアの話 -
エフスタ会津 - フロントエンドエンジニアの話 -エフスタ会津 - フロントエンドエンジニアの話 -
エフスタ会津 - フロントエンドエンジニアの話 -thedesignium Inc.
 
ブラウザでの自動テスト
ブラウザでの自動テストブラウザでの自動テスト
ブラウザでの自動テストhashedrock
 
フロントエンド開発の3ステップ(npm事始め)
フロントエンド開発の3ステップ(npm事始め)フロントエンド開発の3ステップ(npm事始め)
フロントエンド開発の3ステップ(npm事始め)hashedrock
 
デザイナーとディレクターのイイ関係の作り方
デザイナーとディレクターのイイ関係の作り方デザイナーとディレクターのイイ関係の作り方
デザイナーとディレクターのイイ関係の作り方Take Bo
 
現在のWebフロントエンドの現状と愚痴と、それに対するHaxeフロントエンドライブラリMageについて
現在のWebフロントエンドの現状と愚痴と、それに対するHaxeフロントエンドライブラリMageについて現在のWebフロントエンドの現状と愚痴と、それに対するHaxeフロントエンドライブラリMageについて
現在のWebフロントエンドの現状と愚痴と、それに対するHaxeフロントエンドライブラリMageについてNobukazu Hanada
 
WebStorm / PhpStorm 基礎 シーブレイン社内勉強会
WebStorm / PhpStorm 基礎 シーブレイン社内勉強会WebStorm / PhpStorm 基礎 シーブレイン社内勉強会
WebStorm / PhpStorm 基礎 シーブレイン社内勉強会Koji Tanaka
 

Andere mochten auch (7)

Guion general de programa
Guion general de programaGuion general de programa
Guion general de programa
 
エフスタ会津 - フロントエンドエンジニアの話 -
エフスタ会津 - フロントエンドエンジニアの話 -エフスタ会津 - フロントエンドエンジニアの話 -
エフスタ会津 - フロントエンドエンジニアの話 -
 
ブラウザでの自動テスト
ブラウザでの自動テストブラウザでの自動テスト
ブラウザでの自動テスト
 
フロントエンド開発の3ステップ(npm事始め)
フロントエンド開発の3ステップ(npm事始め)フロントエンド開発の3ステップ(npm事始め)
フロントエンド開発の3ステップ(npm事始め)
 
デザイナーとディレクターのイイ関係の作り方
デザイナーとディレクターのイイ関係の作り方デザイナーとディレクターのイイ関係の作り方
デザイナーとディレクターのイイ関係の作り方
 
現在のWebフロントエンドの現状と愚痴と、それに対するHaxeフロントエンドライブラリMageについて
現在のWebフロントエンドの現状と愚痴と、それに対するHaxeフロントエンドライブラリMageについて現在のWebフロントエンドの現状と愚痴と、それに対するHaxeフロントエンドライブラリMageについて
現在のWebフロントエンドの現状と愚痴と、それに対するHaxeフロントエンドライブラリMageについて
 
WebStorm / PhpStorm 基礎 シーブレイン社内勉強会
WebStorm / PhpStorm 基礎 シーブレイン社内勉強会WebStorm / PhpStorm 基礎 シーブレイン社内勉強会
WebStorm / PhpStorm 基礎 シーブレイン社内勉強会
 

Ähnlich wie Rich UI with Knockout.js & Coffeescript

PRDC12 advanced design patterns
PRDC12 advanced design patternsPRDC12 advanced design patterns
PRDC12 advanced design patternsAmir Barylko
 
Cpl12 continuous integration
Cpl12 continuous integrationCpl12 continuous integration
Cpl12 continuous integrationAmir Barylko
 
Awesome html with ujs, jQuery and coffeescript
Awesome html with ujs, jQuery and coffeescriptAwesome html with ujs, jQuery and coffeescript
Awesome html with ujs, jQuery and coffeescriptAmir Barylko
 
PRDCW-avent-aggregator
PRDCW-avent-aggregatorPRDCW-avent-aggregator
PRDCW-avent-aggregatorAmir Barylko
 
Page objects pattern
Page objects patternPage objects pattern
Page objects patternAmir Barylko
 
Page-objects-pattern
Page-objects-patternPage-objects-pattern
Page-objects-patternAmir Barylko
 
A baryklo design-patterns
A baryklo design-patternsA baryklo design-patterns
A baryklo design-patternssdeconf
 
sdec11-Advanced-design-patterns
sdec11-Advanced-design-patternssdec11-Advanced-design-patterns
sdec11-Advanced-design-patternsAmir Barylko
 
PRDCW-advanced-design-patterns
PRDCW-advanced-design-patternsPRDCW-advanced-design-patterns
PRDCW-advanced-design-patternsAmir Barylko
 
Code camp 2012-advanced-design-patterns
Code camp 2012-advanced-design-patternsCode camp 2012-advanced-design-patterns
Code camp 2012-advanced-design-patternsAmir Barylko
 
prdc10-tdd-patterns
prdc10-tdd-patternsprdc10-tdd-patterns
prdc10-tdd-patternsAmir Barylko
 
Open source libraries and tools
Open source libraries and toolsOpen source libraries and tools
Open source libraries and toolsAmir Barylko
 
Codemash-advanced-ioc-castle-windsor
Codemash-advanced-ioc-castle-windsorCodemash-advanced-ioc-castle-windsor
Codemash-advanced-ioc-castle-windsorAmir Barylko
 
Puppet Camp Melbourne Nov 2014 - A Build Engineering Team’s Journey of Infras...
Puppet Camp Melbourne Nov 2014 - A Build Engineering Team’s Journey of Infras...Puppet Camp Melbourne Nov 2014 - A Build Engineering Team’s Journey of Infras...
Puppet Camp Melbourne Nov 2014 - A Build Engineering Team’s Journey of Infras...Peter Leschev
 
ioc-castle-windsor
ioc-castle-windsorioc-castle-windsor
ioc-castle-windsorAmir Barylko
 
Agile requirements
Agile requirementsAgile requirements
Agile requirementsAmir Barylko
 
Why And How To Leverage Predictive APIs In Any Application
Why And How To Leverage Predictive APIs In Any Application Why And How To Leverage Predictive APIs In Any Application
Why And How To Leverage Predictive APIs In Any Application ProgrammableWeb
 
Hi performance table views with QuartzCore and CoreText
Hi performance table views with QuartzCore and CoreTextHi performance table views with QuartzCore and CoreText
Hi performance table views with QuartzCore and CoreTextMugunth Kumar
 
iOSDevCamp 2011 - Getting "Test"-y: Test Driven Development & Automated Deplo...
iOSDevCamp 2011 - Getting "Test"-y: Test Driven Development & Automated Deplo...iOSDevCamp 2011 - Getting "Test"-y: Test Driven Development & Automated Deplo...
iOSDevCamp 2011 - Getting "Test"-y: Test Driven Development & Automated Deplo...Rudy Jahchan
 

Ähnlich wie Rich UI with Knockout.js & Coffeescript (20)

PRDC12 advanced design patterns
PRDC12 advanced design patternsPRDC12 advanced design patterns
PRDC12 advanced design patterns
 
YEG-UG-Capybara
YEG-UG-CapybaraYEG-UG-Capybara
YEG-UG-Capybara
 
Cpl12 continuous integration
Cpl12 continuous integrationCpl12 continuous integration
Cpl12 continuous integration
 
Awesome html with ujs, jQuery and coffeescript
Awesome html with ujs, jQuery and coffeescriptAwesome html with ujs, jQuery and coffeescript
Awesome html with ujs, jQuery and coffeescript
 
PRDCW-avent-aggregator
PRDCW-avent-aggregatorPRDCW-avent-aggregator
PRDCW-avent-aggregator
 
Page objects pattern
Page objects patternPage objects pattern
Page objects pattern
 
Page-objects-pattern
Page-objects-patternPage-objects-pattern
Page-objects-pattern
 
A baryklo design-patterns
A baryklo design-patternsA baryklo design-patterns
A baryklo design-patterns
 
sdec11-Advanced-design-patterns
sdec11-Advanced-design-patternssdec11-Advanced-design-patterns
sdec11-Advanced-design-patterns
 
PRDCW-advanced-design-patterns
PRDCW-advanced-design-patternsPRDCW-advanced-design-patterns
PRDCW-advanced-design-patterns
 
Code camp 2012-advanced-design-patterns
Code camp 2012-advanced-design-patternsCode camp 2012-advanced-design-patterns
Code camp 2012-advanced-design-patterns
 
prdc10-tdd-patterns
prdc10-tdd-patternsprdc10-tdd-patterns
prdc10-tdd-patterns
 
Open source libraries and tools
Open source libraries and toolsOpen source libraries and tools
Open source libraries and tools
 
Codemash-advanced-ioc-castle-windsor
Codemash-advanced-ioc-castle-windsorCodemash-advanced-ioc-castle-windsor
Codemash-advanced-ioc-castle-windsor
 
Puppet Camp Melbourne Nov 2014 - A Build Engineering Team’s Journey of Infras...
Puppet Camp Melbourne Nov 2014 - A Build Engineering Team’s Journey of Infras...Puppet Camp Melbourne Nov 2014 - A Build Engineering Team’s Journey of Infras...
Puppet Camp Melbourne Nov 2014 - A Build Engineering Team’s Journey of Infras...
 
ioc-castle-windsor
ioc-castle-windsorioc-castle-windsor
ioc-castle-windsor
 
Agile requirements
Agile requirementsAgile requirements
Agile requirements
 
Why And How To Leverage Predictive APIs In Any Application
Why And How To Leverage Predictive APIs In Any Application Why And How To Leverage Predictive APIs In Any Application
Why And How To Leverage Predictive APIs In Any Application
 
Hi performance table views with QuartzCore and CoreText
Hi performance table views with QuartzCore and CoreTextHi performance table views with QuartzCore and CoreText
Hi performance table views with QuartzCore and CoreText
 
iOSDevCamp 2011 - Getting "Test"-y: Test Driven Development & Automated Deplo...
iOSDevCamp 2011 - Getting "Test"-y: Test Driven Development & Automated Deplo...iOSDevCamp 2011 - Getting "Test"-y: Test Driven Development & Automated Deplo...
iOSDevCamp 2011 - Getting "Test"-y: Test Driven Development & Automated Deplo...
 

Mehr von Amir Barylko

Functional converter project
Functional converter projectFunctional converter project
Functional converter projectAmir Barylko
 
Elm: delightful web development
Elm: delightful web developmentElm: delightful web development
Elm: delightful web developmentAmir Barylko
 
User stories deep dive
User stories deep diveUser stories deep dive
User stories deep diveAmir Barylko
 
Coderetreat hosting training
Coderetreat hosting trainingCoderetreat hosting training
Coderetreat hosting trainingAmir Barylko
 
There's no charge for (functional) awesomeness
There's no charge for (functional) awesomenessThere's no charge for (functional) awesomeness
There's no charge for (functional) awesomenessAmir Barylko
 
What's new in c# 6
What's new in c# 6What's new in c# 6
What's new in c# 6Amir Barylko
 
Who killed object oriented design?
Who killed object oriented design?Who killed object oriented design?
Who killed object oriented design?Amir Barylko
 
From coach to owner - What I learned from the other side
From coach to owner - What I learned from the other sideFrom coach to owner - What I learned from the other side
From coach to owner - What I learned from the other sideAmir Barylko
 
Communication is the Key to Teamwork and productivity
Communication is the Key to Teamwork and productivityCommunication is the Key to Teamwork and productivity
Communication is the Key to Teamwork and productivityAmir Barylko
 
Acceptance Test Driven Development
Acceptance Test Driven DevelopmentAcceptance Test Driven Development
Acceptance Test Driven DevelopmentAmir Barylko
 
Agile requirements
Agile requirementsAgile requirements
Agile requirementsAmir Barylko
 
Agile teams and responsibilities
Agile teams and responsibilitiesAgile teams and responsibilities
Agile teams and responsibilitiesAmir Barylko
 
Teams and responsibilities
Teams and responsibilitiesTeams and responsibilities
Teams and responsibilitiesAmir Barylko
 

Mehr von Amir Barylko (20)

Functional converter project
Functional converter projectFunctional converter project
Functional converter project
 
Elm: delightful web development
Elm: delightful web developmentElm: delightful web development
Elm: delightful web development
 
Dot Net Core
Dot Net CoreDot Net Core
Dot Net Core
 
No estimates
No estimatesNo estimates
No estimates
 
User stories deep dive
User stories deep diveUser stories deep dive
User stories deep dive
 
Coderetreat hosting training
Coderetreat hosting trainingCoderetreat hosting training
Coderetreat hosting training
 
There's no charge for (functional) awesomeness
There's no charge for (functional) awesomenessThere's no charge for (functional) awesomeness
There's no charge for (functional) awesomeness
 
What's new in c# 6
What's new in c# 6What's new in c# 6
What's new in c# 6
 
Productive teams
Productive teamsProductive teams
Productive teams
 
Who killed object oriented design?
Who killed object oriented design?Who killed object oriented design?
Who killed object oriented design?
 
From coach to owner - What I learned from the other side
From coach to owner - What I learned from the other sideFrom coach to owner - What I learned from the other side
From coach to owner - What I learned from the other side
 
Communication is the Key to Teamwork and productivity
Communication is the Key to Teamwork and productivityCommunication is the Key to Teamwork and productivity
Communication is the Key to Teamwork and productivity
 
Acceptance Test Driven Development
Acceptance Test Driven DevelopmentAcceptance Test Driven Development
Acceptance Test Driven Development
 
Refactoring
RefactoringRefactoring
Refactoring
 
Agile requirements
Agile requirementsAgile requirements
Agile requirements
 
Agile teams and responsibilities
Agile teams and responsibilitiesAgile teams and responsibilities
Agile teams and responsibilities
 
Refactoring
RefactoringRefactoring
Refactoring
 
Sass & bootstrap
Sass & bootstrapSass & bootstrap
Sass & bootstrap
 
Nuget
NugetNuget
Nuget
 
Teams and responsibilities
Teams and responsibilitiesTeams and responsibilities
Teams and responsibilities
 

Rich UI with Knockout.js & Coffeescript

  • 1. Amir Barylko MavenThought Inc. AMIR BARYLKO RICH UI WITH KNOCKOUT.JS & COFFEESCRIPT
  • 2. Amir Barylko MavenThought Inc. INTRO About me Your expectations Overview
  • 3. Amir Barylko MavenThought Inc. WHO AM I? • Software quality expert • Architect • Developer • Mentor • Great cook • The one who’s entertaining you for the next hour!
  • 4. Amir Barylko MavenThought Inc. RESOURCES • Email: amir@barylko.com • Twitter: @abarylko • Blog: http://www.orthocoders.com • Materials: http://www.orthocoders.com/presentations
  • 5. Amir Barylko MavenThought Inc. YOUR EXPECTATIONS • (your input here....)
  • 6. Amir Barylko MavenThought Inc. DISAPPOINTMENT MANAGEMENT • Define Rich / Responsive UI • Intro to Coffeescript • Intro to Knockout.js • Intro to MVVM • Why binding is a good idea • Why binding to classes is even better
  • 7. Amir Barylko MavenThought Inc. RICH UI What’s that? Pros Cons
  • 8. Amir Barylko MavenThought Inc. WHAT’S A RICH UI? • (good question)
  • 11. Amir Barylko MavenThought Inc. COFFEESCRIPT What is it? Basic Constructs Classes
  • 12. Amir Barylko MavenThought Inc. WHAT’S WRONG WITH JS • Too verbose (too many { and } ) • GlobalVariables • Lacks support for classes • Hard to make inheritance • Automatic type conversion between strings and numbers • NaN is not a number, however it is a number
  • 13. Amir Barylko MavenThought Inc. WHAT IS IT? “CoffeeScript is a little language that compiles into JavaScript. Underneath all those awkward braces and semicolons, JavaScript has always had a gorgeous object model at its heart. CoffeeScript is an attempt to expose the good parts of JavaScript in a simple way.” http://coffeescript.org/
  • 14. Amir Barylko MavenThought Inc. STRING INTERPOLATION •You can concatenate inside a double quote string using the “#” and “{ }” "The result is #{3}" == "The result is 3" •Or use any expression "/movies/#{id}"
  • 15. Amir Barylko MavenThought Inc. FUNCTIONS •The arrow/lambda defines functions square = (x) -> x * x •Parenthesis are optional when passing parameters storageDelete movieId, true
  • 16. Amir Barylko MavenThought Inc. FUNCTIONS II •Implicit return (the last expression is the return value) •Multiple lines, indentation is important deleteMovie = (e) -> movieId = $(e.target).... storageDelete(movieId)
  • 17. Amir Barylko MavenThought Inc. OBJECTS AS HASHES •Declared using indentation config = local: user: 'dev' pwd: 'dev123' remote: user: 'superdev' pwd: "impossibleToGuess"
  • 18. Amir Barylko MavenThought Inc. ARRAYS •Arrays are declared with “[ ]” deploy = ['local', 'remote', 'uat'] fib = [1, 3, 5, 8, 13, 21] •Slicing first = fib[0..3] noLast = fib[0..-2]
  • 19. Amir Barylko MavenThought Inc. CLASSES class MovieRepository constructor: (@baseUrl) -> newMovie: -> $.ajax url: "#{@baseUrl}/movies/create" success: (data) -> $(id).append data
  • 20. Amir Barylko MavenThought Inc. INHERITANCE • One class can extend another class Shape constructor: (@width) -> class Square extends Shape computeArea: -> Math.pow @width, 2 class Circle extends Shape radius: -> @width / 2 computeArea: -> Math.PI * Math.pow @radius(), 2
  • 21. Amir Barylko MavenThought Inc. MODELVIEWVIEW-MODEL Definition Pros Cons
  • 22. Amir Barylko MavenThought Inc. MODELVIEW CONTROLLER Model Controller View X
  • 23. Amir Barylko MavenThought Inc. RESPONSIBILITIES •Model provides data •View provides visualization •Controllers provides behaviour •Controller communicates one to the other
  • 24. Amir Barylko MavenThought Inc. CONS •Hard to use •Hard to maintain •Lots of repetition
  • 25. Amir Barylko MavenThought Inc. MODELVIEWVIEWMODEL •Model provides data •View provides visualization •ViewModel provides one-to-one what the view needs •By convention uses binding to update
  • 26. Amir Barylko MavenThought Inc. KNOCKOUT.JS Intro Bindings Observables
  • 27. Amir Barylko MavenThought Inc. INTRO Simplify  dynamic  Javascript   UIs  by  applying  the  Model-­ View-­ViewModel  pattern knockoutjs.com
  • 28. Amir Barylko MavenThought Inc. SOME FEATURES • Free and open source (MIT License) • Pure Javascript • Small & lightweight • No dependencies • Supports all mainstream browsers • Good documentation
  • 29. Amir Barylko MavenThought Inc. DESCRIPTIVE BINDINGS • Use the html5 data-bind attribute • Can have multiple functions • They can be used for data, attributes, etc
  • 30. Amir Barylko MavenThought Inc. STEPS • Write a view model class • Write a view with special markup that references the view model • Apply the bindings in order to be parsed • ko.appplyBindings(new MovieLibraryViewModel())
  • 31. Amir Barylko MavenThought Inc. TEXT BINDING (1) • Value of the markup element • data-bind=‘text: name’
  • 32. Amir Barylko MavenThought Inc. FOR EACH (2) • Binds a collection to markup • data-bind=‘foreach: movies’
  • 33. Amir Barylko MavenThought Inc. CLICK EVENTS (3) • Indicate handlers for click • data-bind=‘click: handlerOnVm’
  • 34. Amir Barylko MavenThought Inc. HIDE ELEMENTS (4) • Visible binding hides markup • data-bind=‘visible: editTitle’ • What happened? Does the input shows? Why?
  • 35. Amir Barylko MavenThought Inc. OBSERVABLES (5) • Subscribe / Publish model • Notifies when the value changes • Updates markup • Triggers notifications
  • 36. Amir Barylko MavenThought Inc. OBSERVABLE VALUES (6) • ko.observable • Behaves like a function • If you want the value you need to “evaluate”
  • 37. Amir Barylko MavenThought Inc. VALUE • Binds the value of the markup for inputs • data-bind=‘value: title’
  • 38. Amir Barylko MavenThought Inc. COMPUTED (7) • Calculate values based on dependencies • ko.computed(fnToUse)
  • 39. Amir Barylko MavenThought Inc. OBSERVABLE ARRAYS (8) • Same idea as values • But are collections that notify events • No need to track adding and removing • The binding updates that for you
  • 40. Amir Barylko MavenThought Inc. SUBSCRIBETO EVENTS (9) • Each observable also notifies events • You can subscribe to trigger other calculations • For example, clear the fields every time before edit a new movie
  • 41. Amir Barylko MavenThought Inc. KNOCKOUT MAPPING • Easy to transform JSON data into observables • read from JSON with fromJS • export to JSON with toJS • customize your mapping to suit your needs
  • 43. Amir Barylko MavenThought Inc. BENEFITS • Short learning curve compared to other frameworks • Easy to implement two way binding • Supports binding templates • Custom bindings • Lots of documentation • Coffeescript adds OOP
  • 44. Amir Barylko MavenThought Inc. DRAWBACKS • The HTML may be harder to read • ViewModel scope can get out of hand • No particular structure enforced (is it really a drawback?) • Other?
  • 45. Amir Barylko MavenThought Inc. RESOURCES • Contact me: amir@barylko.com, @abarylko • Download: http://www.orthocoders.com/presentations • coffescript.org • knockoutjs.com