An introduction to knockout.js

22
An introduction to Knockout.js Emanuele Delbono @emadb

description

An introduction to the features of knockout.js

Transcript of An introduction to knockout.js

Page 1: An introduction to knockout.js

An introduction to Knockout.js

Emanuele Delbono@emadb

Page 2: An introduction to knockout.js

Everyone loves jumping in muddy puddles

Page 3: An introduction to knockout.js

Everyone loves jumping in muddy puddleswriting javascript code

Page 4: An introduction to knockout.js

We write tons of javascript code

Page 5: An introduction to knockout.js

I am

Web Developer in CodicePlastico. I have fun writing web apps in C#, Javascript and Ruby.

Page 6: An introduction to knockout.js

Agenda

• The MVVM Pattern

• Welcome to Knockout

• Binding basics

• The features

• Conclusions

Page 7: An introduction to knockout.js

Presentation patterns

Page 8: An introduction to knockout.js

The MVVM pattern

Model ViewModel View

Page 9: An introduction to knockout.js
Page 10: An introduction to knockout.js
Page 11: An introduction to knockout.js
Page 12: An introduction to knockout.js

ViewModel has methods too!

Page 13: An introduction to knockout.js

What is Knockoutjs?

“Knockout is a JavaScript library that helps you to create rich, responsive display and editor user interfaces with a clean underlying data model. Any time you have sections of UI that update dynamically [...], KO can help you implement it more simply and maintainably.”

http://knockoutjs.com

Page 14: An introduction to knockout.js

Key features

• Declarative binding

• Automatic UI refresh

• Templating

• Dependency tracking

Page 15: An introduction to knockout.js

Bidirectional Data Binding

ViewModel View

Page 16: An introduction to knockout.js

Binding

• data-bind attributes in HTML

• ko.observable() for the properties

• ko.applyBindings() to activate bindings

Page 17: An introduction to knockout.js

Binding

var viewModel = { message: ko.observable('Welcome')}

<div data-bind="text: message"></div>

Page 18: An introduction to knockout.js

Observable is a function!

Don’t do this:

viewModel.message = 'hi'

Do this:

viewModel.message('hi')

Page 19: An introduction to knockout.js

Code time

Page 20: An introduction to knockout.js

Recap

• Simple binding

• Array binding

• Computed properties

• Event binding

• Conditionals

• Templates

• Subscriptions

• Messagging

• Contexts

• Custom bindings

• Virtual elements

Page 21: An introduction to knockout.js

Conclusions

• KO is small focused on binding

• Very low learning curve

• Focused on databinding (no routing, model synchronization, etc...)

• Lots documentation

• Could be hard on big pages