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

The binding expression parser


The expressions written in data-bind attributes are not truly JavaScript or JSON, though they look very similar. Knockout has its own parser to convert these attributes into JavaScript. Say you write a data-bind attribute like this one:

data-bind="value: name, visible: showName"

Then, the binding provider's job is to return an object like this:

{
  value: function() { return name; },
  visible: function() { return showName; }
}

The default binding provider does this using the ko.expressionRewriting module, which is responsible for calling binding preprocessors and returning a JSON-esque string. Internally, this is done using regex to parse the full attribute into a key/value pair array. This might sound messy, but it gets the job done. That being said, even for an under the hood look, the details are not very relevant to Knockout, as the parsing is general purpose. If you are still curious, the code is located at https://github.com/knockout/knockout/blob/master...