PaulUithol / Backbone-relational

Get and set relations (one-to-one, one-to-many, many-to-one) for Backbone models
http://backbonerelational.org
MIT License
2.34k stars 330 forks source link

Dot notation get() clarification #550

Closed RaphaelDDL closed 8 years ago

RaphaelDDL commented 8 years ago

Hello :)

Not complaining, I just wanted to understand better about commit https://github.com/PaulUithol/Backbone-relational/commit/742796a804c2fe6ce2d077bb0c4948f7001aac26

So I'd like to ask you: Could you clarify why "Remove the dot notation syntax for getters; it just doesn't belong in the scope of BBR" ?

From my understanding, while isn't exactly a relational-specific feature, it does have it's merits since it's a "shorthand" version of get specially useful for accessing nested models.

Without it, accessing something needs to be this.model.attributes.this.attributes.that.attributes.key (big lame 'n wrong way) or chain gets this.model.get('this').get('that').get('key'). But of course that means having to do ifs to test for each nested with has before actually calling get. That's because a nested might not available (backend response for the relational key was null so the relational model wasn't created, for example) and would throw an error of calling get on undefined.

Sorry the question wasn't sooner. I just noticed this now that we upgraded relational to latest and code/tests broke. http://backbonerelational.org/#change-log didn't mention anything about the get so I had to go looking through the diffs/commits :P

Best Regards, Raphael

bpatram commented 8 years ago

I might suggest you use this library: https://github.com/amccloud/backbone-dotattr if you require that functionality. I can't say for sure if this library will work out of the box however.

It makes it difficult to debug get() calls using .get('foo.bar.baz') in the case that foo or bar may not exist. Does an error get thrown? Does it just return null instead? Should it return a promise and use getAsync() instead?

Although I cannot comment on the exact reasoning why @PaulUithol originally removed this, I can see how a feature like this could spiral into some confusion for devs unsure of where in their chain of 'foo.bar.baz' has the issue. When you call .get('foo').get('bar').get('baz') you'll know exactly where the error occurs if one of those don't exist.