maccman/spine — pull request #384 by tomchentw (2012-09-21 08:16:08+00:00)
Title: A lot of change on Model behaviors...

After I dig into Spine.Model source codes, I found that I can use prototype object to create model instances and keep them in sync. So we can decouple instance from constructor's events.

it requires:
1. use Model.new to get the new instance of model. (eg. User.new to get a user instance)
2. prevent usage of directly call new Model to create instance. (eg. DONT USE new User() to get a user instance)
3. keep the prototype instance inside Model's protection.
4. keep references to uncreated instances

So when we want all model instances:
User.all() # returns [User, User], all are cloned object( user.__proto__ is prototype instance and should not be access outside Model's methods!)

Same policy applied to User.first(), last(), find(), findByAttr() methods, and definitely new() method.
All these cloned objects methods: [bind, one, trigger, unbind] will be delegate to user.__proto__, so we dont need bind to User and check equality in the callbacks.

Due to these changes, I changed spine.js, relation.js and ajax.js. Also change Model.refresh and add Model.prototype.refresh for attr setting. Note that the Model.refresh should be called for (1) init bootstraping data, or (2) after server response and we want a update. The Model.prototype.refresh should be called (1) by Model.prototype.create/update ( called by Model.prototype.save) internally, and (2) after server response for POST/PUT. As u can see, these methods should be call with care, and it wont trigger any callbacks since it's for updating prototype object.

Please let me know if something need to fix, and tests are welcomed. Thank you.