backbone-metal
v0.6.2
Published
Classes, Mixins, Errors, and more.
Downloads
2,258
Readme
Backbone Metal
Classes, Mixins, Errors, and more.
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
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.