Skip to content
Maxime LUCE edited this page Apr 10, 2015 · 2 revisions

Introduction

KoUnderscore can be used in many contexts ; AMD, CommonJS or as a global.

AMD

Configure require.js or almond

// app/main.js
require.config({
    paths: {
        knockout: 'path/to/knockout',
        underscore: 'path/to/underscore',
        kounderscore: 'path/to/kounderscore
    }
});

Use KoUnderscore in your view models

// app/viewmodel1.js
define(["kounderscore"], function(ko_) {
    var obsArray = ko.observableArray([....]);

    var regularArray = obsArray.filter(function(item) { ... });
    var computedArray = obsArray._filter(function(item) { ... });

    var customComputedArray = ko.computed(function() { return obsArray(); });
    ko_.addToSubscribable(customComputedArray);

    regularArray = customComputedArray .filter(function(item) { ... });
    computedArray = customComputedArray ._filter(function(item) { ... });
});

CommonJS

Load KoUnderscore

In order to have access to ko.observableArray underscore methods you should load KoUnderscore before creating ko.observableArray.

var ko_ = require("kounderscore");

Use it in your own module

var obsArray = ko.observableArray([....]);

var regularArray = obsArray.filter(function(item) { ... });
var computedArray = obsArray._filter(function(item) { ... });

var customComputedArray = ko.computed(function() { return obsArray(); });
ko_.addToSubscribable(customComputedArray);

regularArray = customComputedArray .filter(function(item) { ... });
computedArray = customComputedArray ._filter(function(item) { ... });

Global

Include KoUnderscore in its dependencies in your page

<script type="text/javascript" src="path/to/knockout.js"></script>
<script type="text/javascript" src="path/to/underscore.js"></script>
<script type="text/javascript" src="path/to/kounderscore.js"></script>

Use it in your code

<script type="text/javascript">
    var obsArray = ko.observableArray([....]);

    var regularArray = obsArray.filter(function(item) { ... });
    var computedArray = obsArray._filter(function(item) { ... });

    var customComputedArray = ko.computed(function() { return obsArray(); });
    ko_.addToSubscribable(customComputedArray);

    regularArray = customComputedArray .filter(function(item) { ... });
    computedArray = customComputedArray ._filter(function(item) { ... });
</script>

Clone this wiki locally