Skip to content

Commit 918675c

Browse files
vash15vash15
authored andcommitted
First commit
1 parent ffefa45 commit 918675c

File tree

4 files changed

+93
-1
lines changed

4 files changed

+93
-1
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
.DS_Store
2+
bower_components

README.md

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,31 @@
1-
pubsub
1+
Pubsub
22
======
33

44
PubSub for the browser
5+
6+
7+
## Install
8+
9+
```
10+
bower install backbone.pubsub --save
11+
```
12+
13+
## Usage
14+
15+
```js
16+
var PubSub = require("backbone.pubsub");
17+
18+
var pubSub = new PubSub();
19+
20+
pubSub.on("custom:trigger", function(data){
21+
console.log("Custom trigger fire!!", data);
22+
});
23+
24+
25+
setTimeout(function(){
26+
pubSub.trigger("custom:trigger", {custom: data});
27+
}, 5000);
28+
29+
```
30+
31+
## License

bower.json

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
{
2+
"name": "backbone.pubsub",
3+
"version": "1.0.0",
4+
"homepage": "https://github.com/vash15/pubsub",
5+
"authors": [
6+
"Michele Belluco"
7+
],
8+
"description": "PubSub for the browser",
9+
"main": "lib/pubsub.js",
10+
"moduleType": [
11+
"node"
12+
],
13+
"keywords": [
14+
"backbone",
15+
"pubsub"
16+
],
17+
"license": "MIT",
18+
"ignore": [
19+
"**/.*",
20+
"node_modules",
21+
"bower_components",
22+
"test",
23+
"tests"
24+
],
25+
"dependencies": {
26+
"underscore": "~1.6.0",
27+
"backbone": "~1.1.2"
28+
}
29+
}

lib/pubsub.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
;(function(){
2+
3+
var _ = require('underscore');
4+
var Backbone = require('backbone');
5+
6+
var PubSub = module.exports = _.extend({}, Backbone.Events);
7+
8+
PubSub.middleware = function () {
9+
var self = this;
10+
return function (shared, next) {
11+
shared.pubsub = self;
12+
next();
13+
}
14+
}
15+
16+
PubSub.trigger = function trigger( eventName ){
17+
var args = [].slice.call(arguments, 1);
18+
var self = this;
19+
var compositEventName = [];
20+
21+
_(eventName.split(":")).forEach(function( anEventName ){
22+
23+
compositEventName.push( anEventName );
24+
25+
(function( triggerEventName ){
26+
var triggerArgs = [ triggerEventName ];
27+
Backbone.Events.trigger.apply( self, triggerArgs.concat(args) );
28+
})(compositEventName.join(":") );
29+
30+
});
31+
32+
}
33+
34+
})();

0 commit comments

Comments
 (0)