jeudi 24 novembre 2016

Using JSON API response within Ember component

I have a component I'd like to use to create permalinks (hasherize) some titles for pages on a website CMS thingy I'm creating. I figure since I do this fairly often I should create a component so I can reuse it.

The component just listens to keystrokes on a text input, and uses an ajax call to fetch an API response that will (hopefully) return a single data record containing the permalink.

The component is not using a model because the permalink isn't a record or anything. I'm just using plain ajax utilizing the ember-ajax module. How do I deal with the somewhat 'complex' JSON API responses I'll be getting? Do I need to dig into them manually to get what I want? On a normal 200 response this isn't tough. Errors though, are a different story.

If so, how do I deal with errors? Let's say my permalink cannot be created for some reason (maybe it's a duplicate title and the permalink needs to be unique), I will get a 422 response, meaning it'll be an error object. How would I parse that into usable information?

Here is an example error response:

{
    "errors": [
         {
              "status":"422",
              "detail":"Error analyzing permalink: Permalink for 'test-page-title' already exists.",
              "source":{
                  "pointer":"data/attributes/permalink"
              }
         }
    ]
}

Is using a component for this even what I should be doing?




Aucun commentaire:

Enregistrer un commentaire