Quantcast
Channel: Sacramento - JavaScript, Drupal - JavaScript
Viewing all articles
Browse latest Browse all 30

Backbone.js IS opinionated, or why is using nested models and collections in Backbone so hard?

$
0
0

Background
So after diving into the subject of nested models/collections in Backbone.js and being very frustrated at how Backbone doesn't handle them, I eventually discovered a comment by Backbone.js creator, Jeremy Ashkenas on GitHub:

"The regular party line on nested models / nested data queries / change events on deep properties is available here: http://backbonejs.org/#FAQ-nested

To reiterate, in brief -- Nested data objects are usually way less useful in JS than intelligently-organized "flat" data objects.

Flat models can be more easily manipulated relationally, and trivially loaded from and persisted to relational databases.

With nested models, you either reinvent querying into arbitrary JSON objects (like backbone-deep-model seems to do), or end up having to include something as large as JSONPath.

Flat models make it easy to serialize and persist atomic changes to the state of your app -- folks using deeply nested models often end up sending huge quantities of redundant data over the wire.

Basically -- we want to favor the normalized, relational approach to data modeling on the client side, over the "giant single global blob of deeply nested JSON" approach"

Confusion
...these comments by Jeremy confused me so much when I first read them that I quickly realized I must be missing something very basic. It turns out I was right. Hopefully some of the answers I came up with might help someone else in their decision making process about how to implement their Backbone.js models.

Clarity

"we want to favor the normalized, relational approach to data modeling on the client side, over the "giant single global blob of deeply nested JSON" approach

…what is not explicitly mentioned in that quote, but I believe important to understand (and which I did not previously understand), is that such an approach CAN be used with noSQL databases. That is to say that embedded/document-oriented schemas (e.g., that result in the 'single blob of nested JSON') are NOT the only schema one can use even with a noSQL context - a developer can instead create a flat schema by using references.


Viewing all articles
Browse latest Browse all 30

Latest Images

Trending Articles



Latest Images