Laravel 5 Customize Serialize Data


#1

Hello Everyone,

First question on this forum…

I’m pretty new in ember,

I want to load users, but in my JSON response is:

{ "total":3, "per_page":20, "current_page":1, "last_page":1, "next_page_url":null, "prev_page_url":null, "from":1, "to":3, "data":[ { "id":7, "email":"rizki@talenta.co", "first_name":"Rizki", "last_name":"Andrianto", "created_at":"2016-06-28 08:54:28", "updated_at":"2016-06-28 08:54:28" }, { "id":21, "email":"yansen@talenta.co", "first_name":"Yansen", "last_name":"Tan", "created_at":"2016-07-21 10:12:21", "updated_at":"2016-07-21 10:12:21" }, { "id":115, "email":"uray@talenta.co", "first_name":"Uray", "last_name":"Chrisna", "created_at":"2016-07-29 14 :13:35", "updated_at":"2016-07-29 14:13:35" } ] }

and my app/model/users:

//import Model from 'ember-data/model';
import DS from 'ember-data';

export default DS.Model.extend({
	total: DS.attr('number'),
	per_page: DS.attr('number'),
	current_page: DS.attr('number'),
	last_page: DS.attr('number'),
	next_page_url: DS.attr('string'),
	users: DS.attr()
});

but I get : WARNING: Encountered "data" in payload, but no model was found for model name "datum" (resolved model name using ember-jwt@serializer:-rest:.modelNameFromPayloadKey("data"))

How to display only the user or all of them without changin the backend? I want to google this, but I don’t know the keyword…

Thank you…


#2

What you’ll need is a custom serializer to change your backend data into a format that Ember Data expects. I’d start here (https://guides.emberjs.com/v2.7.0/models/customizing-serializers/) but also search for tutorials on the subject, there have been some good ones in the last 6 months.


#3

Thanks for the reply,

Oh I see, custom serializer. But that’s article is for POST not for GET, right? is it the same way?


#4

You can also send JSON-API formatted data from your server, might be a better idea if you have control over server responses. Check out https://github.com/cloudcreativity/json-api , which is based on https://github.com/neomerx?tab=repositories , you can also check limoncello-* packages and sample ember apps.


#5

Confirmed, it works the same way


#6

This is also a good point. @rizkiandrianto, depending on whether you are able to change your API somewhat drastically, using JSON-API will make your life simpler in the long-run with Ember Data. There are some growing pains to the JSON-API spec (things that need to get worked through still), but it’s growing closer and closer to “drop-in ready” for the types of advanced use cases needed on the Ember side of things …


#7

I’ve followed the instruction from http://stackoverflow.com/questions/31731934/incorrect-json-format-for-ember-data

but it still gets those warning, and doesn’t show anything on template.