I am pretty new to EmberJS, I am trying to get back a value from a promise given by a custom adapter since the backend is not rails API compliant
Here is my custom adapter:
import DS from 'ember-data';
import Ember from 'ember';
export default DS.Adapter.extend({
findRecord( store, type, id ) {
return Ember.$.getJSON("http://localhost:8000/user/by-username/" + id );
}
});
I also modified my serializer:
import DS from 'ember-data';
export default DS.JSONSerializer.extend({
primaryKey:'alias',
});
my controller looks like this (where I am stuck):
import Ember from 'ember';
export default Ember.Controller.extend({
username:'',
actions: {
searchUser : function(){
var id = this.get('username');
var promise = this.get('store').findRecord('roles', id);
var self = this
promise.then(function(value) {
self.set('username', value.get('ID'));
}, function(reason) {
// on rejection
});
}
}
});
Specifically on this line from above controller, I can not get it to work:
self.set('username', value.get('ID'));
The ajax is exectued succesfully since I can see it happen on chrome dev tools returning json following data:
{"ID":"73ebf44f-71bf-4b60-a755-a4bbef208ef1","mjID":34049137,"alias":"coderman","registration_date":"2017-07-07T20:03:11Z"}
I am not able to get the ID value to be set to the username property
Your server should response with expected JSON data format. refer ember-data-model-maker for sample.
Sample format,
{
"data": {
"type": "roles",
"id": "1",
"attributes": {
"id": "foo",
"mj-id": "foo",
"alias": "foo",
"registration-date": "foo" },
}
}
Note : 1.Look at the mj-id
and registration-date
it's hyphenated. and you model property name should like mjId
and registrationDate
. 2. findRecord
we need to mention model name role
not the roles
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.