Book Image

Mastering KnockoutJS

By : Timothy Moran
Book Image

Mastering KnockoutJS

By: Timothy Moran

Overview of this book

Table of Contents (16 chapters)
Mastering KnockoutJS
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Binding to plain JavaScript objects


The last part of Durandal that we are going to cover is the observable plugin, which allows data binding to use normal viewmodel properties as observable objects by converting them under the hood.

The observable plugin uses JavaScript getters and setters created with defineProperty, which is part of the ECMAScript 5 specification. Only modern browsers support this feature, so if your application needs to work in Internet Explorer 8, the observable plugin will not work.

Using the observable plugin removes one of the most common complaints from Knockout's syntax: the parentheses. All of the property access is executed using plain syntax, whether reading or assigning values:

function Contact() {
  var self = this;
  self.firstName = '';
  self.lastName = '';
  self.reset = function() {
    self.firstName = '';
    self.lastName = ''
  };
};

var viewmodel = new Contact();

//HTML
<input data-bind="value: firstName" />
<input data-bind="value: lastName...