If I have a field that is in snake case coming back from the api, how should I define that field in the model? Im using the JSONAPIAdapter. It seems like fields that is one word work fine, but snake case fields come back as undefined.
This is how I have it defined in my model:
import DS from 'ember-data';
export default DS.Model.extend({
typecode_desc: DS.attr('string'),
contactnum: DS.attr('string'),
email: DS.attr('number'),
individual: DS.belongsTo('individual', {async: false})
});
And this is how the json comes back from the API:
1: {
id: "96"
type: "contact_infos"
attributes: {
typecode_desc: "E-mail address"
contactnum: "billybear#yahoo.com"
email: 1
}
}
However, in the ember inspector, typecode_desc comes back as being undefined. Is there something I need to do to tell ember that fields will come back as being snake case?
You need to define keyForRelationship in your JSON API serializer. It will look something like this:
import DS from 'ember-data';
import Ember from 'ember';
export default DS.JSONAPISerializer.extend({
keyForAttribute: function(attr) {
return Ember.String.underscore(attr);
},
keyForRelationship: function(attr) {
return Ember.String.underscore(attr);
}
});
Related
I have an ember route that gets the following data:
return this.store.findAll('servicerequest');
One of the fields is a date field called created_at that I am trying to sort by.
Here is my servicerequest controller:
import Controller from '#ember/controller';
import { sort } from '#ember/object/computed';
export default Controller.extend({
appName: 'Service Requests',
sortAscending: false,
sortDefinition: ['created_at:desc'],
sortedRequests: sort('servicerequest', 'sortDefinition')
});
Here is the hbs section where I am outputting the data:
{{#each sortedRequests as |servicerequest|}}
{{servicerequest-list status=servicerequest.status srdescription=servicerequest.srdescription priority=servicerequest.priority created_at=servicerequest.created_at
current_user_id=servicerequest.current_user_id servicerequestId=servicerequest.id}}
{{/each}}
I have not been able to get anything to sort by the created_at field desc. What am I doing wrong?
UPDATE:
Here is my model:
import DS from 'ember-data';
export default DS.Model.extend({
requester_name: DS.attr('string'),
srdescription: DS.attr('string'),
priority: DS.attr('string'),
status: DS.attr('string'),
created_at: DS.attr('date'),
current_user_id: DS.attr()
});
The data is coming from a rails api. The created_at field is what the systems defaults to on records that is why there is an underscore.
I am using ember-cli: 3.0.2
So I ended up getting it working by doing this:
return this.store.findAll('servicerequest').then(results =>
results.sortBy('created_at').reverse());
Not sure if it is the best way but for now it's doing what I need.
Ideally any data operation should be performed at the backend.
Incase if there are any backend limitations, you can move the sorting logic snippet into the serializer. This way, any fetch call for 'servicerequest', will come sorted and redundant sorting logic can be avoided.
Something like =>
serialize(snapshot, options) {
snapshot.sortBy('created_at').reverse();
return this._super(...arguments);
}
My ember app is not sending my foreign key to the back-end.
I have a table called issues which is has a related table called categories
My model is:
import DS from 'ember-data';
export default DS.Model.extend({
name: DS.attr('string'),
category_id: DS.belongsTo('category'),
description: DS.attr('string')
});
My route is:
import Ember from 'ember';
export default Ember.Route.extend({
model: function(){
return this.store.findAll('issue');
},
actions: {
create: function(){
var issue = this.store.createRecord('issue');
issue.name = this.get('controller').get('newName');
issue.description = this.get('controller').get('newDescription');
issue.category_id = parseInt(this.get('controller').get('newCategory'));
//debugger;
console.log(issue);
issue.save();
},
...
other actions
...
}
}
});
the console.log from above looks like the category_id is getting set correctly:
category_id: 3
description: "foobar"
name: "test"
However my JSON payload that gets sent to the backend looks like:
{"issue":{"name":"test","description":"foobar","category_id":null}}
I tried stepping through by adding a custom serialiser in app/serializers/application.js
export default DS.RESTSerializer.extend({
...
serialize: function(snapshot,options){
console.debug('options='+options);
debugger;
var json = this._super(snapshot, options);;
return json;
}
...
});
But I got lost in all the super calling super indirection.
The snapshot.record has category_id: 3, but the json coming back from the this._super() call has category_id: null
options has includeID:true
Any clues will be much appreciated ...
Ember : 2.0.2
Ember Data : 2.0.0
Your model definition is wrong, when dealing with relationships you define them just as you would define any other attribute, there is no need to use _id.
export default DS.Model.extend({
name: DS.attr('string'),
category: DS.belongsTo('category'),
description: DS.attr('string')
});
As for the creation you should always use setters/getters when dealing with ember objects:
create: function() {
var issue = this.store.createRecord('issue', {
name: this.get('controller').get('newName'),
description: this.get('controller').get('newDescription'),
category: this.get('controller').get('newCategory') // assuming new category is a DS.Model instance of category
});
issue.save();
}
If you wish to stick to the syntax you have you would use issue.set('name', this.get('controller').get('newName')), from the looks of your code it seems you are going about this in the wrong way.
You should have a this.route('new') nested under your issues route, that way you wouldn't have to use the controller to store information.
You would simply set the model of the new route to:
model: function() {
return this.store.createRecord('issue');
}
Your template would make use of the input helpers like so:
{{input value=model.name}} and your action would just get the currentModel and call .save().
I have some models with similar names:
issue-statuse.js
issue-type.js
issue-type is working perfectly, but issue-statuse is causing trouble:
WARNING: Encountered "issue_statuses" in payload, but no model was found for model name "issue-status" (resolved model name using soporte#serializer:issue-statuse:.modelNameFromPayloadKey("issue_statuses"))
//<!--app/adapters/application.js-->
import Ember from 'ember';
import DS from 'ember-data';
export default DS.RESTAdapter.extend({
namespace: 'api/v1',
host: 'http://127.0.0.1:3000',
coalesceFindRequests: true,
headers: {
withCredentials: true,
Authorization: 'Basic eG9qbzpzZWNyZXQ=',
crossDomain: true
},
pathForType: function(type) {
return Ember.String.underscore(type)+'s';
}
});
// added a 's' for pluralize names, as when we need in underscore are in singular once again ...
//<!--app/models/issue-statuse.js-->
import DS from 'ember-data';
export default DS.Model.extend({
name: DS.attr('string'),
short: DS.attr('string'),
plural: DS.attr('string'),
created_at: DS.attr('date'),
active: DS.attr('boolean')
});
I have a workaround in a Serializer, I don't like it but with it it's working fine:
//<<!--app/serializers/issue-statuse.js
import DS from 'ember-data';
export default DS.RESTSerializer.extend({
modelNameFromPayloadKey: function(payloadKey) {
if (payloadKey === 'issue_statuses') {
return this._super("issue-statuse"); //this._super(payloadKey.replace('blog/', ''));
} else {
return this._super('issue-statuse'); //this._super(payloadKey);
}
}
});
But if I use this Serializer, I've got a Deprecation Warning:
Your custom serializer uses the old version of the Serializer API, with `extract` hooks. Please upgrade your serializers to the new Serializer API using `normalizeResponse` hooks instead.
So, I have two options, first one is try to solve why Ember is not finding my model, and the second, use the serializer and try to understand the deprecation and how to eliminate it.
I would prefer option one :-)
Thanks,
edit
It was a typo here in the name of the model file, it's singular:
//<!--app/models/issue-statuse.js-->
edit 2
I've removed completely the app/serializers/issue-statuse.js and created an initializer with the inflector:
//<!--/app/initializers/inflector.js-->
import Ember from 'ember';
export function initialize(/* container, application */) {
var inflector = Ember.Inflector.inflector;
inflector.uncountable('aamc-pcrs');
inflector.irregular('issue-statuse', 'issue-statuses');
}
export default {
name: 'inflector',
initialize: initialize
};
edit 3
I'm using the
pathForType: function(type) {
return Ember.String.underscore(type)+'s';
},
in the Adaptor for changing - for _
My Backend API wants issue_statuses instead of issue-statuses for example. Maybe I can just rename the table in the inflector and remove also this line ...
I recently started learning Ember and using Ember-CLI so I'm not quite well educated about Ember Data and what array names it expects for relationships that are in sub directories in my app.
// models/server.js
import DS from 'ember-data';
export default DS.Model.extend({
serverHistory: DS.hasMany("history/server", { async: true })
});
// models/history/server.js
import DS from 'ember-data';
export default DS.Model.extend({
server: DS.belongsTo("server", { async: true })
});
I've tried returning these names from my API
server_historys_ids
server_histories_ids
history_server_ids
history_servers_ids
But I don't see an XHR request for Server history in my application. The servers itself are fetched fine.
Update
I changed my relationship name and the API is returning history ids but I'm still not getting an history json request even though I'm trying to each in the template. The game relationship data is accessible in the template and a request is successfully made.
// models/server.js
import DS from 'ember-data';
export default DS.Model.extend({
// attr's here.. not relevant
// Relationships
game: DS.belongsTo("game", { async: true }), // works
serverHistories: DS.hasMany("history/server", { async: true }) // doesn't make a request like game does.
});
I also have an adapter/history/server.js but it's only telling what namespace to use - "api".
Update 2
I think the problem may be in the way I'm calling the data to the model.
// routes/server/view/index.js
import Ember from 'ember';
export default Ember.Route.extend({
model: function() {
var parentModel = this.modelFor("server.view");
return this.store.query("server", { server_address: parentModel.server_address });
// return this.store.find("server", 1);
}
});
How come when I use find with an id it updates the template data and when I use query with parameters it doesn't?
Update 3
So I got my find and query problem sorted out, here's the way I got it to work: https://stackoverflow.com/a/31831667/1814027
The relationship problem still persists. I see no serverHistory data in my Ember toolbar nor a request being made to the API for it.
I beleive serverHistory is anti-conventional name for hasMany and serverHistories should be instead.
export default DS.Model.extend({
serverHistories: DS.hasMany("history/server", { async: true })
});
Then in case of ActiveModelAdapter expected server payload is:
{"server": {"id": 1, "server_history_ids": [1,2,3]}}
It doesn't depend on the fact that serverHistory is namespaced model, it depends on relation name only.
For example for model:
// models/server.js
import DS from 'ember-data';
export default DS.Model.extend({
bars: DS.hasMany("history/server", { async: true })
});
expected payload is:
{"server": {"id": 1, "bar_ids": [1,2,3]}}
Update
Working ember-cli example: https://github.com/artych/so_ember_data_subdir
Artych's answer helped me on the right path but Ember didn't want to recognise server_history_ids so I just renamed the hasMany relation to histories and returned histories: [] from my API. Now it works.. don't know why but it works.
I have an endpoint that returns id, name, properties, and properties is a damn string, with comma separated attribute key/value pairs. Yes you read that correctly.
properties = "id:1,name:foobar,phone:123456789,taco:true"
Current Model
import DS from 'ember-data';
export default DS.Model.extend({
contact: DS.belongsTo("contact"),
name: DS.attr(),
phone: DS.attr(),
properties: DS.attr(),
eachProperties: function () {
return this.get('properties').split(",").map(function(propertyKeyValuePair) {
return propertyKeyValuePair.split(":");
});
}.property('properties')
});
properties requires some computation to be helpful on the front-end.
Can you re-use the property named properties instead of creating a new one called eachProperties?
Something like:
export default DS.Model.extend({
// ... other stuff
properties: DS.attr(function(model, obj){
return obj.split(",").map(function(propertyKeyValuePair) {
return propertyKeyValuePair.split(":");
});
});
});
OR how does one change the name of an api property name to something else like rawProperties and then have the computed property called properties?
I know you're thinking "Just fix your damn API" but you know that isn't always possible :)
After some research, I found that i basically wanted to utilize Transforms (docs) to handle the deserializing. Its super easy and rad.
As per Ember-CLI module-directory-naming-structure, you add transforms inside the app/transforms/ folder.
I created a file called properties.js. (Whatever you name the file will be the name of attribute/transform.)
//*inside app/transforms/properties.js*
import DS from 'ember-data';
export default DS.Transform.extend({
deserialize: function (serialized) {
return serialized.split(",").map(function (keyValuePair) {
return keyValuePair.split(":");
});
},
serialize: function (deserialized) {
return deserialized.map(function (keyValuePair) {
return keyValuePair.join(":");
}).join(",");
}
});
This transform will be used when you receive json, and also when you send the JSON back out! super cool.
Updated Model:
import DS from 'ember-data';
export default DS.Model.extend({
contact: DS.belongsTo("contact"),
name: DS.attr('string'),
phone: DS.attr(),
properties: DS.attr('properties')
});
the DS.attr('properties') is where the magic happens!