160 likes | 174 Views
Learn how Knockout simplifies data/UI synchronization using the MVVM pattern and explore object literals in JavaScript. Discover examples, tutorials, and ways to handle objects, constructors, inheritance, and more.
E N D
JS301 Week 4 Knockout and Objects
Knockout • Knockout makes it easier to keep your data and your UI in sync with each other. • It is based on the MVVM (Model, View, ViewModel) pattern...
Model, View, ViewModel (MVVM) ViewModel This is the data which is linked to the user interface 3 1 4 2 View This is the HTML and JavaScript which make up your UI.. Model This is the native data stored by your application.
Model, View, ViewModel (MVVM) • Data is "imported" from your native model to the ViewModel • When data is updated it is "exported" back to your native model • When data in the ViewModel is updated, those updates are automatically written to the UI • When data in the UI is updated, those changes are automatically written to the ViewModel
Model, View, ViewModel (MVVM) • The simplest examples skip the Model and just use a ViewModel and View • Practical use does require a Model to store values
Knockout Examples • Hello World: http://jsfiddle.net/zymsys/7vTBT/ • Click Counter: http://jsfiddle.net/zymsys/JryVC/ • Simple List: http://jsfiddle.net/zymsys/QLjAW/
Learning Knockout • There are more examples here:http://knockoutjs.com/examples/ • And an excellent interactive tutorial here:http://learn.knockoutjs.com/
Basic Object Literals • The easiest way to create an object is with an object literal:http://jsfiddle.net/zymsys/nUS7k/ • No class definitian / object instantiation!
Object Constructors • You can make similar objects with a constructor function:http://jsfiddle.net/zymsys/PVRyS/ • By convention, constructors always start with an upper case character, and other functions always start with a lower case character.
Objects & the importance of New • If we don't use new for constructors we run into big problems:http://jsfiddle.net/zymsys/zVfE4/ • You can write constructors which fix this:http://jsfiddle.net/zymsys/KgHhN/
Objects: This and That • In most OO languages, this refers to the running instance of a class. • JavaScript doesn't have classes, but it does have "this". • This doesn't always work as expected!http://jsfiddle.net/zymsys/6Dq2W/
Objects: This and That • Depending on the value of 'this' can cause problems. • You can get around 'this' problem by creating your own copy of 'this':http://jsfiddle.net/zymsys/nthJA/ • The copy is commonly called 'that' or 'self'.
Objects: Private Variables Using private variables can get around the this / that problem completely:http://jsfiddle.net/zymsys/E2pUX/
Objects: Inheritance Objects can inherit behaviours from other objects: • http://jsfiddle.net/zymsys/NVCyU/(confusing) • http://jsfiddle.net/zymsys/6uCgk/(less confusing?)
Objects: Parts / Traits Instead of inheriting functionality you can add "Parts" or "Traits" to any object: http://jsfiddle.net/zymsys/xpgQA/
Where to go from here? • Experiment with consoles or jsfiddle.net • Tutorials (JavaScript, jQuery, knockout.js) • Books (in order of difficulty): • Head First JavaScript • JavaScript: The Definitive Guide • JavaScript Patterns • Test-Driven JavaScript Development • JavaScript: The Good Parts (Difficult!) • Real world projects • Framework Source (jQuery, MooTools, YUI)