viatropos/tower — pull request #240 by edubkendo (2012-08-25 08:10:08+00:00)
Title: Flash client

Adds the flash to the client side of Tower. There is a little bit more necessary to use it, but the api is essentially the same. To use it on the index action, of a client-side users controller for instance, you can use:

```coffeescript
index:(params) ->
@findCollection (error, collection) =>
@flash 'info', 'Hello!'
@flash 'error', 'Warning! Danger Will Robinson'
@flash 'success', 'Yay! You did it!'
@set('getFlash', @flash())
@render 'index'
```

Just like the example in the docs for the server side controller, these look the same, and this example will cause all three to appear. The difference in this situation is that after setting the flash, and before render is called, you have to essentially "prime" the flash with:

```coffeescript
@set('getFlash', @flash())
```
This is due to fundamental differences between how the two types of controller's operate, and out of everything I came up with, was the simplest solution requiring the least code.

A very cool feature of the way these works is that you can also call it from an action on the view, take the "submit" action in the form view, for example:

```coffeescript
submit: (event) ->
@get('resource').save()
users = App.router.get('usersController')
users.flash 'success', 'Yay! You made a user!'
Tower.router.transitionTo('users.index')
return false
```
To set this one, you have to use the router's "get" method to grab the controller first, and call the method on the router. Also, I haven't used it in an action that doesnt cause a transition, but I imagine that the flash would not show up until the next time the UI was repainted. You could probably call something forcing the UI to re-render though, if you needed to use the flash yet keep the user on the same page. Experimentation must ensue. The odd thing here, in this particular case, it only works IF you've manually defined the action on the controller that the action is transitioning to. This is simple enough, in my case I simply copied the default action from tower / packages / tower-controller / client / actions.coffee:

```coffeescript
index: (params) ->
@findCollection (error, collection) =>
@render 'index'
```

When I update the docs, I'll include these for reference. Again, a little more work to use than the server-side versions, but still much easier than rolling your own, and keeping to the same API. I really tried to make them exactly the same but as far as I can tell it's not possible without re-writing a lot more of tower than I'm comfortable doing, and even then, I wouldn't be sure how to do it.

Will update the docs soon, and do more experimenting so I can include more examples, but for the most part it should act work mostly like the originals. Also, the server side API still works for those using server side routing/rendering.