DockYard / ember-route-action-helper

Bubble closure actions in routes
MIT License
329 stars 48 forks source link

Call `route-action` logic inside a route or mixin. #52

Open viniciussbs opened 7 years ago

viniciussbs commented 7 years ago

Hi.

Inside my app a use component helper to dynamically call a component. But the attributes may vary depending of which component I'm calling.

Let's say I have this code:

{{#if isUserModal}}
  {{modal/user-modal user=model like=(route-action "like")}}
{{else if isEventModal}}
  {{modal/event-modal event=model like=(route-action "like") join=(route-action "join")}}
{{/if}}

I would like to write it this way:

{{component componentName modalModel=model modalActions=modalActions}}

In this case, modalActions is a hash with the actions. But, to do this, I need a way to use route-action logic inside a mixin. The changeset helper uses internally a Changetset class that can be used anywhere. It would be great to have a RouteAction class, too.

lolmaus commented 7 years ago

When you use the component helper, your polymorphic components must have identical arguments.

In your example, you have to refactor user=model and event=model into a common name: modalModel.

But if some of the components needs a unique argument, you'll be passing it as usual, e. g. eventGroup=eventGroup. A user modal would simply ignore that argument.

You already know that. So why don't you apply the same principle to actions? Use common names for shared actions, use unique names for non-shared actions.

In this example I've separated shared attributes and actions from unique attributes and actions:

{{component componentName
  modalModel   = model
  like         = (route-action "like")

  eventGroup   = eventGroup
  joinEvent    = (route-action "join")
}}

This approach does not require making such changes to ember-route-action-helper that are unlikely to be used by anyone else.