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

ampersand-dependency-mixin

v0.2.3

Published

A mixin that provides dependency management for Ampersand.js and Backbone

Downloads

4

Readme

ampersand-dependency-mixin

A mixin that provides dependency management for Ampersand.js and Backbone.

Based on a talk I presented at Backbone Conf.

wercker status

Usage

Install with npm:

$ npm install --save ampersand-dependency-mixin

Extend the mixin into a constructor:

var deps = require('ampersand-dependency-mixin');
var Model = require('ampersand-model');

var MyModel = Model.extend(deps, {
  dependencies: ['config']

  initialize: function(options) {
    this.attachDeps(options)
  }
});

To check for dependencies on a class, declare a dependencies array and call this.attachDeps(options).

When attachDeps is run it checks the object passed to it for keys matching any dependencies, throwing an error if they're missing and attaching them if present.

var model = new MyModel({ config: { ... } });
model.config
// -> { ... }

var shouldThrow = new MyModel();
// -> Error: 'Missing required dependencies: `config`'

Custom Error Messages

Custom error messages can be provied by declaring dependencies as an object:

var CustomMsg = Model.extend(deps, {
  dependencies: {
    config: 'App Config'
  }
});

new CustomMsg();
// -> Error: 'Missing required dependencies: App Config'

Inheriting Dependencies

Dependencies can also be provided as a function that retuns an object or an array, which can be useful for sharing common dependencies through inheritance.

var BaseModel = Model.extend(deps, {
  dependencies: function() {
    return ['config']
  },
  initialize: function(options) {
    this.attachDeps(options);
  }
});

new BaseModel();
// -> Error: 'Missing required dependencies: `config`'

var MyModel = BaseModel.extend({
  dependencies: function() {
    var baseDeps = BaseModel.prototype.dependencies.call(this);
    return baseDeps.concat(['user'])
  }
});

new MyModel();
// -> Error: 'Missing required dependencies: `config`, `user`'

Overwriting properties

By default, attachDeps will not overwrite existing properties, but it will still enforce that they're provided in the options object. This behavior works well with classes that already attach options for you automatically (such as the model or collection options with Backbone.View,) but where you still want to enforce that a given option has been supplied.

Use the overwrite: true option to overwrite any pre-existing values:

var UserModel = View.extend({
  dependencies: ['config'],

  config: { apiKey: 123 },

  initialize: function(options) {
    this.attachDeps(options, { overwrite: true });
  }
});

var view = new UserModel({
  config: { apiKey: 321 }
});

view.config
// -> { apiKey: 321 }