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
2 Answers
Reset to default 8To overwrite the way models are saved and loaded from the database you can overwrite two Methods.
Model.toJSON
place custom serialization logic here.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
版权声明:本文标题:php - Backbone.js serializing the models attributes for syncing - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744133340a2592277.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论