# arale

> Arale Class and Events

Latest version **0.2.0** (published 2012-12-19) · 0 weekly downloads

## Install

```sh
npm install arale
pnpm add arale
yarn add arale
bun add arale
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.2.0 |
| Published | 2012-12-19 |
| First published | 2012-12-08 |
| Weekly downloads | 0 |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Arale Team |
| Maintainers | lepture, lifesinger, popomore |
| Keywords | class, events, OOP |

## Links

- npm: https://www.npmjs.com/package/arale
- Repository: https://github.com/lepture/arale
- npm.io page: https://npm.io/package/arale

## Alternatives

- [async-exit-hook](https://npm.io/package/async-exit-hook.md) — 3.7M weekly downloads
- [evnty](https://npm.io/package/evnty.md) — 7.2K weekly downloads
- [eleventy-plugin-asciidoc](https://npm.io/package/eleventy-plugin-asciidoc.md) — 3.5K weekly downloads
- [@jswork/next-get2get](https://npm.io/package/@jswork/next-get2get.md) — 945 weekly downloads
- [@dashersw/axon](https://npm.io/package/@dashersw/axon.md) — 934 weekly downloads

## Recent versions

- 0.2.0 (latest) — 2012-12-19
- 0.1.0 — 2012-12-08

## README

# Arale

This is a fork of [arale.class](http://aralejs.org/class/) and [arale.events](http://aralejs.org/events/). The core of arale projects.

-----------------

[![Build Status](https://travis-ci.org/lepture/arale.png?branch=master)](https://travis-ci.org/lepture/arale)

The Class and Events of [Arale](http://aralejs.org/) is very easy to use, and stable now. Test cases with [97% coverage](http://lab.lepture.com/arale/coverage), and you don't have to worry about the 3% left.


## Class

Read it in Chinese at [Arale.Class](http://aralejs.org/class/).


### create

`Class.create([parent], [properties])` create a new class:

```javascript
/* pig.js */
var Class = require('arale').Class;

var Pig = Class.create({
    initialize: function(name) {
        this.name = name;
    },

    talk: function() {
        console.log('I am' + this.name);
    }
});

module.exports = Pig;
```

### extend

Every class created by `Class.create` has an `extend` method:

```javascript
/* red-pig.js */
var Pig = require('./pig');

var RedPig = Pig.extend({
    initialize: function(name) {
        RedPig.superclass.initialize.call(this, name);
    },

    color: 'red'
});

module.exports = RedPig;
```

### implement

You can also mixin other classes:

```javascript
/* flyable.js */
exports.fly = function() {
    console.log('I am flying');
};
```

```javascript
/* flyable-red-pig.js */
var RedPig = require('./red-pig');
var Flyable = require('./flyable');

var FlyableRedPig = RedPig.extend({
    Implements: Flyable,

    initialize: function(name) {
        FlyableRedPig.superclass.initialize.call(this, name);
    }
});

module.exports = FlyableRedPig;
```

The other way of mixin:

```javascript
/* flyable-red-pig-extension.js */
var FlyableRedPig = require('./flyable-red-pig');

FlyableRedPig.implement({
   swim: function() {
       console.log('I can swim');
   }
});
```

### Class

Transfrom a function to class:

```javascript
function Animal() {
}
Animal.prototype.talk = function() {};

var Dog = Class(Animal).extend({
    swim: function() {}
});
```

## Events

Read it in Chinese at [Arale.Events](http://aralejs.org/events/).

### How to

There are two ways of implementing events:

```javascript
var Events = require('arale').Events;

var object = new Events();
object.on('expand', function() {
    console.log('expanded');
});

object.trigger('expand');
```

The other way:

```javascript
var Events = require('arale').Events;

function Dog() {
}
Events.mixTo(Dog);

Dog.prototype.sleep = function() {
    this.trigger('sleep');
};

var dog = new Dog();
dog.on('sleep', function() {
    console.log('the dog is sleeping');
});

dog.sleep();
```

### on

`object.on(event, callback, [context])`

You can pass a context to change the **this**.

```javascript
post.on('saved', callback, that);
```

There is an ``all`` event:

```javascript
proxy.on('all', function(eventName) {
    object.trigger(eventName);
});
```

### off

`object.off([event], [callback], [context])`

```javascript
// remove the onChange callback of change event
object.off('change', onChange);

// remove all callbacks of change event
object.off('change');

// remove all onChange callbacks of every event
object.off(null, onChange);

// remove all events for the context
object.off(null, null, context);

// remove all events of the object
object.off();
```


### trigger

`object.trigger(event, [*args])`

```javascript
var obj = new Events();

obj.on('x y', fn);

// equals:
obj.on('x').on('y');
```

---
_Source: https://npm.io/package/arale · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
