Skip to content

Commit

Permalink
Adding JS2.Obeservable
Browse files Browse the repository at this point in the history
  • Loading branch information
jeffsu committed Oct 21, 2010
1 parent 26a8dea commit 0f567ec
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 7 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG
@@ -1,3 +1,6 @@
## Version 0.1.8
* Adding JS2.Observable module to boostrap

## Version 0.1.7
* Support for Ruby 1.9.x

Expand Down
6 changes: 2 additions & 4 deletions README.md
@@ -1,8 +1,6 @@
[JS2](http://github.com/jeffsu/js2) - Friendly Cross Browser Object Oriented Javascript
=======================================================================================

Friendly Cross Browser Object Oriented Javascript
-------------------------------------------------
JS2 is a way to write classes in a more familiar way for programmers who are used to syntax in languages such as: Java, Ruby, C++, etc...
The main motivation is to provide a clean and a less verbose syntax for creating classes, inheritance and modules in Javascript. JS2 plays
nicely with most of the popular frameworks including: jQuery and prototype. It also provides some syntactic sugar on the repetative aspects
Expand Down Expand Up @@ -39,8 +37,8 @@ Things You need to run JS2
* RubyInline
* haml (optional)

Quick Start
-----------
Quick Start Guide
-----------------
Create a js2 file (foo.js2):
class Person {
function speak() {
Expand Down
2 changes: 1 addition & 1 deletion Rakefile
Expand Up @@ -2,7 +2,7 @@ require 'rubygems'
require 'rake'
require 'echoe'

JS2_VERSION = '0.1.7'
JS2_VERSION = '0.1.8'
Echoe.new('js2', JS2_VERSION) do |p|
p.description = ""
p.url = "http://github.com/jeffsu/js2"
Expand Down
4 changes: 2 additions & 2 deletions js2.gemspec
Expand Up @@ -2,11 +2,11 @@

Gem::Specification.new do |s|
s.name = %q{js2}
s.version = "0.1.7"
s.version = "0.1.8"

s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
s.authors = ["Jeff Su"]
s.date = %q{2010-10-08}
s.date = %q{2010-10-21}
s.default_executable = %q{js2}
s.description = %q{}
s.email = %q{me@jeffsu.com}
Expand Down
47 changes: 47 additions & 0 deletions lib/js2/util/js2bootstrap.js2
Expand Up @@ -399,3 +399,50 @@ class JS2.App.JQuery extends JS2.App {
}
}

module JS2.Observable {
function addListener (namespace, funct) {
this._initializeObservable(namespace);

var id = this._listenerCount++;
var chain = this._listeners[namespace];
var index = chain.length;
var info = [ funct, id, namespace ];

this._listenerLookup[id.toString()] = info;
chain.push(info);
}

function trigger () {
if (! this._listeners) return;

var namespace = arguments[0];
var chain = this._listeners[namespace];
if (! chain) return;

var args = [];
for (var i=1; i<arguments.length; i++) {
args.push(arguments[i]);
}

if (chain) {
foreach (var ele in chain) {
ele[0].apply(this, args);
}
}
}

private

function _initializeObservable (namespace) {
if (! this._listeners) {
this._listeners = {};
this._listenerLookup = {};
this._listenerCount = 1;
}

if (! this._listeners[namespace]) {
this._listeners[namespace] = [];
}
}
}

0 comments on commit 0f567ec

Please sign in to comment.