Simple fast js to ko and ko to js mapper
JavaScript CSS HTML
Switch branches/tags
Nothing to show
Latest commit 3f47485 Aug 20, 2015 @arj03 Merge pull request #10 from arj03/revert-9-option-to-wrap-objects-in-…
…observables

Revert "Added an option to wrap full objects into observable"
Permalink
Failed to load latest commit information.
tests
.gitattributes Initial commit Jun 7, 2012
.gitignore Add helper function to simplify normal use case Jun 8, 2012
README.md
knockout.wrap.1.0.0.nupkg
knockout.wrap.1.0.1.nupkg Add knockoutjs dependency Jul 3, 2013
knockout.wrap.js Merge pull request #10 from arj03/revert-9-option-to-wrap-objects-in-… Aug 20, 2015

README.md

Knockout wrap

Motivation

The performance of ko.mapping.fromJS(JSObject) is very slow. A simple test wrapping 500 simple elements takes 700ms in firefox. This seems to be a known issue, that is still there.

Futhermore I don't have the need for ko.mappings ability to find out what needs to be updated in already mapped objects. This shaves off a huge chunk of the complexity.

Solution

A new plugin knockout.wrap with almost the same interface as ko.mapping (fromJS, fromJSON, toJS and toJSON). The plugin can do simple mappings from JS to observables and the other way again. Because the code is much simpler it is super fast:

ko.mapping.fromJS(500JSObjects): 737ms
ko.wrap.fromJS(500JSObjects): 16ms

ko.mapping.toJS(500WrappedObjects): 22ms
ko.wrap.toJS(500WrappedObjects): 5ms 

Usage

Basic use cases:

var observable = ko.wrap.fromJS(JSObjects);

var jsObjects = ko.wrap.toJS(observable);

Please note that if you are updating an observable, then it's preferable to use ko.wrap.updateFromJS(observable, jsObject).

ko.mapping can attach computed functions while it is wrapping. ko.wrap can do this as well, the syntax is a bit different:

function populateArray()
{
    var t = {elements: []};

    for (var i = 0; i < 500; ++i)
       t.elements.push({id: i, name: "hello" + i});

    return t;
}

var computedFunctions = {
    "/elements": function(e) {
        e.nameLength = ko.computed(function() {
            return e.name().length;
        }, e);
        return e;
    }
};

ko.wrap.fromJS(t, computedFunctions);

License

MIT license - http://www.opensource.org/licenses/mit-license.php