Giter Site home page Giter Site logo

kontainer's Introduction

Kontainer

Kontainer is a dependency injection companion for Knockout.js. It allows view models to consume other JavaScript modules and services without being coupled to the global object. A nice side effect is that view models become more naturally testable.

The array based injection pattern (see examples below) takes inspiration from the minification-safe form of DI found in AngularJS. This is the only form supported by Kontainer.

Example Usage

// Register the Kontainer component loader with ko.
// This should be done only once during application bootstrapping.
ko.components.loaders.unshift(kontainer.loader);

// Register some singleton values with Kontainer.
kontainer.registerValue('name', 'injected value');

// Register module / service factories with their own dependencies.
kontainer.registerFactory('service', ['name', function (name) {
    var reverseName = name.split('').reverse().join('');

    return {
        reverseName: reverseName
    };
}]);

// Finally register a component with ko.
// As long as the view model is an array, Kontainer will take over resolution.
ko.components.register('some-component', {
    viewModel: ['service', function (service) {
        return {
            name: ko.observable(service.reverseName)
        };
    }],
    template: '<p data-bind="text: name"></p>'
});

To consume the params and componentInfo arguments normally passed to a view model function, inject them as named dependencies in any order:

ko.components.register('some-component', {
    viewModel: ['componentInfo', 'service', 'params', function (componentInfo, service, params) {
        return {
            // Do something with componentInfo, service and params
        };
    }],
    template: '<p data-bind="text: name"></p>'
});

Caveats

Kontainer could be described as a fully functional experiment. It works exactly as designed, but it's usefulness evaporates if Knockout.js is used with an AMD loader (highly recommended).

Knockout.js already plays extremely well with Require.js, which provides a nice level of decoupling and its own form of dependency injection. Since test-time injection libraries for Require.js already exist - such as Squire.js - adding Kontainer on top would likely just add noise to a project.

kontainer's People

Contributors

benquarmby avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar

Forkers

viktor-nikolaev

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.