ember member actions normalizing response - ember.js

I am using ember member API actions and was wondering how can response be normalized in such cases.
I have a model "users" with member action "postAddress". It is a post request which returns me a response. Now the problem I am facing is data is not normalized as returned data does not map to any store model. I know when we do a findAll and give a model, then ember automatically normalizes the data returned from API call. But in case of member actions, can anyone suggest how can data be normalized? (snake case to camel case).

ember-api-actions addon does not provide any serializer integration. There is an open issue about it. As discussed there you could manually push the response to store using pushPayload method of store service provided by Ember Data.
If you don't want to push the data into the store but just use a Serializer to normalize the response, you could do so by combining serializerFor() method of DS.Store and normalizeResponse() method of DS.Serializer.

Related

In the backend, can you access data from previous requests?

This is more of a theory question, so I'm not going to post any code.
On the frontend, the user types in a search command. On the backend (Django in my case), it hits an API, the results of the search are saved into a Django View in views.py. On the frontend, the user interacts with this returned data and sends another request. On the backend, is the data from the first Django View still available for use? How do you access it?
(The data is also in the frontend and I can send it with the second request. But if it's still stored on the backend then I wouldn't need to.)
HTTP by it's own nature is a stateless protocol. It does mean that protocol doesn't know what or when should happen any request. Request comes and your API just reacts to this request by your implemented logic.
If you want to persist/save any state/data on your API side, you can do it by persisting them to database or saving to any local/global variable. Then you can access this saved state/data while recieving other requests to your back-end and implement the logic to use of previous state with the new incoming data.

Ember Data - how to fetch data without using id

I'm building an app using Ember and trying to talk to a back end API using Ember Data and writing custom adapters.
The API I am using is not RESTful or conform to JSONAPI standard. It has many endpoints that does not take in id but returns data for current user.
For instance I have 'getAccountData' api that returns account data of the current user. The returned data has "id" associated with it (user_id of the current user) but you don't pass in "id" when calling the api to get the data.
I tried implementing findRecord method in my custom adapter but "id" parameter is required and it complains if I just call this.get('store').findRecord('account-data').
I can pass in dummy id like this.get('store').findRecord('account-data', 1) but this seems wrong since the 'id' of returned data won't be 1.
Should I be using findAll or query instead? is there a way to do this in clean way?
You should use queryRecord. From the Ember docs:
This method makes a request for one record, where the id is not known
beforehand (if the id is known, use findRecord instead).

Ember and custom endpoint

I have a problem with ember in a custom endpoint and a component
I need to get data from the custom endpoint when using a simple findAll the component works perfect but the findAll not going to custom endpoint, When I use adapter ajax goes to the custom endpoint and then pushPayload the data to the store but the component does not work.
What is the best way to call to an custom endpoint from Ember?

Ember store find() - use ID from server response instead of whatever you pass to find function

Whenever I call this.store.find('campaign', url), if the entry is not found in store it makes a request to API and creates a record in that store with ID being url. I need to be able to make sure that the record that is being pushed to the store uses id from the payload that is returned by API and not url that I pass in to find() function. What's the best way to go about it?
Override extractId() of your serializer, whether it's a RESTSerializer or JSONSerializer.

Server to ask Ember app to display one of its route while passing model data

My understanding is that if a given route in an Ember app needs to grab data from the server to feed its model, we will either use Ember data with a RESTful API or just an AJAX request, and the request for model data will come from the Ember app to the server.
But what if I want the server itself, at the end of a server-side process, to ask the Ember app to display one of its routes by passing to it some data to be used as the route model?
Basically, a process on the Node-based server (with Express) ends up like so:
function(req,res) {
res.redirect("/#/someEmberAppRoute");
}
The req parameter carries some object that I want to pass to the Ember route in order to be used as a model for that route.
The way I ended up solving this problem was to store on the server the output of the server-side process and have the Ember route someEmberAppRoute's model send a GET request to the server to grab that output and feed its model, instead of trying to push it directly from the server to the client.