Skip to content

Instantly share code, notes, and snippets.

@gcpantazis
Last active August 29, 2015 13:56
Show Gist options
  • Save gcpantazis/8832259 to your computer and use it in GitHub Desktop.
Save gcpantazis/8832259 to your computer and use it in GitHub Desktop.
Compiled version of https://github.com/theinterned/backbone-responsive-view, slightly modified.
(function(_, Enquire) {
'use strict';
window.BackboneEnquireMixin = {
enquire: function(breakpoints) {
var breakpoint, handler, method, _i, _len, _ref;
if (!breakpoints) {
breakpoints = this.breakpoints;
}
breakpoints = this._getBreakpoints(breakpoints);
if (typeof breakpoints !== 'object') {
return;
}
for (breakpoint in breakpoints) {
handler = breakpoints[breakpoint];
if (typeof handler === 'string') {
handler = this[handler];
} else if (typeof handler === 'object') {
_ref = ['match', 'unmatch', 'setup', 'destroy'];
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
method = _ref[_i];
if (typeof handler[method] === 'string') {
handler[method] = this[handler[method]];
}
}
}
Enquire.register(breakpoint, _.bind(handler, this));
}
return this;
},
unenquire: function(breakpoints, breakpoint) {
var handler;
if (!breakpoints) {
breakpoints = this.breakpoints;
}
if (breakpoints === false) {
Enquire.unregister(breakpoint);
} else {
breakpoints = this._getBreakpoints(breakpoints);
for (breakpoint in breakpoints) {
handler = breakpoints[breakpoint];
if (typeof handler === 'string') {
handler = this[handler];
}
Enquire.unregister(breakpoint, handler);
}
}
return this;
},
_getBreakpoints: function(breakpoints) {
if (typeof breakpoints === 'string') {
breakpoints = this[breakpoints];
}
if (breakpoints === true) {
breakpoints = this.breakpoints;
}
return breakpoints;
}
};
})(window._, window.enquire);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment