Skip to content
This repository has been archived by the owner on Nov 17, 2020. It is now read-only.

Latest commit

 

History

History
106 lines (75 loc) · 2.73 KB

README.md

File metadata and controls

106 lines (75 loc) · 2.73 KB

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 [email protected]: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.