From 25aab8b43f4686d6b16a7def0673dfc71d10a6ad Mon Sep 17 00:00:00 2001 From: Steven Orvell Date: Tue, 27 Oct 2015 17:39:07 -0700 Subject: [PATCH] Remove use of Function.bind --- src/lib/base.html | 7 ++++--- src/lib/bind/accessors.html | 13 +++++++++---- src/lib/custom-style.html | 5 +++-- src/lib/debounce.html | 5 ++++- src/lib/dom-api-distributed-nodes-observer.html | 6 ++++-- src/lib/dom-api-effective-nodes-observer.html | 9 +++++++-- src/lib/template/dom-bind.html | 10 ++++++++-- src/lib/template/dom-repeat.html | 17 +++++++++++------ src/lib/template/templatizer.html | 15 +++++++++++---- src/standard/annotations.html | 5 ++++- src/standard/gestures.html | 4 +++- src/standard/utils.html | 14 +++++++++++--- test/unit/base.html | 5 ++--- 13 files changed, 81 insertions(+), 34 deletions(-) diff --git a/src/lib/base.html b/src/lib/base.html index 3ce0612000..c72391031b 100644 --- a/src/lib/base.html +++ b/src/lib/base.html @@ -44,10 +44,11 @@ // https://code.google.com/p/chromium/issues/detail?id=516550 // To allow querying style/layout data in attached, we defer it // until we are sure rendering is ready. + var self = this; Polymer.RenderStatus.whenReady(function() { - this.isAttached = true; - this._doBehavior('attached'); // abstract - }.bind(this)); + self.isAttached = true; + self._doBehavior('attached'); // abstract + }); }, // reserved for canonical behavior diff --git a/src/lib/bind/accessors.html b/src/lib/bind/accessors.html index b07987cd8c..352e8e6d02 100644 --- a/src/lib/bind/accessors.html +++ b/src/lib/bind/accessors.html @@ -238,15 +238,20 @@ }, setupBindListeners: function(inst) { - inst._bindListeners.forEach(function(info) { + var b$ = inst._bindListeners; + for (var i=0, l=b$.length, info; (i.on.-changed: = e.detail.value //console.log('[_setupBindListener]: [%s][%s] listening for [%s][%s-changed]', this.localName, info.path, info.id || info.index, info.property); - var node = inst._nodes[info.index]; - node.addEventListener(info.event, inst._notifyListener.bind(inst, info.changedFn)); + this._addNotifyListener(inst._nodes[info.index], inst, info); + }; + }, + + _addNotifyListener: function(element, context, info) { + element.addEventListener(info.event, function(e) { + return context._notifyListener(info.changedFn, e); }); } - }; diff --git a/src/lib/custom-style.html b/src/lib/custom-style.html index 56c5c8168c..de7f039039 100644 --- a/src/lib/custom-style.html +++ b/src/lib/custom-style.html @@ -117,10 +117,11 @@ if (e.textContent || this.include) { this._apply(); } else { + var self = this; var observer = new MutationObserver(function() { observer.disconnect(); - this._apply(); - }.bind(this)); + self._apply(); + }); observer.observe(e, {childList: true}); } } diff --git a/src/lib/debounce.html b/src/lib/debounce.html index f787670173..73263bf0de 100644 --- a/src/lib/debounce.html +++ b/src/lib/debounce.html @@ -24,7 +24,10 @@ var Debouncer = function(context) { this.context = context; - this.boundComplete = this.complete.bind(this); + var self = this; + this.boundComplete = function() { + self.complete(); + } }; Debouncer.prototype = { diff --git a/src/lib/dom-api-distributed-nodes-observer.html b/src/lib/dom-api-distributed-nodes-observer.html index 00f6512222..57d7f1f279 100644 --- a/src/lib/dom-api-distributed-nodes-observer.html +++ b/src/lib/dom-api-distributed-nodes-observer.html @@ -58,8 +58,10 @@ var root = this.domApi.getOwnerRoot(); var host = root && root.host; if (host) { - this._observer = Polymer.dom(host).observeNodes( - this._scheduleNotify.bind(this)); + var self = this; + this._observer = Polymer.dom(host).observeNodes(function() { + self._scheduleNotify(); + }); // NOTE: we identify this listener as needed for // notification so that enableShadowAttributeTracking // can find these observers an ensure that we pass always diff --git a/src/lib/dom-api-effective-nodes-observer.html b/src/lib/dom-api-effective-nodes-observer.html index a4d41f70f4..f98de7d456 100644 --- a/src/lib/dom-api-effective-nodes-observer.html +++ b/src/lib/dom-api-effective-nodes-observer.html @@ -107,7 +107,10 @@ }, _observeContent: function(content) { - var h = Polymer.dom(content).observeNodes(this._scheduleNotify.bind(this)); + var self = this; + var h = Polymer.dom(content).observeNodes(function() { + self._scheduleNotify(); + }); h._avoidChangeCalculation = true; return h; }, @@ -200,7 +203,9 @@ } }; this._observer = new MutationObserver(this._mutationHandler); - this._boundFlush = this._flush.bind(this); + this._boundFlush = function() { + self._flush(); + } Polymer.dom.addStaticFlush(this._boundFlush); // NOTE: subtree true is way too aggressive, but it easily catches // attribute changes on children. These changes otherwise require diff --git a/src/lib/template/dom-bind.html b/src/lib/template/dom-bind.html index caac780fa5..d52912b189 100644 --- a/src/lib/template/dom-bind.html +++ b/src/lib/template/dom-bind.html @@ -66,7 +66,10 @@ created: function() { // Ensure dom-bind doesn't stamp until all possible dependencies // have resolved - Polymer.RenderStatus.whenReady(this._markImportsReady.bind(this)); + var self = this; + Polymer.RenderStatus.whenReady(function() { + self._markImportsReady(); + }); }, _ensureReady: function() { @@ -119,7 +122,10 @@ config[prop] = this[prop]; } // Pass values set before attached as initialConfig to _setupConfigure - this._setupConfigure = this._setupConfigure.bind(this, config); + var setupConfigure = this._setupConfigure; + this._setupConfigure = function() { + setupConfigure.call(this, config); + }; }, attached: function() { diff --git a/src/lib/template/dom-repeat.html b/src/lib/template/dom-repeat.html index 0a56ed7891..90173c40ee 100644 --- a/src/lib/template/dom-repeat.html +++ b/src/lib/template/dom-repeat.html @@ -376,9 +376,10 @@ } // Apply user sort to keys if (this._sortFn) { + var self = this; keys.sort(function(a, b) { - return this._sortFn(c.getItem(a), c.getItem(b)); - }.bind(this)); + return self._sortFn(c.getItem(a), c.getItem(b)); + }); } // Generate instances and assign items and keys for (var i=0; i> 1; diff --git a/src/lib/template/templatizer.html b/src/lib/template/templatizer.html index 4d51457d4b..dd35ccfa23 100644 --- a/src/lib/template/templatizer.html +++ b/src/lib/template/templatizer.html @@ -194,8 +194,9 @@ if (!c._notes) { var rootDataHost = archetype._rootDataHost; if (rootDataHost) { - Polymer.Annotations.prepElement = - rootDataHost._prepElement.bind(rootDataHost); + Polymer.Annotations.prepElement = function() { + rootDataHost._prepElement(); + } } c._notes = Polymer.Annotations.parseAnnotations(template); Polymer.Annotations.prepElement = null; @@ -250,10 +251,16 @@ // Instance setup if (template != this) { Polymer.Bind.prepareInstance(template); - template._forwardParentProp = this._forwardParentProp.bind(this); + var self = this; + template._forwardParentProp = function(source, value) { + self._forwardParentProp(source, value); + } } this._extendTemplate(template, proto); - template._pathEffector = this._pathEffectorImpl.bind(this); + var self = this; + template._pathEffector = function(path, value, fromAbove) { + return self._pathEffectorImpl(path, value, fromAbove); + } } }, diff --git a/src/standard/annotations.html b/src/standard/annotations.html index 5cd86828b8..d162897e3e 100644 --- a/src/standard/annotations.html +++ b/src/standard/annotations.html @@ -122,7 +122,10 @@ this._notes = []; } else { // TODO(sorvell): ad hoc method of plugging behavior into Annotations - Polymer.Annotations.prepElement = this._prepElement.bind(this); + var self = this; + Polymer.Annotations.prepElement = function(element) { + self._prepElement(element); + } if (this._template._content && this._template._content._notes) { this._notes = this._template._content._notes; } else { diff --git a/src/standard/gestures.html b/src/standard/gestures.html index 132abb556e..49a09304be 100644 --- a/src/standard/gestures.html +++ b/src/standard/gestures.html @@ -448,7 +448,9 @@ x: event.clientX, y: event.clientY, sourceEvent: event, - prevent: Gestures.prevent.bind(Gestures) + prevent: function(e) { + return Gestures.prevent(e); + } }); } }); diff --git a/src/standard/utils.html b/src/standard/utils.html index 59ff1fff9b..7d803b3d68 100644 --- a/src/standard/utils.html +++ b/src/standard/utils.html @@ -240,7 +240,10 @@ * @return {number} Handle that may be used to cancel the async job. */ async: function(callback, waitTime) { - return Polymer.Async.run(callback.bind(this), waitTime); + var self = this; + return Polymer.Async.run(function() { + callback.call(self); + }, waitTime); }, /** @@ -336,11 +339,16 @@ var l = document.createElement('link'); l.rel = 'import'; l.href = href; + var self = this; if (onload) { - l.onload = onload.bind(this); + l.onload = function(e) { + return onload.call(self, e); + } } if (onerror) { - l.onerror = onerror.bind(this); + l.onerror = function(e) { + return onerror.call(self, e); + } } document.head.appendChild(l); return l; diff --git a/test/unit/base.html b/test/unit/base.html index 679d6bb2c3..5ce1aa3169 100644 --- a/test/unit/base.html +++ b/test/unit/base.html @@ -128,13 +128,12 @@ test('calls attributeChanged()', function() { var args = null; Child.attributeChanged = function() {args = arguments}; - instance.attributeChangedCallback('attr', null, 1, 'stuff'); + instance.attributeChangedCallback('attr', null, 1); - assert.equal(args.length, 4); + assert.equal(args.length, 3); assert.equal(args[0], 'attr'); assert.equal(args[1], null); assert.equal(args[2], 1); - assert.equal(args[3], 'stuff'); }); });