Where/when should I remove the relationship type to avoid an unknown keys warning? - ember.js

I'm using Ember Data with a server application that follows the json:api standard. When I normalize the response from the server, I'm adding a relationshipType attribute from the links so that Ember Data knows what type of model to build when the relationship is polymorphic.
For example, here's the response from the server:
{
"members": {
"id": "1",
"created_at": "2014-10-15T18:35:00.000Z",
"updated_at": "2014-10-15T18:35:00.000Z",
"links": {
"user": {
"id": "1",
"type": "users",
"href": "http://test.host/api/v1/users/1"
},
"organization": {
"id": "2",
"type": "customers",
"href": "http://test.host/api/v1/customers/2"
}
}
}
}
The organization relationship is polymorphic, and the type in this instance is customers.
In the Ember application, I'm normalizing the response into following (which follows the RESTSerializer convention):
{
"members": {
"id": "1",
"created_at": "2014-10-15T18:35:00.000Z",
"updated_at": "2014-10-15T18:35:00.000Z",
"user": "1",
"userType": "users",
"organization": "2",
"organizationType": "customers"
}
}
This works, and Ember Data builds the correct user relationship and organization relationship (using the Customer model).
But, I'm receiving the following warning:
WARNING: The payload for '(subclass of DS.Model)' contains these unknown keys:
[userType,organizationType]. Make sure they've been defined in your model.
I'd like to remove these relationshipType keys and their values after they've been used.
Where should I do this?

Have you tried using https://github.com/kurko/ember-json-api? I may have some other overrides in my app to handle polymorphism but hopefully the package will get you closer.
Also see my pending PR.

Related

List users as non admin with custom fields

As per the documentation, I should be able to get a list of users with a custom schema as long as the field in the schema has a value of ALL_DOMAIN_USERS in the readAccessType property. That is the exact set up I have in the admin console; Moreover, when I perform a get request to the schema get endpoint for the schema in question, I get confirmation that the schema fields are set to ALL_DOMAIN_USERS in the readAccessType property.
The problem is when I perform a users list request, I don't get the custom schema in the response. The request is the following:
GET /admin/directory/v1/users?customer=my_customer&projection=full&query=franc&viewType=domain_public
HTTP/1.1
Host: www.googleapis.com
Content-length: 0
Authorization: Bearer fakeTokena0AfH6SMD6jF2DwJbgiDZ
The response I get back is the following:
{
"nextPageToken": "tokenData",
"kind": "admin#directory#users",
"etag": "etagData",
"users": [
{
"externalIds": [
{
"type": "organization",
"value": "value"
}
],
"organizations": [
{
"department": "department",
"customType": "",
"name": "Name",
"title": "Title"
}
],
"kind": "admin#directory#user",
"name": {
"fullName": "Full Name",
"givenName": "Full",
"familyName": "Name"
},
"phones": [
{
"type": "work",
"value": "(999)999-9999"
}
],
"thumbnailPhotoUrl": "https://photolinkurl",
"primaryEmail": "user#domain.com",
"relations": [
{
"type": "manager",
"value": "user#domain.com"
}
],
"emails": [
{
"primary": true,
"address": "user#domain.com"
}
],
"etag": "etagData",
"thumbnailPhotoEtag": "photoEtagData",
"id": "xxxxxxxxxxxxxxxxxx",
"addresses": [
{
"locality": "Locality",
"region": "XX",
"formatted": "999 Some St Some State 99999",
"primary": true,
"streetAddress": "999 Some St",
"postalCode": "99999",
"type": "work"
}
]
}
]
}
However, if I perform the same request with a super admin user, I get an extra property in the response:
"customSchemas": {
"Dir": {
"fieldOne": false,
"fieldTwo": "value",
"fieldThree": value
}
}
My understanding is that I should get the custom schema with a non admin user as long as the custom schema fields are set to be visible by all domain users. This is not happening. I opened a support ticket with G Suite but the guy that provided "support", send me in this direction. I believe this is a bug or maybe I overlooked something.
I contacted G Suite support and in fact, this issue is a domain specific problem.
It took several weeks for the issue to be addressed by the support engineers at Google but it was finally resolved. The behaviour is the intended one now.

How to get included values of jsonapi on a router with ember-data?

I'm using jsonapi directives to make a connection between ember.js app and an API. I have a track that could have like 50 or more comments and I need to load them on a route to perform some logic.
This is and example response of the API:
{
"data": {
"id": 1,
"type": "track",
"attributes": {
"name": "XPTO"
},
"relationships": {
"comment": {
"data": [
{"id": 1, "type": "comment"}
]
}
}
},
"include": [
{
"id": 1,
"type": "comment",
"attributes": {
"text": "Lorem ipsum..."
}
}
]
}
Now imagine it with 50 comments, this would be very consuming to make a request for each call. If I do it in a view with an each loop, it doesn't make all the requests, but it I try to access it in a Route, it will make all the requests. How do I achieve that with the following code?
this.store.findRecord('track', 1).then((t) => {
// logic here
// I tried this but it would make all the requests too
t.get('comments');
})
U can use property "coalesceFindRequests" in your adapter
coalesceFindRequests: true
If you set coalesceFindRequests to true it will instead trigger the following request:
GET /comments?ids[]=1&ids[]=2
so this makes only one call for all the comments.
Note: Requests coalescing rely on URL building strategy. So if you
override buildURL in your app groupRecordsForFindMany more likely
should be overridden as well in order for coalescing to work.

jsonapi + ember 2.0 + pagination

How to do pagination in latest version of ember-data (v2.2) and a jsonapi backend?
I am in control of the backend implementation so I can implement any strategy, but I would prefer to follow the standard as described here:
http://jsonapi.org/format/#fetching-pagination
However, that description is a bit cryptic to me without an example.
And how to handle that smoothly on the client (ember) side?
Is there some built in stuff in ember-data to process the paging-links?
EDIT:
I guess to get started I must process the meta information. By overriding the serializer. At the time deserialize is called I can read the meta from the payload but at that time I don't have an object to store it on. Later when my object is done deserialized I don't have access to the payload with the meta information.
Does that make sence? Here is my example payload:
{
"data": {
"id": "1",
"type": "user",
"attributes": {
"firstname": "John",
"lastname": "Doe",
"email": "john.doe#example.com",
}
},
"included": [{
"id": "68",
"type": "activity",
"attributes": {
"title": "Yoga",
}
}, {
"id": "65",
"type": "activity",
"attributes": {
"title": "Slalom",
}
}],
"meta": {
"activity-total": "23",
"activity-pagesize": "2",
"activity-offset": "0"
}
}
Where to put my code that reads the meta information and stores it on the user object?
You could use the Ember Infinity Addon. It does all the magic for you.

Ember - Sending particular values in PUT request

I have a form which I am using to edit details via form. Ember does a PUT request like required but I need it to only send particular values and not everything within the JSON object. The object is structured as:
{
consoles: {
"id": "1",
"name": "Street Fighter",
"type":"Beat-em-up"
"versions": "10",
"consoles": [
{
"id": "1",
"name": "Microsoft",
"console": "Xbox 360"
},{
"id": "2",
"name": "Microsoft",
"console": "Xbox One"
},{
"id": "3",
"name": "Sony",
"console": "Playstation 4"
}
],
"Characters":[],
"Reviews":[]
}
}
The only editable values within my form are:
name
type
versions
When I press the update button, everything is sent in a PUT request. Is it possible for ONLY name, type and versions to be sent in the request?
I'm aware of overriding updateRecord but I wouldn't know where to start if this was what I needed to do.
You can write your own Serializer to only send specific attributes up to the server.
See the docs here

Ember data and mongodb state?

According to http://emberjs.com/api/data/classes/DS.EmbeddedRecordsMixin.html
Records without an id property are not considered embedded records, model instances must have an id property to be used with Ember Data.
If thats so how would i use a model like:
{
"_id": "545b54f20dbd7015f015359a",
"comments": [
{
"msg": "what is this jelly",
"time": "1415271666",
"user": {
"id": "53fefc9ee4b07d3fe92fc077",
"username": "kfir124"
}
}
],
"files": [
{
"key": "1415271653.39af9c9fb8cc8b49dcaac5eec72614ce85.jpg",
"order": 0
}
],
"time": "1415271666",
"uploader": {
"id": "53fefc9ee4b07d3fe92fc077",
"username": "kfir124"
}
}
So uploader may be an embedded record but what about files and comments? they are both arrays of objects which do not live outside of the main model's namespace therefore they have no IDs