Skip to content

Instantly share code, notes, and snippets.

@matthewwithanm
Created April 21, 2012 14:57

Revisions

  1. matthewwithanm created this gist Apr 21, 2012.
    31 changes: 31 additions & 0 deletions :(.js
    Original 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, ...}
    ]
    };