From eeb20673b3c68003df5d3b0d409a288d72edd14b Mon Sep 17 00:00:00 2001 From: Nathan Sobo Date: Tue, 27 Dec 2011 19:13:54 -0600 Subject: [PATCH] Pass the event and element to event-handling methods on the view. --- spec/stdlib/template-spec.coffee | 8 ++++++++ src/stdlib/template.coffee | 10 ++++------ 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/spec/stdlib/template-spec.coffee b/spec/stdlib/template-spec.coffee index e8d409ccb..b33a59cac 100644 --- a/spec/stdlib/template-spec.coffee +++ b/spec/stdlib/template-spec.coffee @@ -48,6 +48,14 @@ fdescribe "Template", -> it "binds events for elements with event name attributes", -> spyOn(view, 'li1Clicked') spyOn(view, 'li2Keypressed') + spyOn(view, 'li1Clicked').andCallFake (event, elt) -> + expect(event.type).toBe 'click' + expect(elt).toMatchSelector 'li.foo:contains(one)' + + spyOn(view, 'li2Keypressed').andCallFake (event, elt) -> + expect(event.type).toBe 'keypress' + expect(elt).toMatchSelector "li.bar:contains(two)" + view.li1.click() expect(view.li1Clicked).toHaveBeenCalled() expect(view.li2Keypressed).not.toHaveBeenCalled() diff --git a/src/stdlib/template.coffee b/src/stdlib/template.coffee index 43c723cf4..d4585a360 100644 --- a/src/stdlib/template.coffee +++ b/src/stdlib/template.coffee @@ -35,11 +35,9 @@ class Template view[outletName] = elt bindEvents: (view) -> - for event in this.constructor.events - view.find("[#{event}]").each -> + for eventName in this.constructor.events + view.find("[#{eventName}]").each -> elt = $(this) - methodName = elt.attr(event) - elt[event](-> view[methodName]()) - - + methodName = elt.attr(eventName) + elt[eventName]((event) -> view[methodName](event, elt))