forked from tower-archive/tower
-
Notifications
You must be signed in to change notification settings - Fork 0
callbacks
Lance Pollard edited this page Apr 12, 2012
·
1 revision
Tower supports 3 main callbacks:
before
around
after
The following callbacks are implemented:
@after "initialize"
@before "validate"
@after "validate"
@before "create"
@after "create"
@before "update"
@after "update"
@before "save"
@after "save"
@before "destroy"
@after "destroy"
Callbacks are available on any model.
class App.Post extends Tower.Model
@field "title", type: "String"
@field "slug", type: "String"
@before "save", "generateSlug"
generateSlug: ->
@set "slug", @get("title").replace(/[^a-z0-9]+/, '-').toLowerCase()
class App.Post extends Tower.Model
@field "title", type: "String"
@field "slug", type: "String"
@callback "save", "before", "generateSlug"
generateSlug: ->
@set "slug", @get("title").replace(/[^a-z0-9]+/, '-').toLowerCase()
class App.Post extends Tower.Model
@field "title", type: "String"
@field "slug", type: "String"
@before "save", ->
@set "slug", @get("title").replace(/[^a-z0-9]+/, '-').toLowerCase()
If you have a callback that executes asynchronous code, you can add the callback
argument to your function, and call it when complete:
class App.Post extends Tower.Model
@field "title", type: "String"
@field "url", type: "String"
@before "save", "scrapeWebsite"
scrapeWebsite: (callback) ->
SomeCrawler.scrapeHTML @get("url"), (error, html) ->
callback(error)