Created
March 29, 2016 18:42
-
-
Save runspired/4ba05244ab211ea0c098120029e94bc2 to your computer and use it in GitHub Desktop.
Subscribe
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 subscribe from '../subscribe'; | |
export default Ember.Controller.extend({ | |
appName: 'Ember Twiddle', | |
myService: Ember.inject.service('my-service'), | |
foos: 0, | |
onFoo: subscribe('myService.foo', function() { | |
this.incrementProperty('foos'); | |
}) | |
}); |
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.Service.extend(Ember.Evented, { | |
init() { | |
this._super(); | |
setInterval(() => { | |
this.trigger('foo', { id: Date.now() }); | |
}, 3000); | |
} | |
}); |
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'; | |
const { | |
assert, | |
on | |
} = Ember; | |
export default function subscribe(path, method) { | |
let [service, event, err] = path.split('.'); | |
let _listener = null; | |
assert(`'subscribe()' expects a path with exactly one leaf, given ${path}`, event && service && !err); | |
let computedFn = function() { | |
if (!this.get(service) || _listener) { | |
return; | |
} | |
// ensure teardown | |
let _super = this.get('willDestroy'); | |
this.set('willDestroy', function() { | |
this.get(service).off(event, _listener); | |
_listener = null; | |
computedFn = null; | |
_super(); | |
}); | |
// proxy the event | |
_listener = (e) => { | |
method.call(this, e); | |
}; | |
// subscribe to the event | |
this.get(service).on(event, _listener); | |
}; | |
return on.call(this, 'init', computedFn); | |
} |
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.7.1", | |
"EmberENV": { | |
"FEATURES": {} | |
}, | |
"options": { | |
"use_pods": false, | |
"enable-testing": false | |
}, | |
"dependencies": { | |
"jquery": "https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.3/jquery.js", | |
"ember": "https://cdnjs.cloudflare.com/ajax/libs/ember.js/2.4.3/ember.debug.js", | |
"ember-data": "https://cdnjs.cloudflare.com/ajax/libs/ember-data.js/2.4.3/ember-data.js", | |
"ember-template-compiler": "https://cdnjs.cloudflare.com/ajax/libs/ember.js/2.4.3/ember-template-compiler.js" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment