Where should I define a `save` action?

I’ve started playing with using command objects to perform complex business logic to slim down the router.

Eg a simplified example where after saving a task we enqueue a background job to send an email:

TasksRoute = Ember.Route.extend
  actions:
    save: (task) ->
      @commandFor("create-task").exec(task).then =>
        @transitionTo("task", task)
CreateTask = Ember.Object.extend
  project: alias("controller.project")
  person: alias("current.person")

  exec: (task) ->
    task.save().then =>
      @queue.publish("send-email", "new-task", {
        by: @get("person")
        project: @get("project")
      })

The routing/transitioning logic stays in the route, with commandFor the command automatically gets wired up with the current controller and some other injected stuff but doesn’t have access to the route.

In the actual app some of these command objects get quite large but are still nicely self contained & easy to test whilst keeping the routes easy to see what’s going on.

Similar to the skinny model, skinny controller pattern in rails.

5 Likes