Skip to content

Commit

Permalink
Enforce that event payloads are objects
Browse files Browse the repository at this point in the history
  • Loading branch information
geekdave committed Jul 17, 2013
1 parent 3e05950 commit 6f207f4
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 9 deletions.
18 changes: 16 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,17 @@ To solve this issue, Geppetto implements a scalable **Controller** architecture

### Getting Geppetto

*Latest Stable Release: 0.6*
*Latest Stable Release: 0.6.1*

* Minified: [backbone.geppetto.min.js](https://github.com/ModelN/backbone.geppetto/blob/0.6.1/dist/backbone.geppetto.min.js)
* Development (Uncompressed, Comments): [backbone.geppetto.js](https://raw.github.com/ModelN/backbone.geppetto/0.6.1/backbone.geppetto.js)
* Full Release (Tests, Examples): [0.6.1.zip](https://github.com/ModelN/backbone.geppetto/archive/0.6.1.zip).

*Unreleased Edge Version (master)*

* Minified: [backbone.geppetto.min.js](https://raw.github.com/ModelN/backbone.geppetto/master/dist/backbone.geppetto.min.js)
* Development (Uncompressed, Comments): [backbone.geppetto.js](https://raw.github.com/ModelN/backbone.geppetto/master/backbone.geppetto.js)
* Full Release (Tests, Examples): [0.6.zip](https://github.com/ModelN/backbone.geppetto/archive/0.6.zip).
* Full Release (Tests, Examples): [master.zip](https://github.com/ModelN/backbone.geppetto/archive/master.zip).

Visit the [project repo](https://github.com/ModelN/backbone.geppetto) to download the latest unreleased code (may be unstable).

Expand Down Expand Up @@ -352,6 +358,9 @@ context.dispatchGlobally( "fooEvent");

### Dispatching Events with a Payload

If your event has some associated data that should be available to the consumer of your event, you can
pass that event as an object as the second parameter of the call to `dispatch` like so:

```javascript
context.dispatch( "fooEvent",
{
Expand Down Expand Up @@ -536,6 +545,11 @@ Run the current Geppetto Test Specs in your browser [here](http://modeln.github.

## Version History

### 0.6.1
*Released 17 July 2013*

* Enforce that event payloads are objects, and not other types.

### 0.6
*Released 2 June 2013*

Expand Down
8 changes: 5 additions & 3 deletions backbone.geppetto.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Backbone.Geppetto v0.6
// Backbone.Geppetto v0.6.1
//
// Copyright (C) 2013 Model N, Inc.
// Distributed under the MIT License
Expand Down Expand Up @@ -109,6 +109,9 @@
};

Geppetto.Context.prototype.dispatch = function dispatch( eventName, eventData ) {
if ( ! _.isUndefined(eventData) && ! _.isObject(eventData) ) {
throw "Event payload must be an object";
}
eventData = eventData || {};
eventData.eventName = eventName;
this.vent.trigger( eventName, eventData ); };
Expand Down Expand Up @@ -159,8 +162,7 @@
this.dispatchToParent(Geppetto.EVENT_CONTEXT_SHUTDOWN);
};

var extend = Backbone.View.extend;
Geppetto.Context.extend = extend;
Geppetto.Context.extend = Backbone.View.extend;

var debug = {

Expand Down
2 changes: 1 addition & 1 deletion dist/backbone.geppetto.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "backbone.geppetto",
"description": "Bring your Backbone applications to life with an event-driven Command framework.",
"version": "0.6.0",
"version": "0.6.1",
"homepage": "https://github.com/ModelN/backbone.geppetto",
"author": {
"name": "Dave Cadwallader",
Expand All @@ -27,7 +27,7 @@
"node": ">= 0.8.0"
},
"scripts": {
"test": "grunt travis --verbose"
"test": "grunt travis --verbose"
},
"dependencies": {
"backbone": "~1.0.0"
Expand Down
26 changes: 25 additions & 1 deletion specs/geppetto-specs.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,31 @@ pavlov.specify("Backbone.Geppetto", function(){
assert(payload.bar).isEqualTo("baz");
});

it("should the foo event when listened from the parent view", function() {
it("should throw an exception if the payload object is a string, not an object", function() {
assert(function() {
contextInstance.dispatch("foo", "baz");
} ).throwsException("Event payload must be an object");
});

it("should throw an exception if the payload object is a boolean false, not an object", function() {
assert(function() {
contextInstance.dispatch("foo", false);
} ).throwsException("Event payload must be an object");
});

it("should throw an exception if the payload object is a boolean true, not an object", function() {
assert(function() {
contextInstance.dispatch("foo", true);
} ).throwsException("Event payload must be an object");
});

it("should throw an exception if the payload object is null, not an object", function() {
assert(function() {
contextInstance.dispatch("foo", null);
} ).throwsException("Event payload must be an object");
});

it("should pass the foo event when listened from the parent view", function() {
var parentFooSpy = sinon.spy();
contextInstance.listen(parentView, "foo", parentFooSpy);
contextInstance.dispatch("foo");
Expand Down

0 comments on commit 6f207f4

Please sign in to comment.