Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Some nice features to add #26

Open
ccorcos opened this issue Feb 17, 2015 · 8 comments
Open

Some nice features to add #26

ccorcos opened this issue Feb 17, 2015 · 8 comments

Comments

@ccorcos
Copy link

ccorcos commented Feb 17, 2015

Hey there, I wrote some convenient functions that you may like to include in this package. They don't use your internal functions so you would probably want to do that.

Blaze.TemplateInstance.prototype.getParentNamed = (name) ->
  template = this
  while template
    if name is template.view.name.split('.').splice(1).join('.')
      return template

    view = template.view.originalParentView or template.view.parentView

    while view and (not view.template or view.name is '(contentBlock)')
      view = view.originalParentView or view.parentView

    template = view.templateInstance?()

  return template

Blaze.TemplateInstance.prototype.trigger = (name) -> triggerEventHelper(name, this, null)

@trigger = (name) -> (e,t) -> triggerEventHelper(name, t, e)

triggerEventHelper = (name, template, event) ->
  while template
    if template.view.template.__eventMaps
      for eventMap in template.view.template.__eventMaps
        if eventMap[name]
          eventMap[name].apply(template.view, [event, template])

    view = template.view.originalParentView or template.view.parentView

    while view and (not view.template or view.name is '(contentBlock)')
      view = view.originalParentView or view.parentView

    template = view.templateInstance?()
@ccorcos
Copy link
Author

ccorcos commented Feb 17, 2015

trigger allows you to call a parent template event

@mitar
Copy link
Collaborator

mitar commented Feb 17, 2015

Interesting. Otherwise things are easier to discuss though a pull request. And also iterate on that.

I think that it would be maybe more useful to extend template.parent(numLevels, includeBlockHelpers) in a way that numLevels could also be a function, and then you could pass in any filter function you like, for example one which would find you a template instance with a given name.

For trigger I do not have an opinion. Can you describe the use case?

@ccorcos
Copy link
Author

ccorcos commented Feb 17, 2015

That would be a nice high order function!

Trigger is inspired to make UI components. For example, you may have nav component and you want to pass its nav events to the responsible template.

I think it would be far more useful if it could call events not just on its parents but any template. Not sure how to do that though... Im basically just bastardizing the Template.events to work as a generalized event emitter as well...

@aldeed
Copy link
Collaborator

aldeed commented Feb 17, 2015

If we add trigger to instance prototype, I think it should trigger on that instance. So you can do instance.parent().trigger('custom-event', arg1, arg2), which allows triggering on any template instance in the ancestor tree.

EDIT: Not sure whether the "bubbling" in your code is necessary. Guess I'd have to see what the various exact use cases are.

@ccorcos
Copy link
Author

ccorcos commented Feb 17, 2015

that makes sense actually. We can then invert the logic a bit. As to @mitar's suggestion, we could pass a filtering function to findAncestor to see if the template has the event and then trigger it.

instance.ancestors(hasEvent(eventName)).trigger(eventName, args...)

@ccorcos
Copy link
Author

ccorcos commented Feb 17, 2015

I think it would be very convenient if Template events could be treated as you might an event emitter that automatically unsubscribe when they're destroyed. An example would be if you had two sibling templates -- content and menu. Suppose menu is a swappable menu. It would be great if Menu can communicate to Content when its opened and it would be great if the content could communicate to the menu that it has been tapped (so close the menu).

This is all part of some brainstorming I've been doing into making components with Blaze.

https://github.com/ccorcos/meteor-blaze-components

@aldeed
Copy link
Collaborator

aldeed commented Feb 17, 2015

Cool, I have been interested in components from the autoform perspective. The componentization already within autoform works pretty well, but is specific to that usage. End goal would be to support whatever MDG or the community settles on for components.

@aldeed
Copy link
Collaborator

aldeed commented Feb 17, 2015

Oh, and you can submit a PR for these things, or one of us will add them when we have time.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants