Created
November 8, 2017 15:00
-
-
Save mandymozart/626334df161cb49947e9ee986ce1dbca to your computer and use it in GitHub Desktop.
Extend controller for parent event bubbling
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Attache publish bindings for event manager | |
* TODO requires EventManager to be injected into each controller on factory | |
* [data-publish] {string} message | |
* [data-payload]? {string} payload (TODO maybe json or object) | |
* [data-event]? {ZeptoEventHandlers} @default click | |
* focusin focusout focus blur load resize scroll unload click dblclick | |
* mousedown mouseup mousemove mouseover mouseout mouseenter mouseleave | |
* change select keydown keypress keyup error | |
*/ | |
public attachPublishEvents(){ | |
this.$el.find('[data-publish]').each((index: number, item: Element) => { | |
let eventType = (item.hasAttribute('data-event')) ? item.getAttribute('data-event') : 'click' | |
$(item).bind(eventType,(e: Event) => { | |
e.preventDefault() | |
let payload = (item.hasAttribute('data-payload')) ? item.getAttribute('data-payload') : null | |
let message = item.getAttribute('data-publish') | |
// Parent Cuid | |
let parentControllerAttributes = this.$el.get(0).parentNode.attributes | |
let i = parentControllerAttributes.length | |
console.log(parentControllerAttributes) | |
let parentCuid: string | |
while(i--){ | |
let attr = parentControllerAttributes[i] | |
if(/_sws-/g.test(attr.name)) { | |
this.parentCuid = attr.name.substr(5) | |
break | |
} | |
} | |
this.events.publish(this.parentCuid + ':' + message,payload) | |
console.log('Controller::publishEvent:',eventType,this.parentCuid + ':' + message,payload) | |
}) | |
return true | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment