Permalink
Browse files

renamed 'on' to 'upon', added callbacks

  • Loading branch information...
1 parent 2fc1774 commit 797f005a7783b377236de1b3a31a7ba5db83877e @dnagir committed Dec 20, 2011
Showing with 18 additions and 19 deletions.
  1. +3 −9 README.md
  2. +6 −5 lib/assets/javascripts/knockout/model.js.coffee
  3. +9 −5 spec/javascripts/knockout/model_spec.js.coffee
View
@@ -156,7 +156,7 @@ class @Page extends ko.Model
@configure 'page'
# Subscribe to 'sayHi' event
- @on 'sayHi', (name) ->
+ @upon 'sayHi', (name) ->
alert name + @name
page = Page.new name: 'Home'
@@ -175,22 +175,16 @@ Some of them are:
class @Page extends ko.Model
@configure 'page'
- beforeSave: ->
+ @beforeSave ->
@age = @birthdate - new Date()
- beforeValidation: ->
- @name ||= 'Default page'
-
# This would be similar to
class @Page extends ko.Model
@configure 'page'
- on 'beforeSave', ->
+ @on 'beforeSave', ->
@age = @birthdate - new Date()
-
- on 'beforeValidation', ->
- @name ||= 'Default page'
```
@@ -19,11 +19,13 @@ class Module
Events =
ClassMethods:
- extended: -> @include Events.InstanceMethods
- on: (eventName, callback) ->
+ extended: ->
@events ||= {}
+ @include Events.InstanceMethods
+ upon: (eventName, callback) ->
@events[eventName] || = []
@events[eventName].push callback
+ this # Just to chain it if we need to
InstanceMethods:
trigger: (eventName, args...) ->
@@ -34,9 +36,7 @@ Events =
Callbacks =
ClassMethods:
- tbd: ->
- InstanceMethods:
- tbd: ->
+ beforeSave: (callback) -> @upon('beforeSave', callback)
Ajax =
ClassMethods:
@@ -62,6 +62,7 @@ Ajax =
toJSON: -> ko.mapping.toJS @, @mapping()
save: ->
+ @trigger('beforeSave') # Consider moving it into the beforeSend or similar
data = {}
data[@constructor.className] =@toJSON()
params =
@@ -1,7 +1,10 @@
class Page extends ko.Model
- @configure 'page'
- @on 'sayHi', (hi) ->
- @sayHi = hi
+ @configure 'page'
+ @upon 'sayHi', (hi) ->
+ @sayHi = hi
+
+ @beforeSave ->
+ @beforeSaved = true
describe "Model", ->
@@ -95,8 +98,9 @@ describe "Model", ->
expect(@page.sayHi).toBe 'abc'
describe "callbacks", ->
- it "beforeSave should be called ", -> expect('tbd').toBe 'done'
- it "beforeValidation should be called", -> expect('tbd').toBe 'done'
+ it "beforeSave should be called ", ->
+ @page.save()
+ expect(@page.beforeSaved).toBeTruthy()
describe "validations", ->
it "should execute the validator", -> expect('tbd').toBe 'done'

0 comments on commit 797f005

Please sign in to comment.