Reputation: 506
How can I create a event which will run on any page?
When creating a helper on the main layout template it doesn't work.
Template.layout.events
'click': ->
console.log "you clicked on the site"
Upvotes: 12
Views: 2700
Reputation: 3184
Neither answer currently is 'wrong' per se, they just work in different situations:
Event maps, Template.body.events({})
, on Template.body do not get applied to elements added to the body via Blaze.render, Iron-Router, Flow-Router jQuery, or the DOM API, or to the body element itself. If you are using the above you will need to add the package body-events
to be able to utilise the event map on body using the command below:
meteor add gwendall:body-events
If you are not using any of the template rendering packages above then you can directly add to the body event map using the code below:
Template.body.events({
'click': function () {
alert("clicked on the page. oh yeah!");
}
});
nb. it seems the latest version of Blaze allows directly adding event maps to body
Upvotes: 2
Reputation: 1045
you need a special package to create a global event.
install
meteor add gwendall:body-events
and you can use the events from Template.body.events
in every template
example
Template.body.events({
'click .myClass':function(){
alert("BODY EVENT");
}
});
or if you like the new syntax
Template.body.events({
'click .myClass'(){
alert("BODY EVENT");
}
});
Upvotes: 6
Reputation: 75945
You can use Template.body.events
instead of Template.layout.events
to create events for any template that will apply on anything in <body>
Docs: http://docs.meteor.com/#/full/template_body
Upvotes: 15