Skip to content

Commit

Permalink
Use existing add/removeEventListener for elements
Browse files Browse the repository at this point in the history
This makes it possible to use can-define with HTML elements so that both
property events and DOM events can be listened to and fire correctly.

The method for doing this is to call to the existing `addEventListener`
if one exists, otherwise to do the default action of calling to
eventLifecycle.addAndSetup.

Closes #145
  • Loading branch information
matthewp committed Feb 7, 2017
1 parent 4fbb7eb commit 237ef58
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 9 deletions.
22 changes: 14 additions & 8 deletions can-define.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,9 @@ module.exports = define = ns.define = function(objPrototype, defines, baseDefine
return data;
});

// Add in the base event methods
define.addBaseEvents(objPrototype);

// Add necessary event methods to this object.
for (var prop in eventsProto) {
Object.defineProperty(objPrototype, prop, {
Expand Down Expand Up @@ -280,7 +283,7 @@ make = {
if (newVal !== current) {
setData.call(this, newVal);

canEvent.dispatch.call(this, {
canEvent.trigger.call(this, {
type: prop,
target: this
}, [newVal, current]);
Expand Down Expand Up @@ -644,7 +647,6 @@ assign(eventsProto, {
_eventSetup: function() {},
_eventTeardown: function() {},
addEventListener: function(eventName, handler) {

var computedBinding = this._computed && this._computed[eventName];
if (computedBinding && computedBinding.compute) {
if (!computedBinding.count) {
Expand All @@ -656,10 +658,7 @@ assign(eventsProto, {

}

var baseAddEventListener = this.__proto__.addEventListener ||
eventLifecycle.addAndSetup;

return baseAddEventListener.apply(this, arguments);
return this._baseEvents.addEventListener.apply(this, arguments);
},

// ### unbind
Expand All @@ -678,8 +677,7 @@ assign(eventsProto, {

}

return eventLifecycle.removeAndTeardown.apply(this, arguments);

return this._baseEvents.removeEventListener.apply(this, arguments);
}
});
eventsProto.on = eventsProto.bind = eventsProto.addEventListener;
Expand Down Expand Up @@ -721,6 +719,14 @@ define.setup = function(props, sealed) {
};
define.replaceWith = replaceWith;
define.eventsProto = eventsProto;
define.addBaseEvents = function(objPrototype){
objPrototype._baseEvents = objPrototype.addEventListener &&
objPrototype.addEventListener !== eventsProto.addEventListener ?
{ addEventListener: objPrototype.addEventListener,
removeEventListener: objPrototype.removeEventListener } :
{ addEventListener: eventLifecycle.addAndSetup,
removeEventListener: eventLifecycle.removeAndTeardown };
};
define.defineConfigurableAndNotEnumerable = defineConfigurableAndNotEnumerable;
define.make = make;
define.getDefinitionOrMethod = getDefinitionOrMethod;
Expand Down
1 change: 1 addition & 0 deletions list/list.js
Original file line number Diff line number Diff line change
Expand Up @@ -1056,6 +1056,7 @@ assign(DefineList.prototype, {


// Add necessary event methods to this object.
define.addBaseEvents(DefineList.prototype);
for (var prop in define.eventsProto) {
DefineList[prop] = define.eventsProto[prop];
Object.defineProperty(DefineList.prototype, prop, {
Expand Down
3 changes: 2 additions & 1 deletion map/map.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ var setProps = function(props, remove) {
}
else if( ("replace" in curVal) && isArray(newVal)) {
curVal.replace(newVal);
}
}
else if( ("set" in curVal) && (isPlainObject(newVal) || isArray(newVal))) {
curVal.set(newVal, remove);
}
Expand Down Expand Up @@ -259,6 +259,7 @@ var DefineMap = Construct.extend("DefineMap",{
});

// Add necessary event methods to this object.
define.addBaseEvents(DefineMap.prototype);
for(var prop in define.eventsProto) {
DefineMap[prop] = define.eventsProto[prop];
Object.defineProperty(DefineMap.prototype, prop, {
Expand Down

0 comments on commit 237ef58

Please sign in to comment.