Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

knockout-mapping

crissdev46.2kMIT2.6.0

Knockout Mapping plugin

knockout, mapping, knockout-mapping, knockout.mapping, plugin, ko

readme

knockout.mapping

Build Status npm version

Object mapping plugin for Knockout

Documentation

Official documentation here.

Install

Bower

bower install bower-knockout-mapping --save-dev

NPM

npm install knockout-mapping --save

Quick Start


var data = {
    email: 'demo@example.com',
    name: 'demo',
    addresses: [
        { type: 'home', country: 'Romania', city: 'Cluj' },
        { type: 'work', country: 'Spain', city: 'Barcelona' }
    ]
};

// Create a view model from data
var viewModel = ko.mapping.fromJS(data);

// Now use the viewModel to change some values (properties are now observable)
viewModel.email('demo2@example.com');
viewModel.name('demo2');
viewModel.addresses()[0].city('Bucharest');

// Retrieve the updated data (as JS object)
var newData = ko.mapping.toJS(viewModel);

// newData now looks like this
{
  email: 'demo2@example.com',
  name: 'demo2',
  addresses: [
    { type: 'home', country: 'Romania', city: 'Bucharest' },
    { type: 'work', country: 'Spain', city: 'Barcelona' }
  ]
}

Run this example in JSFiddle.

Test

Continuous Integration tests are done with Travis, and the associated Gulp task is test-ci. For development test task is used, which runs the tests against the latest version of Knockout.

License

MIT

changelog

2.6.0 (2016-02-28)

  • Issue #124: Use hasOwnProperty to check for bucket existence
  • Small performance improvements e6c5631, f97d5cd, 57a0b56
  • Issue #96: Fix incorrect handling of properties with periods
  • Issue #205: Allow JSON.stringify parameters to be specified to ko.mapping.toJSON
  • Issue #9: Fix pure computeds are treated the same as deferred, not auto-evaluated after mapping

2.5.0 (2015-02-12)

  • Remove deprecated methods (updateFromJS and updateFromJSON)
  • Issue #1: Compatibility with Knockout 3.x may still be a problem
  • Issue #4: Library is not exported in ko namespace for CommonJS/Node

2.4.1 (2013-02-08)

  • Added mappedGet for observable arrays
  • Issue #134: Throttle issue using mapping
  • Issue #135: Why is custom update for observableArray firing twice when using mapping plugin?

2.4.0 (2013-02-04)

  • Removed asynchronous processing that was used to reset mapping nesting
  • Improved getType performance

2.3.5 (2012-12-10)

  • Issue #121: Added functionality so that explicit declared none observable members on a ViewModel will remain none observable after mapping

2.3.4 (2012-11-22)

  • Issue #114: Added new "observe" array to options

2.3.3 (2012-10-30)

  • Fixed issue #105, #111: Update callback is not being called
  • Fixed issue #107: String values in mapping cause infinite recursion in extendObject

2.3.2 (2012-08-20)

  • Fixed issue #86: Don't update properties on object with update callback

2.3.1 (2012-08-06)

  • Fixed issue #33: Create method in mappings receive meaningless options.parent for observableArray properties
  • Fixed issue #99: Updating throttled observable
  • Fixed issue #100: private variable leaks onto window object

2.3.0 (2012-07-31)

  • Added support for not mapping certain array elements (return "options.skip" from your create callback)
  • Fixed issue #91: "wrap" function makes computed writable
  • Fixed issue #94: Bug/problem with ignore argument in mapping.fromJS