Created
April 21, 2012 14:57
Revisions
-
matthewwithanm created this gist
Apr 21, 2012 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,31 @@ // 1. Relying on pluralization can be tricky. var payload = { "deer": { "id": 1, "name": "Bambi", "related_deer": [1, 2, 3] }, "deerses": [ // ???? {"id": 2, ...}, {"id": 3, ...} ] }; // 2. Getting result object requires knowing requested type. // 3. Method of accessing the result is different for every type. doSomethingWithPayload(payload); // Oops. doSomethingWithResult needs to know that "deer" is the special key for this payload. // 4. Related object lookups require special logic for requested type. var relatedDeer = payload.deerses.filter(function(o) { return payload.deer.related_deer.indexOf(o.id) !== -1; }); // Oops! Won't work because Bambi isn't in deerses list. // 5. The solution can't be extrapolated for collections. (We can't use the plural key because there'd be no way to differentiate between results and objects related to results. Keeping the results and related objects under different keys just exacerbates issue 4.) var payload = { "deerses": [ // Which match our request? {"id": 2, ...}, {"id": 3, ...} ] };