 
   null
 
+viewToTemplateInstance = (view) ->
+  # We skip contentBlock views which are injected by Meteor when using
+  # block helpers (in addition to block helper view). This matches more
+  # the visual structure of templates and not the internal implementation.
+  while view and (not view.template or view.name is '(contentBlock)')
+    view = view.originalParentView or view.parentView
+
+  # Body view has template field, but not templateInstance. We return null in that case.
+  return null unless view?.templateInstance
+
+  _.bind view.templateInstance, view
+
 addEvents = (view, component) ->
   for events in component.events()
     eventMap = {}

  # Component-level data context. Reactive. Use this to always get the
  # top-level data context used to render the component.
  data: ->
    Blaze.getData @templateInstance.view

  # Caller-level data context. Reactive. Use this to get in event handlers the data
  # context at the place where event originated (target context). In template helpers
  # the data context where template helpers were called. In onCreated, onRendered,
  # or onDestroyed, the same as @data(). Inside a template this is the same as this.
  currentData: ->
    Blaze.getData()

# We copy utility methods ($, findAll, autorun, subscribe, etc.) from the template instance prototype.
for methodName, method of Blaze.TemplateInstance::

        eventMap[spec] = (args...) ->
          event = args[0]

          currentView = Blaze.getView event.currentTarget
          templateInstance = viewToTemplateInstance currentView

          # We set template instance based on the current target so that inside event handlers
          # BlazeComponent.currentComponent() returns the component of event target.
          Template._withTemplateInstanceFunc templateInstance, ->
            # We set view based on the current target so that inside event handlers
            # BlazeComponent.currentData() (and Blaze.getData() and Template.currentData())
            # returns data context of event target and not component/template.
            Blaze._withCurrentView currentView, ->
              handler.apply component, args

          # Make sure CoffeeScript does not return anything. Returning from event
          # handlers is deprecated.
