backbone-metal

0.6.2 • Public • Published

Backbone Metal

Classes, Mixins, Errors, and more.

Travis Status Code Climate Score Coverage Dependency Status

Usage

Classes

Classes are objects that you can instantiate with new Class(). You can also create a subclass from any existing class by calling its extend() method.

import {Class} from 'backbone-metal';
 
const MyClass = Class.extend({
  initialize(options) {
    console.log(`Created! ${options.greeting} ${options.subject}!`);
  }
});
 
let myClass = new MyClass({
  greeting: 'Hello',
  subject: 'World'
});
// >> Created! Hello World!

Mixins

When working with multiple classes, sometimes you want to share functionality between them. You can easily do this by creating a new Mixin and adding it to all the classes that need it.

import {Mixin, Class} from 'backbone-metal';
 
const MyMixin = new Mixin({
  alert(message) {
    console.log(`Alert! ${message}`);
  }
});
 
const MyClass = Class.extend({
  initialize() {
    this.alert('You have successfully used a Mixin!');
  }
});
 
MyClass.mixin(MyMixin);
 
let myClass = new MyClass();
// >> Alert! You have successfully used a Mixin!

Super

When working with subclasses, sometimes you want to modify one of the parent's methods and then calling the parent method inside. You can easily do this by calling _super.

import {Class} from 'backbone-metal';
 
const FirstClass = Class.extend({
  initialize() {
    console.log('First Class checking in!');
  }
});
 
const SecondClass = FirstClass.extend({
  initialize() {
    this._super();
    console.log('Second Class checking in!');
  }
});
 
let secondClass = new SecondClass();
// >> First Class checking in!
// >> Second Class checking in!

Contributing

Getting Started

Fork and clone this repo.

git clone git@github.com:marionettejs/backbone-metal.git && cd backbone-metal

Make sure Node.js and npm are installed.

npm install

Running Tests

npm test

===

© 2014 James Kyle. Distributed under ISC license.

Readme

Keywords

none

Package Sidebar

Install

npm i backbone-metal

Weekly Downloads

234

Version

0.6.2

License

ISC

Last publish

Collaborators

  • thejameskyle