Skip to content
This repository has been archived by the owner on Jan 12, 2018. It is now read-only.

Commit

Permalink
Make sure model property values are cached on initialize
Browse files Browse the repository at this point in the history
  • Loading branch information
Ivan Navarrete and Jonas Nicklas authored and jnicklas committed Oct 21, 2013
1 parent aaf11ae commit 6851840
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/model.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ class Model
for own name, value of attributes
property = @[name + "_property"]
if property
property.update(value)
property.initialize(value)
else
@[name] = value

Expand Down
4 changes: 4 additions & 0 deletions src/property.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ class PropertyAccessor
@event = new Event(@object, @name + "_change", @definition.eventOptions)
@_gcQueue = []

initialize: (value) ->
@update(value)
@_oldValue = value

update: (value) ->
if @definition.set
@definition.set.call(@object, value)
Expand Down
6 changes: 6 additions & 0 deletions test/model.spec.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ describe 'Serenade.Model', ->
expect(nameTriggered).to.be.false
expect(ageTriggered).to.be.false

it 'keeps track of constructor values for change events', ->
class Person extends Serenade.Model
@property "name"
john = new Person(name: 'John')
expect(-> john.name = 'Peter').to.triggerEvent(john.name_property, with: ["John", "Peter"])

describe '.extend', ->
it 'sets up prototypes correctly', ->
Test = Serenade.Model.extend()
Expand Down

0 comments on commit 6851840

Please sign in to comment.