maccman/spine — pull request #171 by mitchlloyd (2011-12-30 04:43:01+00:00)
Title: Bug concerning the integration of relation.coffee and Spine.Model.load

If you look at the update Method (line 78) for the Singleton class in relation.coffee you'll see that the record data being imported (value) has it's foreign key set to the id of the "parent" record.

update: (value) ->
unless value instanceof @model
value = @model.fromJSON(value)

value[@fkey] = @record.id # Here!
value.save()

This usually works because the 'id' happens to be near the beginning of the atts object when Spine.Model's @load method is called:

load: (atts) ->
for key, value of atts
if typeof @[key] is 'function'
@[key](value)
else
@[key] = value
@

However, if the association data happens to come before id in atts, then the id will be undefined when update is called. This results in the foreign key being overwritten with undefined.

value[@fkey] = @record.id # <-- may be undefined here.

I'm using the solution in the pull request to always set the id attribute first in load().

So let's say album @hasOne 'user'. Loading in something like the following would be a problem.

{albumName: "My Album", user: {name: "john"}, id: "ABCD-12345"}

Aside: I saw your discussion with someone about compiling with coffee-script 1.2.0. When I compile with 1.2.0 I get a bunch of changes. Has the code been compiled with an older version of CoffeeScript?