Skip to content

Commit

Permalink
Updating the pull request.
Browse files Browse the repository at this point in the history
  • Loading branch information
mitar committed Apr 4, 2015
1 parent f4e3b23 commit a4bb15d
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 10 deletions.
40 changes: 33 additions & 7 deletions lib.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@ Blaze._getTemplateHelper = (template, name, templateInstance) ->
helper = template.__helpers.get name
if helper is Blaze._OLDSTYLE_HELPER
isKnownOldStyleHelper = true
else if helper?
return wrapHelper bindDataContext(helper), templateInstance
else
return helper
return null

# Old-style helper.
if name of template
Expand All @@ -31,31 +33,55 @@ Blaze._getTemplateHelper = (template, name, templateInstance) ->
template.__helpers.set name, Blaze._OLDSTYLE_HELPER
unless template._NOWARN_OLDSTYLE_HELPERS
Blaze._warn "Assigning helper with `" + template.viewName + "." + name + " = ...` is deprecated. Use `" + template.viewName + ".helpers(...)` instead."
return template[name]
if template[name]?
return wrapHelper bindDataContext(template[name]), templateInstance
else
return null

return null unless templateInstance

# TODO: Blaze.View::lookup should not introduce any reactive dependencies. Can we simply ignore reactivity here? Can this template instance or parent template instances change without reconstructing the component as well? I don't think so. Only data context is changing and this is why templateInstance or .get() are reactive and we do not care about data context here.
component = Tracker.nonreactive ->
templateInstance = templateInstance()
templateInstance.get 'component'
templateInstance().get 'component'

# Component.
if component
if name of component
return wrapHelper component, component[name]
return wrapHelper bindComponent(component, component[name]), templateInstance

if mixin = component.getMixinWith null, name
return wrapHelper mixin, mixin[name]
return wrapHelper bindComponent(mixin, mixin[name]), templateInstance

null

wrapHelper = (component, helper) ->
bindComponent = (component, helper) ->
if _.isFunction helper
_.bind helper, component
else
helper

bindDataContext = (helper) ->
if _.isFunction helper
->
data = Blaze.getData()
data ?= {}
helper.apply data, arguments
else
helper

wrapHelper = (f, templateFunc) ->
# XXX COMPAT WITH METEOR 1.0.3.2
return Blaze._wrapCatchingExceptions f, 'template helper' unless Blaze.Template._withTemplateInstanceFunc

return f unless _.isFunction f

->
self = @
args = arguments

Blaze.Template._withTemplateInstanceFunc templateFunc, ->
Blaze._wrapCatchingExceptions(f, 'template helper').apply self, args

viewToTemplateInstance = (view) ->
# We skip contentBlock views which are injected by Meteor when using
# block helpers (in addition to block helper view). This matches more
Expand Down
10 changes: 7 additions & 3 deletions lookup.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,10 @@ Blaze._getTemplateHelper = function (template, name, templateInstance) {
var helper = template.__helpers.get(name);
if (helper === Blaze._OLDSTYLE_HELPER) {
isKnownOldStyleHelper = true;
} else if (helper != null) {
return wrapHelper(bindDataContext(helper), templateInstance);
} else {
return helper;
return null;
}
}

Expand All @@ -42,7 +44,9 @@ Blaze._getTemplateHelper = function (template, name, templateInstance) {
'.helpers(...)` instead.');
}
}
return template[name];
if (template[name] != null) {
return wrapHelper(bindDataContext(template[name]), templateInstance);
}
}

return null;
Expand Down Expand Up @@ -98,7 +102,7 @@ Blaze.View.prototype.lookup = function (name, _options) {

} else if (template &&
((helper = Blaze._getTemplateHelper(template, name, boundTmplInstance)) != null)) {
return wrapHelper(bindDataContext(helper), boundTmplInstance);
return helper;
} else if (lookupTemplate &&
((foundTemplate = Blaze._getTemplate(name, boundTmplInstance)) != null)) {
return foundTemplate;
Expand Down

0 comments on commit a4bb15d

Please sign in to comment.