GithubHelp home page GithubHelp logo

pwfoo / asevented Goto Github PK

View Code? Open in Web Editor NEW

This project forked from mkuklis/asevented

0.0 1.0 0.0 107 KB

Micro event emitter which provides the observer pattern to JavaScript object.

JavaScript 93.87% HTML 6.13%

asevented's Introduction

asEvented

build status

Description

asEvented is a micro event emitter which provides the observer pattern to JavaScript object. It works in the browser and server (node.js). The code follows a functional mixins pattern described by Angus Croll a-fresh-look-at-javascript-mixins.

Usage

In browser include single JavaScript file:

<script src="asevented.js"></script>

On server install asEvented via npm first:

npm install asEvented

and then include it in your project with:

var asEvented = require('asEvented');

##Example Usage Define the constructor which will make use of asEvented:

function Model() {
  this.id = 0;
}

Add (mixin) asEvented functionality to Model with:

asEvented.call(Model.prototype);

model has now access to trigger, bind/on, one and unbind/off functions

Add some functions to Model:

Model.prototype.setId = function (id) {
  this.id = id;
  this.trigger('change:id', id); // Model has now access to trigger
}

Create a new object and bind to change:id event:

var model = new Model();
model.bind('change:id', function (id) {
  console.log('id changed to: ' +  id);
});

Set a few model ids in order to trigger change:id event:

model.setId(1);
model.setId(2);
model.setId(3);

A single handler can be bound to multiple events by separating the events by spaces, though it will be ignorant of the triggering event:

model.bind('change:id change:pass', function() {
  console.log('Event called.');
});

And multiple events can be unbound in one call:

model.unbind('onload onready');

##Contributors

##License:

(The MIT License)

Copyright (c) 2015 Michal Kuklis

asevented's People

Contributors

davidjamesstone avatar garex avatar knpwrs avatar michaelrhodes avatar mkuklis avatar mpdaugherty avatar nazomikan avatar plalx avatar rk avatar vinicius73 avatar vojtechkral avatar whiteinge avatar zspencer avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.