npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

backbone.dependencyinjection

v0.1.2

Published

Dependency injection for Backbone

Downloads

2,550

Readme

Backbone Dependency Injection


Introduction

backbone.dependencyinjection plugin adds Dependency Injection to the Backbone

Download

Download 0.1.2

Usage

register service by calling register method:


var Collection = Backbone.Collection.extend();

var Model = Backbone.Model.extend({
    defaults: {
        key: 'value'
    }
});

var View = Backbone.View.extend({
    model: 'inject:mymodel', // injecting model
    collection: 'inject:mycollection' // injection collection
});

var Router = Backbone.Router.extend({
    initialize: function () {
        'use strict';
        var myModel = new MyModel();

        this.register('mycollection', MyCollection); //registering service 'mycollection'
        this.register('mymodel', myModel); //registering service 'mymodel'
        this.register('myview', MyView); //registering service 'myview'
    },
    
    routes: {
        '*action': 'defaultAction'
    },
    
    defaultAction: function () {
        this.defaultView = this.inject('myview');
        console.log(this.defaultView.model.get('key')); // 'value'
        console.log(this.defaultView.collection instanceof Collection); // true
    }
});

var router = new Router();

Factory

var Collection = Backbone.Collection.extend();
var View = Backbone.View.extend({
    collection: 'inject:mycollection'
});

var Router = Backbone.Router.extend({
    routes: {
        '*action': 'defaultAction'
    },

    initialize: function () {
        'use strict';
        var myModel = new MyModel();

        this.register('mycollection', MyCollection);
        this.register('collectionModel', CollectionModel);
        
        this.listenTo(Backbone.Injector, 'inject:mycollection', function (object) {
            var i = 10;

            while (i > 0) {
                object.service.add(this.factory('collectionModel', {idx: i}));
                i--;
            }
        });
    },

    defaultAction: function () {
        'use strict';
        this.defaultView = this.inject('myview');
        this.defaultView.collection.at(0).get('idx'); // 10
    }
});

router = new Router();

API

Backbone.Model, Backbone.Collection, Backbone.View and Backbone.Router are extended by the following methods:

inject

Injects registered service.

this.inject(serviceName, [serviceConstructorParams])

or in class constructor:

   ...
   propertyName: 'inject:serviceName'
   ...

if inject of the Class constructor is called for the first time a new instance of this Class will be created, next times it returns this instance from the cache.

example

var View = Backbone.View.extend({
    model: 'inject:mymodel',
    
    initialize: function () {
        this.collection = this.inject('mycollection');
    }
})

To use inject from constructor with require.js you should load Dependency injection plugin to the module in which you are going to use it:

example

define([
    'backbone',
    'backbone.dependencyinjection'
    ], function (Backbone) {
    'use strict';

    var ChildView;
    
    ChildView = Backbone.View.extend({
        model: 'inject:child-model',
        ....
    });

    return ChildView;  
        
});

register

this.register('serviceName', service)

service parameter can be either an object instance or a constructor. If service parameter is a constructor then an instance of the Class will be created on the first inject call

example

registers Class constructor:

var Router = Backbone.Router.extend({
    initialize: function () {
        this.register('mymodel', MyModel);
    }
});

registers object instance:

var Model = Backbone.Model.extend({
    initialize: function () {
        this.register('globalModel', this);
    }
})

factory

this.factory('serviceName', optionalParameters);

returns a new instance of registered service Class.

example

...
initialize: function () {
    this.childview0 = this.factory('myview');
    this.childview1 = this.factory('myview');
}
...

dropService

this.dropService('serviceName')

deletes service completely from repository

example

close: function () {
    this.dropService('globalModel')
}

Dependencies

Backbone > 1.0, underscore.js

License

MIT