javascript - Meteor JS: How should I bind events to the window in Meteor? -


i trying detect mouseup outside of window in meteor. tried this, window doesn't seem work:

template.layout.events({   'mouseup window' : function(e) {     console.log("mouseup");   } }); 

how should bind events window in meteor?

the code snippet below bind event handler when template created , unbind when template destroyed. should give behavior you're looking for.

var layoutmouseuphandler = function(e) {     console.log('window.mouseup'); };  template.layout.oncreated(function() {     $(window).on('mouseup', layoutmouseuphandler); });  template.layout.ondestroyed(function() {     $(window).off('mouseup', layoutmouseuphandler); }); 

note event bound in oncreated handler, there's chance template not have been rendered yet when event fires. if handler interacts dom, need check that. not bound in onrendered handler because cause mouseup handler bound multiple times if template re-rendered.

edit: serkan mentions below, new ui engine calls onrendered once when template inserted dom. makes better choice oncreated if event handler interact dom.


Comments

Popular posts from this blog

html - Sizing a high-res image (~8MB) to display entirely in a small div (circular, diameter 100px) -

java - IntelliJ - No such instance method -

identifier - Is it possible for an html5 document to have two ids? -