Skip to content
Lance Pollard edited this page Apr 12, 2012 · 1 revision

Tower.Model.Callbacks

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.

Define a callback with the callback phase helpers

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()

Define the phase and callback directly

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()

Define callbacks with anonymous functions

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()

Callbacks can be asynchronous

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)

Tower.js

Everything here will be reflected on http://towerjs.org/guides.

Clone this wiki locally