How do you query nested attributes in JSON?

That celebration didn’t last long… don’t even know how to ask a question because I’m not sure there’s an answer. JSON will only return one particular term for this route. If you remove the term from JSON, the route goes blank like the rest. If you add another group to the term, Ember Data has no problem returning the information for the other group but the term won’t show up on the other page. JSON is formatted the same for every term and works fine in the other routes.

Here’s a 58-second look at the problem… pray for me. :tired_face:

(repo for anyone that’s interested)

Check out this Video

Check out this video: https://embed.vidyard.com/share/ZfAntSBqPmjQxtVBgMFxXq

Backed the JSON all the way down to 2 terms and still stuck in the same circle returning all terms or zero terms. Pretty sure the problem is querying nested attributes… anyone know how to do that?

For instance:

  {
        "id": "octahedral_layer",
        "term": "octahedral layer",
        "letter": "o",
        "disciplines": [
          { "title": "drilling_fluids" },
          { "title": "drilling" }
        ],
          "definitions": [
            {
              "speech_type": "1. n.",
              "category": "Drilling Fluids",
              "definition": "<div class=\"definition-text\">\n One of the layers that constitute the atomic\n <a href=\"/en/Terms/s/structure.aspx\">\n  structure\n </a>\n of the\n <a href=\"/en/Terms/c/clay.aspx\">\n  clay\n </a>\n <a href=\"/en/Terms/g/group.aspx\">\n  group\n </a>\n of layered\n <a href=\"/en/Terms/s/silicate.aspx\">\n  silicate\n </a>\n minerals. The structure of these minerals can consist of two, three or four layers. The octahedral\n <a href=\"/en/Terms/l/layer.aspx\">\n  layer\n </a>\n is a plane of aluminum hydroxide octahedra (aluminum at the center and hydroxides at all six corners). Another\n <a href=\"/en/Terms/s/structural.aspx\">\n  structural\n </a>\n layer is a plane of silicon dioxide tetrahedra (silicon at the center and oxygen at all four corners of the tetrahedron). The tetrahedral and octahedral layers fit one on top of the other, with oxygen atoms being shared as oxide and hydroxide groups.\n</div>\n",
              "see": [
                {
                  "title": "bentonite",
                  "link": "https://www.glossary.oilfield.slb.com/en/Terms/b/bentonite.aspx"
                },
                {
                  "title": "silica layer",
                  "link": "https://www.glossary.oilfield.slb.com/en/Terms/s/silica_layer.aspx"
                },
                {
                  "title": "tetrahedral layer",
                  "link": "https://www.glossary.oilfield.slb.com/en/Terms/t/tetrahedral_layer.aspx"
                }
                ],
                "more_details": [],
                "synonyms": [],
                "antonyms": []
            }
        ]
      }

I’m able to get:

/api/terms?disciplines=drilling_fluids

But how would you get?

/api/terms?disciplines.title=drilling_fluids

Was able to get the query going with:

export default Route.extend({
  model(params) {
    return this.store.query("term", { "disciplines.title": params.disciplines });
  }
});

Sadly, it still returns all of the terms in the store. :disappointed:

If anyone’s got any ideas I’d love to hear them.

Just got the answer via #help on Discord.

    if (disciplines) {
      results = results.filter(term => {
      return term.disciplines.some(discipline => discipline.group === disciplines);
      });

Works!!

The issue I see there is a backend implementation. I don’t think ember-data or Ember offer anything special in this regard. What you pass to .query() is given to the backend and the backend does what it wants to with it.

Either update the backend to do the right thing or you could post-process the data.

As for deep diving into POJO (A.K.A. JSON) objects there are several solutions. A cheap one is Ember’s get() helper. Alternatively you might like more functional options like maybe-simple (shameless self promotion) or even a very small utility.

There are a huge number of deep diving utilities out there. Even jQuery and Underscore/Lodash have them. And soon ES7 might offer optionals as a builtin language feature.

1 Like