-
-
Save HenryVonfire/bd403a300282e9c448e0 to your computer and use it in GitHub Desktop.
Ember PubSub Service
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Ember from 'ember'; | |
import { BusListenerMixin } from 'demo-app/services/bus'; | |
export default Ember.Component.extend(BusListenerMixin, { | |
classNames: ['message-listener'], | |
_setup: function() { | |
this.set('messageLog', []); | |
}.on('didInitAttrs'), | |
receivedSubMessage: function(name, message) { | |
this.get('messageLog').pushObject({ | |
name: name, | |
message: JSON.stringify(message) | |
}); | |
}, | |
messageTypes: Ember.computed('messageSubs.[]', function() { | |
var subs = (this.get('messageSubs') || []); | |
return '[' + subs.join(',') + ']'; | |
}) | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Ember from 'ember'; | |
export default Ember.Route.extend({ | |
setupController: function(controller) { | |
controller.set('subsOne', ['tomdale', 'wycats']); | |
controller.set('subsTwo', ['wycats', 'mixonic']); | |
} | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Ember from 'ember'; | |
export var BusListenerMixin = Ember.Mixin.create({ | |
bus: Ember.inject.service(), | |
messageSubs: null, | |
init: function() { | |
this._super(); | |
this.get('bus').on('pubMessage', this, this.receivedMessage); | |
}, | |
destroy: function() { | |
this.get('bus').off('pubMessage', this, this.receivedMessage); | |
this._super(); | |
}, | |
receivedMessage: function(name, message) { | |
var messageSubs = this.get('messageSubs'); | |
if(messageSubs && messageSubs.contains(name)) { | |
this.receivedSubMessage(name, message); | |
} | |
}, | |
receivedSubMessage: function(name, message) { | |
// Primary hook. | |
} | |
}); | |
export var BusPublisherMixin = Ember.Mixin.create({ | |
bus: Ember.inject.service(), | |
publishMessage: function(name, message) { | |
this.get('bus').publish(name, message); | |
} | |
}); | |
var BusService = Ember.Service.extend(Ember.Evented, { | |
setupService: function() { | |
/* Sometimes these require setup */ | |
}, | |
publish: function(name, message) { | |
this.trigger('pubMessage', name, message); | |
} | |
}); | |
export default BusService; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
body { | |
margin: 12px 16px; | |
font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; | |
font-size: 12pt; | |
} | |
.message-pub-button { | |
display: inline-block; | |
} | |
.message-listener { | |
padding: 5px 8px; | |
color: #0A1d26; | |
background-color: #E6EEF2; | |
margin: 15px 0; | |
} | |
.message-listener .message-block { | |
margin-left: 5px; | |
} | |
.message-listener .message-block pre { | |
display: inline-block; | |
margin: 0; | |
background-color: #CED5D9; | |
font-style: italic; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"version": "0.4.8", | |
"dependencies": { | |
"jquery": "https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.3/jquery.js", | |
"ember": "https://cdnjs.cloudflare.com/ajax/libs/ember.js/1.13.8/ember.js", | |
"ember-data": "https://cdnjs.cloudflare.com/ajax/libs/ember-data.js/1.13.9/ember-data.js", | |
"ember-template-compiler": "https://cdnjs.cloudflare.com/ajax/libs/ember.js/1.13.8/ember-template-compiler.js" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment