Skip to content

Instantly share code, notes, and snippets.

@valueof
Created June 6, 2012 00:50
Show Gist options
  • Save valueof/2879178 to your computer and use it in GitHub Desktop.
Save valueof/2879178 to your computer and use it in GitHub Desktop.
Backbone view that initializes a third-party JavaScript app, attaches a couple of listeners and sends a signal back to the parent page: with and without fat arrows.
var Lounge = Backbone.View.extend({
initialize: function (options) {
// ...
Bus.listen('window.inViewport', () => {
this.states.inViewport = true;
this.trigger('inViewport');
});
Bus.listen('window.scrollOffViewport', () => {
this.states.inViewport = false;
});
this.switches.on('reset', () => {
var isThreadReady = () => {
if (!this.states.templateReady)
return false;
return this.thread && this.thread.id;
};
DISQUS.defer(isThreadReady, _.bind(this.poll, this));
DISQUS.defer(isThreadReady, _.bind(this.renderThreadVotes, this, this.thread));
});
Bus.bind('init', (config) => {
lounge.bootstrap(config);
});
Bus.sendHostMessage('ready');
}
});
var Lounge = Backbone.View.extend({
initialize: function (options) {
// ...
Bus.listen('window.inViewport', _.bind(function () {
this.states.inViewport = true;
this.trigger('inViewport');
}, this));
Bus.listen('window.scrollOffViewport', _.bind(function () {
this.states.inViewport = false;
}, this));
this.switches.on('reset', function () {
var isThreadReady = function () {
if (!this.states.templateReady)
return false;
return this.thread && this.thread.id;
};
DISQUS.defer(_.bind(isThreadReady, this), _.bind(this.poll, this));
DISQUS.defer(_.bind(isThreadReady, this),
_.bind(this.renderThreadVotes, this, this.thread));
}, this);
Bus.bind('init', _.bind(function (config) {
lounge.bootstrap(config);
}));
Bus.sendHostMessage('ready');
}
});
@domenic
Copy link

domenic commented Jun 6, 2012

Nice. The isThreadReady is a pretty big win in particular.

I do kind of wish the () in () => ... was optional though. And the bind operator strawman would be a deliciously sugary fix for your remaining _.bind.

Thanks for putting these out there; I think it helps the community a lot.

@valueof
Copy link
Author

valueof commented Jun 6, 2012

Tested using Traceur and updated fixed version.

@allenwb
Copy link

allenwb commented Jun 6, 2012

I would think that the two remaining bind calls could also be replaced:

DISQUS.defer(isThreadReady, ()=>this.poll());
DISQUS.defer(isThreadReady, ()=>this.renderThreadVotes(this.thread));

I'm not familiar enough with the frameworks to be be sure that the arguments passed to the defered functions are actually ignored, but it appears that way from the samples. If not, explicit arguments could be specified for the arrows.

I'd suggest that in the long run this maybe a better way to think about this pattern. Instead of "binding the method". You are just passing a thunk that converts a "function call" to a "method call" on a predetermine object.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment