Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

update to make it jsLint more happy. _ -> $

  • Loading branch information...
commit 56c057600c7a2f4537fbe1b1e7531da4c786e09a 1 parent 17b8b58
@uxder authored
Showing with 189 additions and 181 deletions.
  1. +157 −133 radio.js
  2. +32 −32 tests/core.js
  3. +0 −15 tests/test.js
  4. +0 −1  tests/tests.html
View
290 radio.js
@@ -1,145 +1,169 @@
-/**
- * Author: Scott Murphy twitter: @hellocreation, github: uxder
- * radio.js - The Chainable, Dependency Free Publish/Subscribe for Javascript
- */
+/**
+ radio.js - Chainable, Dependency Free Publish/Subscribe for Javascript
-(function(global) {
- "use strict";
+ Author: Scott Murphy 2011
+ twitter: @hellocreation, github: uxder
- radio._ = {
- version: '0.0.1',
- channelName: "",
- channels: [],
- /**
- * Broadcast (publish)
- * Based on the current channelname set by the user, iterate through all listeners and send messages
- * @param arguments of the message can send unlimited parameters
- * @return [this] this returns self for chaining
- * @example
- * basic usage
- * radio('channel1').broadcast('my message'); //will immediately run myFunction
- * send an unlimited number of parameters
- * radio('channel2').broadcast(param1, param2, param3 ... );
- */
- broadcast: function() {
- var i, c = this.channels[this.channelName],
- l = c.length,
- listener, callback, context;
- //iterate through this channel and run each listener
- for (i = 0; i < l; i++) {
- //save the current listener into local var for performance
- listener = c[i];
+ Permission is hereby granted, free of charge, to any person
+ obtaining a copy of this software and associated documentation
+ files (the "Software"), to deal in the Software without
+ restriction, including without limitation the rights to use,
+ copy, modify, merge, publish, distribute, sublicense, and/or sell
+ copies of the Software, and to permit persons to whom the
+ Software is furnished to do so, subject to the following
+ conditions:
- //if listener was an array, set the callback and context.
- if ((typeof(listener) === 'object') && (listener.length)) {
- callback = listener[0];
- //if user sent it without a context, set the context to the function
- context = (listener[1]) ? listener[1] : window;
- }
+ The above copyright notice and this permission notice shall be
+ included in all copies or substantial portions of the Software.
- //run the listener
- callback.apply(context, arguments);
- }
- return this;
- },
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
+ OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
+ HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+ WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+ OTHER DEALINGS IN THE SOFTWARE.
+ */
- /**
- * Create the channel if it doesn't exist and set the current channel/event name
- * @param[string] name the name of the channel
- * @return[this] returns self for chaining
- * @example
- * radio('channel1');
- */
- channel: function(name) {
- var c = this.channels;
- //create a new channel if it doesn't exists
- if (! (c[name])) c[name] = [];
- this.channelName = name;
- return this;
- },
- /**
- * Add Listener to channel (subscribe)
- * Take the arguments and add it to the this.channels array.
- * @param[callback|array] arguments list of callbacks or arrays[callback, context] separated by commas
- * @return [this] this returns self for chaining
- * @example
- * //basic usage
- * var callback = function() {};
- * radio('channel1').add(callback); //will run callback on
- *
- * radio('channel1').add(callback, callback2, callback3 ...); //you can add an endless amount of callbacks
- *
- * //adding callbacks with context
- * radio('channel1').add([callback, context],[callback1, context], callback3);
- */
- add: function() {
- var a = arguments,
- c = this.channels[this.channelName],
- i, l = a.length, p, ai = [];
+(function (global) {
+ "use strict";
- //run through each arguments and add it to the channel
- for (i = 0; i < l; i++) {
- ai = a[i];
- //add accepts either an array (fucntion, context) or just the function.
- //if the user send just a function, wrap the fucntion in an array [function]
- p = (typeof(ai) === "function") ? [ai] : ai;
- if ((typeof(p) === 'object') && (p.length)) c.push(p);
- }
- return this;
- },
+ /**
+ * Main Wrapper for radio.$ and create a function radio to accept the channelName
+ * @param {String} channelName topic of event
+ */
+ function radio(channelName) {
+ radio.$.channel(channelName);
+ return radio.$;
+ }
- /**
- * Remove Listener from channel (subscribe)
- * Take the arguments and add it to the this.channels array.
- * @param[function] callback list of callbacks to be removed separated by commas
- * @return [this] this returns self for chaining
- * @example
- * //basic usage
- * radio('channel1').remove(callback); //will remove callback from channel1
- * radio('channel1').remove(callback, callback2, callback3 ...); //you can remove as many callbacks as you want
- *
- * //callbacks with context
- * radio('channel1').add([callback, context]).remove(callback); //just remove the callback
- */
- remove: function() {
- var a = arguments,
- i, j,
- c = this.channels[this.channelName],
- l = a.length,
- cl = c.length,
- offset = 0,
- jo;
- //loop through each argument
- for (i = l; i--;) {
- //reset variables that change as the channel array items are removed
- offset = 0;
- cl = c.length;
- //loop through the channel
- for (j = 0; j < cl; j++) {
- jo = j - offset;
- //if there is a match with the argument and the channel function remove it from the channel array
- if (c[jo][0] === a[i]) {
- //remove this item from the channel array
- c.splice(jo, 1);
- offset++;
- }
+ radio.$ = {
+ version: '0.1.0',
+ channelName: "",
+ channels: [],
+ /**
+ * Broadcast (publish)
+ * Iterate through all listeners (callbacks) in current channel and pass arguments to subscribers
+ * @param arguments data to be sent to listeners
+ * @example
+ * //basic usage
+ * radio('channel1').broadcast('my message');
+ * //send an unlimited number of parameters
+ * radio('channel2').broadcast(param1, param2, param3 ... );
+ */
+ broadcast:function () {
+ var i, c = this.channels[this.channelName],
+ l = c.length,
+ subscriber,
+ callback,
+ context;
+ //iterate through this channel and run each subscriber
+ for (i = 0; i < l; i++) {
+ subscriber = c[i];
+ //if listener was an array, set the callback and context.
+ if ((typeof(subscriber) === 'object') && (subscriber.length)) {
+ callback = subscriber[0];
+ //if user sent it without a context, set the context to the function
+ context = subscriber[1] || global;
+ }
+ callback.apply(context, arguments);
+ }
+ return this;
+ },
- }
- }
- return this;
- }
- };
+ /**
+ * Create the channel if it doesn't exist and set the current channel/event name
+ * @param {String} name the name of the channel
+ * @example
+ * radio('channel1');
+ */
+ channel:function (name) {
+ var c = this.channels;
+ //create a new channel if it doesn't exists
+ if (!c[name]) c[name] = [];
+ this.channelName = name;
+ return this;
+ },
- /**
- * Main Wrapper for radio._ and create a function radio to accept the channelName
- * @param[string] channelName topic of event
- */
- function radio(channelName) {
- radio._.channel(channelName);
- return radio._;
- }
+ /**
+ * Add Subscriber to channel
+ * Take the arguments and add it to the this.channels array.
+ * @param {Function|Array} arguments list of callbacks or arrays[callback, context] separated by commas
+ * @example
+ * //basic usage
+ * var callback = function() {};
+ * radio('channel1').add(callback);
+ *
+ * //add an endless amount of callbacks
+ * radio('channel1').add(callback, callback2, callback3 ...);
+ *
+ * //adding callbacks with context
+ * radio('channel1').add([callback, context],[callback1, context], callback3);
+ *
+ * //add by chaining
+ * radio('channel1').add(callback).radio('channel2').add(callback).add(callback2);
+ */
+ add:function () {
+ var a = arguments,
+ c = this.channels[this.channelName],
+ i,
+ l = a.length,
+ p, ai = [];
+
+ //run through each arguments and add it to the channel
+ for (i = 0; i < l; i++) {
+ ai = a[i];
+ //if the user send just a function, wrap the fucntion in an array [function]
+ p = (typeof(ai) === "function") ? [ai] : ai;
+ if ((typeof(p) === 'object') && (p.length)) c.push(p);
+ }
+ return this;
+ },
+
+ /**
+ * Remove subscriber from channel
+ * Take arguments and add it to the this.channels array.
+ * @param {Function} arguments callbacks separated by commas
+ * @example
+ * //basic usage
+ * radio('channel1').remove(callback); //will remove callback from channel1
+ * //you can remove as many callbacks as you want
+ * radio('channel1').remove(callback, callback2, callback3 ...);
+ * //callbacks with context
+ * radio('channel1').add([callback, context]).remove(callback); //just remove the callback
+ */
+ remove:function () {
+ var a = arguments,
+ i,
+ j,
+ c = this.channels[this.channelName],
+ l = a.length,
+ cl = c.length,
+ offset = 0,
+ jo;
+ //loop through each argument
+ for (i = 0; i < l; i++) {
+ //reset vars that change as the channel array items are removed
+ offset = 0;
+ cl = c.length;
+ //loop through the channel
+ for (j = 0; j < cl; j++) {
+ jo = j - offset;
+ //if there is a match with the argument and the channel function remove it from the channel array
+ if (c[jo][0] === a[i]) {
+ //remove matched item from the channel array
+ c.splice(jo, 1);
+ offset++;
+ }
+
+ }
+ }
+ return this;
+ }
+ };
//add radio to window object
- (global.radio) ? global.radio : global.radio = radio;
-})(window);
+ global.radio = global.radio || radio;
+})(window);
View
64 tests/core.js
@@ -28,36 +28,36 @@ describe("Radio Core Test", function() {
});
it("should return a version number", function() {
- expect(radio._.version).toBeDefined();
+ expect(radio.$.version).toBeDefined();
});
});
describe("radio() method", function() {
it("should set the channel name", function() {
radio('channel1');
- expect(radio._.channelName).toBe('channel1');
+ expect(radio.$.channelName).toBe('channel1');
});
it("requires a string", function() {
radio('channel1');
- expect(radio._.channelName).toBe('channel1');
+ expect(radio.$.channelName).toBe('channel1');
});
});
describe("radio.channel method", function() {
it("should set the channel name", function() {
radio('test').channel('channel1');
- expect(radio._.channelName).toBe('channel1');
+ expect(radio.$.channelName).toBe('channel1');
});
it("should make multiple channels chainable", function() {
radio('channel1').add(f,f).channel('channel2').add(f);
- expect(radio._.channelName).toBe('channel2');
- expect(radio._.channels.channel1.length).toBe(2);
- expect(radio._.channels.channel2.length).toBe(1);
+ expect(radio.$.channelName).toBe('channel2');
+ expect(radio.$.channels.channel1.length).toBe(2);
+ expect(radio.$.channels.channel2.length).toBe(1);
});
it("should create a new channel if it doesn't exists", function() {
- expect(radio._.channels['test']).not.toBeDefined();
+ expect(radio.$.channels['test']).not.toBeDefined();
radio('test');
- expect(radio._.channels['test']).toBeDefined();
+ expect(radio.$.channels['test']).toBeDefined();
});
});
@@ -66,73 +66,73 @@ describe("Radio Core Test", function() {
describe("radio.add method", function() {
it("should register a single listener to a channel", function() {
var test = radio('channel1').add(f);
- expect(radio._.channels.channel1.length).toBe(1);
+ expect(radio.$.channels.channel1.length).toBe(1);
});
it("should add multiple listeners to a channel", function() {
radio('channel1').add(f,f,f);
- expect(radio._.channels.channel1.length).toBe(3);
+ expect(radio.$.channels.channel1.length).toBe(3);
});
it("should suppport adding listerners with setting the context of 'this'", function() {
radio('channel1').add([test.selfTest, test]);
- expect(radio._.channels.channel1.length).toBe(1);
+ expect(radio.$.channels.channel1.length).toBe(1);
});
it("should support a combination of adding functions in different ways", function() {
radio('channel1').add(f,[test.selfTest, test], f2);
- expect(radio._.channels.channel1.length).toBe(3);
+ expect(radio.$.channels.channel1.length).toBe(3);
});
it("should not allow a non-function or array to be added", function() {
radio('channel1').add("string", f);
- expect(radio._.channels.channel1.length).toBe(1);
+ expect(radio.$.channels.channel1.length).toBe(1);
radio('channel1').add(2, f);
- expect(radio._.channels.channel1.length).toBe(2);
+ expect(radio.$.channels.channel1.length).toBe(2);
});
it("should be chainable", function() {
radio('channel1').add(f).add(f2);
- expect(radio._.channels.channel1.length).toBe(2);
+ expect(radio.$.channels.channel1.length).toBe(2);
});
});
describe("radio.remove method", function() {
it("should remove a listener from a channel", function() {
radio('channel1').add(f);
- expect(radio._.channels.channel1.length).toBe(1);
+ expect(radio.$.channels.channel1.length).toBe(1);
radio('channel1').remove(f);
- expect(radio._.channels.channel1.length).toBe(0);
+ expect(radio.$.channels.channel1.length).toBe(0);
});
it("should remove multiple listeners from a channel", function() {
radio('channel1').add(f,f2,f3);
- expect(radio._.channels.channel1.length).toBe(3);
+ expect(radio.$.channels.channel1.length).toBe(3);
radio('channel1').remove(f);
- expect(radio._.channels.channel1.length).toBe(2);
+ expect(radio.$.channels.channel1.length).toBe(2);
});
it("should remove duplicate listeners from a channel", function() {
//check 1
radio('channel1').add(f,f,f);
- expect(radio._.channels.channel1.length).toBe(3);
+ expect(radio.$.channels.channel1.length).toBe(3);
radio('channel1').remove(f);
- expect(radio._.channels.channel1.length).toBe(0);
+ expect(radio.$.channels.channel1.length).toBe(0);
//check2
radio('channel1').add(f,f,f, f2, f3, [test.selfTest, test], test.selfTest);
- expect(radio._.channels.channel1.length).toBe(7);
+ expect(radio.$.channels.channel1.length).toBe(7);
radio('channel1').remove(f);
- expect(radio._.channels.channel1.length).toBe(4);
+ expect(radio.$.channels.channel1.length).toBe(4);
radio('channel1').remove(test.selfTest);
- expect(radio._.channels.channel1.length).toBe(2);
+ expect(radio.$.channels.channel1.length).toBe(2);
radio('channel1').remove(f2, f3);
- expect(radio._.channels.channel1.length).toBe(0);
+ expect(radio.$.channels.channel1.length).toBe(0);
});
it("should remove listeners add with context from channel", function() {
radio('channel1').add([test.selfTest, test], f);
- expect(radio._.channels.channel1.length).toBe(2);
+ expect(radio.$.channels.channel1.length).toBe(2);
radio('channel1').remove(test.selfTest);
- expect(radio._.channels.channel1.length).toBe(1);
+ expect(radio.$.channels.channel1.length).toBe(1);
});
it("should not throw an error if remove an item that doesn't exists", function() {
radio('newChannel').remove(f2);
});
it("should be chainable", function() {
radio('channel1').add(f, f2, f3).remove(f2);
- expect(radio._.channels.channel1.length).toBe(2);
+ expect(radio.$.channels.channel1.length).toBe(2);
});
});
@@ -142,7 +142,7 @@ describe("Radio Core Test", function() {
spyOn(test, 'add');
spyOn(test, 'selfTest');
radio('channel1').add([test.selfTest, test], [test.add, test]);
- expect(radio._.channels.channel1.length).toBe(2);
+ expect(radio.$.channels.channel1.length).toBe(2);
radio('channel1').broadcast('test');
expect(test.selfTest).toHaveBeenCalled();
expect(test.add).toHaveBeenCalled();
@@ -199,7 +199,7 @@ describe("Radio Core Test", function() {
it("should work all together in a chain", function() {
//test 1
radio('channel1').add(f, f2, f3).remove(f);
- expect(radio._.channels.channel1.length).toBe(2);
+ expect(radio.$.channels.channel1.length).toBe(2);
//test2
spyOn(window, 'f');
@@ -217,7 +217,7 @@ describe("Radio Core Test", function() {
afterEach(function() {
//clear out the channels.
- radio._.channels = [];
+ radio.$.channels = [];
});
});
View
15 tests/test.js
@@ -1,15 +0,0 @@
-var test = {
-
- selfTest : function() {
- return this;
- },
-
- test1: function() {
-
- },
-
- test2: function() {
-
- }
-
-}
View
1  tests/tests.html
@@ -6,7 +6,6 @@
<title>Radio.js Tests</title>
<link rel="stylesheet" href="jasmine-1.1.0/jasmine.css" />
<script src="../radio.js"></script>
- <script src="test.js"></script>
<!--jasmine files-->
<script src="jasmine-1.1.0/jasmine.js"></script>
<script src="jasmine-1.1.0/jasmine-html.js"></script>

0 comments on commit 56c0576

Please sign in to comment.
Something went wrong with that request. Please try again.