Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
JavaScript
Failed to load latest commit information.
spec
src
.editorconfig Simplification based on generator
.gitignore
.jshintrc Update from generator
.travis.yml Simplification based on generator
Gruntfile.js Simplification based on generator
README.md Simplification based on generator
bower.json
karma.conf.js Simplification based on generator
package.json grunt-bower-requirejs required

README.md

Number binding for knockout

Build status

Usage

HTML

<input data-bind="number: myobs" >

JS

ko.applyBindings({
    myobs : ko.observable()
});

This binding is written in AMD. It returns the binding object. It will attach itself to knockout.bindingHandlers.number once required for the first time. This can be overridden with a config section in your requirejs config like shown below.

requirejs.config({
    config: {
        'bower_components/knockout.bindingHandlers.number/src/number': {
            name: 'someOtherName'
    }
});

Behaviour

If the observable on init contains a string then the value inserted into the input will also initially be stripped of any non digit characters.

When the change event fires on the input, all non digits are stripped and reflected back to the observable. If the original value was a string, the reflected value will also be a string. Empty string when input is empty.

Otherwise it will be converted to a number and put in the observable. If no digits in the input the value will be undefined.

valueUpdate

This binding supports the valueUpdate binding. If for instance you use afterkeydown the value of the input will immediately be stripped of non digits and updated to the observable. The input will still have the value you typed as long as it has focus.

Demo

There is a demo at http://mokkabonna.github.io/knockout.bindingHandlers.number

Dependencies

  • knockout
  • jquery

For accurate versions check bower.json

Contributing

Clone, then run (assuming you have node)

npm install & bower install

You can now use grunt develop for a ready made watch task for development. Tests, linting..

grunt develop
Something went wrong with that request. Please try again.