Commit 516420f9 authored by Robert Knight's avatar Robert Knight Committed by GitHub

Merge pull request #333 from...

Merge pull request #333 from hypothesis/fix-annotating-sites-with-broken-Function.bind-polyfills-take-3

Fix annotating sites with broken function.bind polyfills take 3
parents b0a27588 65b18b31
'use strict';
module.exports = {
rules: {
'no-restricted-properties': [2, {
// Disable `bind` usage in annotator/ code to prevent unexpected behavior
// due to broken bind polyfills. See
// https://github.com/hypothesis/client/issues/245
property: 'bind',
message: 'Use function expressions instead of bind() in annotator/ code'
}]
}
};
...@@ -6,10 +6,7 @@ ...@@ -6,10 +6,7 @@
// It also listens for events from Annotator when new annotations are created or // It also listens for events from Annotator when new annotations are created or
// annotations successfully anchor and relays these to the sidebar app. // annotations successfully anchor and relays these to the sidebar app.
function AnnotationSync(bridge, options) { function AnnotationSync(bridge, options) {
var event; var self = this;
var func;
var handler;
var method;
this.bridge = bridge; this.bridge = bridge;
...@@ -27,20 +24,20 @@ function AnnotationSync(bridge, options) { ...@@ -27,20 +24,20 @@ function AnnotationSync(bridge, options) {
this._emit = options.emit; this._emit = options.emit;
// Listen locally for interesting events // Listen locally for interesting events
for (event in this._eventListeners) { Object.keys(this._eventListeners).forEach(function(eventName) {
if (Object.prototype.hasOwnProperty.call(this._eventListeners, event)) { var listener = self._eventListeners[eventName];
handler = this._eventListeners[event]; self._on(eventName, function(annotation) {
this._on(event, handler.bind(this)); listener.apply(self, [annotation]);
} });
} });
// Register remotely invokable methods // Register remotely invokable methods
for (method in this._channelListeners) { Object.keys(this._channelListeners).forEach(function(eventName) {
if (Object.prototype.hasOwnProperty.call(this._channelListeners, method)) { self.bridge.on(eventName, function(data, callbackFunction) {
func = this._channelListeners[method]; var listener = self._channelListeners[eventName];
this.bridge.on(method, func.bind(this)); listener.apply(self, [data, callbackFunction]);
} });
} });
} }
// Cache of annotations which have crossed the bridge for fast, encapsulated // Cache of annotations which have crossed the bridge for fast, encapsulated
......
...@@ -42,7 +42,7 @@ module.exports = class Sidebar extends Host ...@@ -42,7 +42,7 @@ module.exports = class Sidebar extends Host
this._setupSidebarEvents() this._setupSidebarEvents()
_setupDocumentEvents: -> _setupDocumentEvents: ->
sidebarTrigger(document.body, @show.bind(this)) sidebarTrigger(document.body, => this.show())
@element.on 'click', (event) => @element.on 'click', (event) =>
if !@selectedTargets?.length if !@selectedTargets?.length
...@@ -62,8 +62,8 @@ module.exports = class Sidebar extends Host ...@@ -62,8 +62,8 @@ module.exports = class Sidebar extends Host
_setupSidebarEvents: -> _setupSidebarEvents: ->
annotationCounts(document.body, @crossframe) annotationCounts(document.body, @crossframe)
@crossframe.on('show', this.show.bind(this)) @crossframe.on('show', => this.show())
@crossframe.on('hide', this.hide.bind(this)) @crossframe.on('hide', => this.hide())
@crossframe.on(events.DO_LOGIN, => @crossframe.on(events.DO_LOGIN, =>
if @onLoginRequest if @onLoginRequest
@onLoginRequest() @onLoginRequest()
......
...@@ -29,8 +29,8 @@ describe('AnnotationSync', function() { ...@@ -29,8 +29,8 @@ describe('AnnotationSync', function() {
}; };
options = { options = {
on: emitter.on.bind(emitter), on: emitter.on.bind(emitter), // eslint-disable-line no-restricted-properties
emit: emitter.emit.bind(emitter), emit: emitter.emit.bind(emitter), // eslint-disable-line no-restricted-properties
}; };
publish = function() { publish = function() {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment