Skip to content

Commit

Permalink
Remove use of Function.bind
Browse files Browse the repository at this point in the history
  • Loading branch information
Steven Orvell committed Oct 28, 2015
1 parent 5fb20da commit 25aab8b
Show file tree
Hide file tree
Showing 13 changed files with 81 additions and 34 deletions.
7 changes: 4 additions & 3 deletions src/lib/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 9 additions & 4 deletions src/lib/bind/accessors.html
Original file line number Diff line number Diff line change
Expand Up @@ -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<l) && (info=b$[i]); i++) {
// Property listeners:
// <node>.on.<property>-changed: <path]> = 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);
});
}

};

</script>
5 changes: 3 additions & 2 deletions src/lib/custom-style.html
Original file line number Diff line number Diff line change
Expand Up @@ -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});
}
}
Expand Down
5 changes: 4 additions & 1 deletion src/lib/debounce.html
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down
6 changes: 4 additions & 2 deletions src/lib/dom-api-distributed-nodes-observer.html
Original file line number Diff line number Diff line change
Expand Up @@ -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 <content>
// notification so that enableShadowAttributeTracking
// can find these observers an ensure that we pass always
Expand Down
9 changes: 7 additions & 2 deletions src/lib/dom-api-effective-nodes-observer.html
Original file line number Diff line number Diff line change
Expand Up @@ -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;
},
Expand Down Expand Up @@ -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
Expand Down
10 changes: 8 additions & 2 deletions src/lib/template/dom-bind.html
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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() {
Expand Down
17 changes: 11 additions & 6 deletions src/lib/template/dom-repeat.html
Original file line number Diff line number Diff line change
Expand Up @@ -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<keys.length; i++) {
Expand Down Expand Up @@ -415,7 +416,9 @@
var instances = this._instances;
var keyMap = {};
var pool = [];
var sortFn = this._sortFn || this._keySort.bind(this);
var self = this;
var sortFn = this._sortFn ||
function(a, b) { return self._keySort(a, b); };
// Dedupe added and removed keys to a final added/removed map
splices.forEach(function(s) {
for (var i=0; i<s.removed.length; i++) {
Expand Down Expand Up @@ -462,9 +465,10 @@
}, this);
}
// Sort added keys
var self = this;
addedKeys.sort(function(a, b) {
return this._sortFn(c.getItem(a), c.getItem(b));
}.bind(this));
return self._sortFn(c.getItem(a), c.getItem(b));
});
// Insertion-sort new instances into place (from pool or newly created)
var start = 0;
for (var i=0; i<addedKeys.length; i++) {
Expand All @@ -478,7 +482,8 @@
var item = c.getItem(key);
var end = this._instances.length - 1;
var idx = -1;
var sortFn = this._sortFn || this._keySort.bind(this);
var sortFn = this._sortFn ||
function(a, b) { return self._keySort(a, b); };
// Binary search for insertion point
while (start <= end) {
var mid = (start + end) >> 1;
Expand Down
15 changes: 11 additions & 4 deletions src/lib/template/templatizer.html
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}
},

Expand Down
5 changes: 4 additions & 1 deletion src/standard/annotations.html
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
4 changes: 3 additions & 1 deletion src/standard/gestures.html
Original file line number Diff line number Diff line change
Expand Up @@ -448,7 +448,9 @@
x: event.clientX,
y: event.clientY,
sourceEvent: event,
prevent: Gestures.prevent.bind(Gestures)
prevent: function(e) {
return Gestures.prevent(e);
}
});
}
});
Expand Down
14 changes: 11 additions & 3 deletions src/standard/utils.html
Original file line number Diff line number Diff line change
Expand Up @@ -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);
},

/**
Expand Down Expand Up @@ -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;
Expand Down
5 changes: 2 additions & 3 deletions test/unit/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});

});
Expand Down

0 comments on commit 25aab8b

Please sign in to comment.