admin管理员组

文章数量:1400024

I am trying to save a fairly plex model including embedded collections back to a relational database. Due to the embedded collections The data returned to the server contains objects which is fair enough. I am however building the backbone app on top of an already existing application and have to return the values in scalar form to be able to re-use the server side code. What is the best of going about this, I was thinking of overriding the model's toJSON function however I don't really feel like it should be. So the other alternative that I can think of is to overwrite the sync method and do it there. However even that doesn't seem right. Am I missing something or is overwriting the sync method a necessary evil?

I am trying to save a fairly plex model including embedded collections back to a relational database. Due to the embedded collections The data returned to the server contains objects which is fair enough. I am however building the backbone app on top of an already existing application and have to return the values in scalar form to be able to re-use the server side code. What is the best of going about this, I was thinking of overriding the model's toJSON function however I don't really feel like it should be. So the other alternative that I can think of is to overwrite the sync method and do it there. However even that doesn't seem right. Am I missing something or is overwriting the sync method a necessary evil?

Share Improve this question edited Dec 20, 2012 at 22:07 hakre 198k55 gold badges449 silver badges855 bronze badges asked Jun 27, 2011 at 14:49 luxeramaluxerama 1,1192 gold badges14 silver badges31 bronze badges 2
  • 3 Overwriting the models .toJSON method is the remended approach. – Raynos Commented Jun 27, 2011 at 14:52
  • @Raynos, would you mind adding your ment as an answer, otherwise I cannot nominate it as the one that worked for me? Thanks v. much for your help – luxerama Commented Jul 1, 2011 at 8:42
Add a ment  | 

2 Answers 2

Reset to default 8

To overwrite the way models are saved and loaded from the database you can overwrite two Methods.

  1. Model.toJSON place custom serialization logic here.
  2. Model.parse place custom de-serialization logic here.

Ideally you only have custom serialization / de-serialization logic to "optimise" the database. I.e. if you have an Age and DateOfBirth field you only store one in the database in Model.toJSON and calculate the other in Model.parse.

If you need custom serialization / de-serialization logic that is NOT model specific then overwrite Backbone.Sync.

You can also overwrite model.Sync. This means the model will use a specific custom Sync function rather then using Backbone.Sync

I think your idea to overwrite the sync method is exactly right. toJSON should always return JSON, if it returned something other than JSON, other programmers might find it difficult to understand your code.

本文标签: phpBackbonejs serializing the models attributes for syncingStack Overflow