maccman/spine — pull request #277 by joelvh (2012-04-26 06:27:18+00:00)
Title: Custom Spine.Model attribute serialization/deserialization

Made it really simple to handle serialization of model attributes. The "deserialize" and "serialize" methods are automatically called on a model when an instance is created or serialized to JSON.

Example: JSON.stringify serializes dates to ISO format, but does not deserialize them. A model instance's "deserialize" method lets you check for an attribute that has the ISO format to convert it to a Date() object. Serialization works in reverse using the "serialize" method.

deserialize: (key, value) ->
switch key
when "date" then return @deserializeISO(value)
else return super

Rather than a "switch" statement inside an overridden serialization method, you can register serializers.

Example: After calling the "configure" method when setting up a model, call model's class method "deserialize" or "serialize", passing in a hash of attribute names and associated serialization functions. Functions can be associated with more than one attribute name by separating by spaces.

class MyModel extends Spine.Model
@configure 'MyModel', 'date', 'start_time', 'end_time'
@deserialize
date: @deserializeRubyDate
'start_time end_time': @deserializeISO