Book Image

Knockout.JS Essentials

Book Image

Knockout.JS Essentials

Overview of this book

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

Extending the product model


To validate our models using the Knockout Validation library, we are going to extend our model's attributes. Extenders are a basic feature of Knockout. Using extenders we can add some properties to our observables to increase their behavior. For more information on extenders, please refer to the following link:

http://knockoutjs.com/documentation/extenders.html

We are going to extend our product model with some properties that will allow us to validate data by following these steps:

  1. Go to the models/Product.js file.

  2. Update the name field. It should have at least three letters and should contain just letters, numbers, and dashes:

    _name = ko.observable(name).extend({
      required: true,
      minLength: 3,
      pattern: {
        message: 'Hey this doesn\'t match my pattern',
        params: '^[A-Za-z0-9 \-]+$'
      }
    })
  3. Update the price to allow just numbers, and set a range (maximum and minimum values) for it:

    _price = ko.observable(price).extend({
      required: true,
      number:true,
      min...