I'm getting a list of courses from the server and I need to handle different kind of custom errors.
This is how I set the model and how I catch the error, the problem is that I can't send a custom message for the error:
App.CoursesRoute = Ember.Route.extend({
model: function(params) {
return this.store.find('course').then(function(response) {
return response;
}, function(error) {
// need to retrieve error message
});
}
});
This is the model for the Courses
App.Course = DS.Model.extend({
name: attr('string'),
teachers: attr('string')
});
This is de JSON that matches the courses model
{"courses":[{"name":"My Course","teachers":"2"},{"name":"Other Course","teachers":"3"}]}
I've tried many things like creating a Model for Errors and sending a JSON like this but nothing works...
App.Error = DS.Model.extend({
message: attr('string')
});
{"error":[{"message":"No courses found"}]}
Related
This is my Ember inspector info tab:
I am using ember-cli-mirage and I'm facing issue here.
I am not able to get belongsTo relationship of a model. When I do car.get('user'), I get a promise in result which gets fullfilled but the value is always null.
My user model
import DS from 'ember-data';
export default DS.Model.extend({
name: DS.attr('String'),
car: DS.belongsTo('car',{async: true})
});
My car model
import DS from 'ember-data';
export default DS.Model.extend({
color: DS.attr('String'),
user: DS.belongsTo('user',{async: true})
});
my mirage/config
this.get('/cars',function(db,request) {
var qp = request.queryParams.searchString.toLowerCase();
return {
cars: db.cars.where({'color':qp}),
users: db.users
};
});
I get the list of cars with search color but I don't get the user.
it returns a promise which when fullfilled gives null.
Since I am using ember-data 1.13.8
I tried using this Mirage working with json but then I get error
datum model is not defined error so i guess new json api is not my issue for some reason my ember data uses old json format and original config works.
this.get('/cars', function(db, request) {
return {
data: db.cars.map(attrs => (
{type: 'cars', id: attrs.id, attributes: attrs }
))
};
})
My cars route look like this
model: function(params) {
self = this;
if(!params){
return [];
}
return this.store.findQuery('car',params);
},
I tried
return this.store.findQuery('car',params).then(function(item){
console.log(item.get('user'));
});
but i still got console.log //undefined
I can see user_id in my json returned.
SOLUTION:
found the issue i was saving relationship as user_id = user.id. It should be user=user.id
SOLUTION: found the issue i was saving relationship as user_id = user.id. It should be user=user.id
I am very new at Ember/ED/EmberFire so apologies if this is a trivial question. I am able to save records to Firebase but I am unable to specify relationships to those records in my controller. I am using
DEBUG: Ember : 1.10.0
DEBUG: Ember Data : 1.0.0-beta.12
DEBUG: Firebase : 2.2.3
DEBUG: EmberFire : 1.4.3
DEBUG: jQuery : 1.11.2
I have a model as such:
var Patient = DS.Model.extend({
lastName: DS.attr('string'),
firstName: DS.attr('string'),
encounters: DS.hasMany('encounter', {async: true})
});
var Encounter = DS.Model.extend({
dateOfEncounter: DS.attr('string'),
patient: DS.belongsTo('patient', {async: true})
});
I am simply trying to specify the patient that my newly created encounter object is associated with. This is my controller:
actions: {
registerEncounter: function() {
var newEncounter = this.store.createRecord('encounter', {
dateOfEncounter: this.get('dateOfEncounter'),
patient: this.store.find('patient', '-Jl8u8Tph_w4PMAXb9H_')
});
newEncounter.save();
this.setProperties({
dateOfEncounter: ''
});
}
}
I can successfully create the encounter record in Firebase, but there is no associated patient property. All I get is
https://github.com/firebase/emberfire/issues/232
this.store.find('patient', '-Jl8u8Tph_w4PMAXb9H_') returns a promise.
Try this:
this.store.find('patient', '-Jl8u8Tph_w4PMAXb9H_').then(function(pat) {
var newEncounter = this.store.createRecord('encounter', {
dateOfEncounter: this.get('dateOfEncounter'),
patient: pat
});
newEncounter.save();
});
I'm a newbie to ember and I'm trying to create a basic sign-up form.
Relevant model:
App.NewUser = DS.Model.extend({
user_name: DS.attr('string'),
password: DS.attr('string'),
confirm_password: DS.attr('string'),
email: DS.attr('string'),
first_name: DS.attr('string'),
last_name: DS.attr('string'),
});
Relevant controller:
App.SignupController = Ember.ArrayController.extend({
actions: {
signup: function() {
var data = this.getProperties('first_name', 'last_name', 'email', 'user_name', 'password', 'confirm_password');
var newUser = this.store.createRecord('newUser', data);
newUser.save();
},
},
});
When the "signup" action executes, I get the following error:
Error: Attempted to handle event `didSetProperty` on <App.NewUser:ember332:null> while in state root.deleted.saved. Called with {name: last_name, oldValue: undefined, originalValue: undefined, value: undefined}.
What am I doing wrong?
This is a bug, Ember Data is setting the record state incorrectly if you're setting a value to what it's currently set to (undefined on createRecord)
You'll want to either coerce your values into empty strings or not set undefined values while creating the record.
for(var key in data){
if(!data[key]) delete data[key];
}
http://emberjs.jsbin.com/OxIDiVU/124/edit
https://github.com/emberjs/data/issues/1648
My server returns a JSON response like this:
{
artists: [{
id: "1",
first_name: "Foo",
last_name: "Bar"
}],
studios: [{
id: 1,
name: "Test",
// ...
artist_ids: ["1"]
}]
}
'artist' is in fact a User model but with a different name. How can I map artist to the User model? Maybe a bad explanation but if I rename the JSON response serverside to 'users' instead of 'artist' and use the models below everything works like I want. I simply want to use the name 'artist' instead of 'user', both server side and client side. Hope you guys understand what i mean.
App.Studio = DS.Model.extend
name: DS.attr 'string'
// ..
users: DS.hasMany 'App.User'
App.User = DS.Model.extend
firstName: DS.attr 'string'
lastName: DS.attr 'string'
studio: DS.belongsTo 'App.Studio'
I guess that the simplest thing to do would be something like artists: DS.hasMany 'App.User' but obviously this does not work.
First, I recommend using the latest Ember / EmberData, so relationships are defined like this:
App.Studio = DS.Model.extend({
name: DS.attr('string'),
// ..
users: DS.hasMany('user')
});
App.User = DS.Model.extend({
firstName: DS.attr('string'),
lastName: DS.attr('string'),
studio: DS.belongsTo('studio')
});
Next, I recommend using the ActiveModelAdapter if you are getting underscores in your response JSON:
App.ApplicationAdapter = DS.ActiveModelAdapter;
Finally, overriding typeForRoot and keyForRelationship in a custom serializer should fix your issue:
App.ApplicationSerializer = DS.ActiveModelSerializer.extend({
typeForRoot: function(root) {
if (root == 'artist' || root == 'artists') { root = 'user'; }
return this._super(root);
},
keyForRelationship: function(key, kind) {
if (key == 'users') { key = 'artists'; }
return this._super(key, kind);
}
});
Example JSBin
One last thing: you can even get rid of the custom keyForRelationship if you name the relationship artists in Studio:
App.Studio = DS.Model.extend({
name: DS.attr('string'),
// ..
artists: DS.hasMany('user')
});
Have you tried just creating an Artist model extended from User?
App.Artist = App.User.extend({})
I haven't tried it, but I suspect that might work.
App.User = DS.Model.extend({
posts: DS.hasMany('post', {async: true})
});
App.Post = DS.Model.extend({
body: DS.attr(),
user: DS.belongsTo('user')
});
App.ProfileRoute = Ember.Route.extend({
model: function(params) {
return this.get('store').find('user', params.user_id)
}
});
and in template
{{#each post in model.posts}}
{{post.body}}
{{/each}}
json for user. I don't want embed posts in user json
{user: { posts: [1, 2, 3] }}
This don't render anything. It receives posts json from server after this error occur
Assertion failed: You looked up the 'posts' relationship on '' but some of the associated records were not loaded. Either make sure they are all loaded together with the parent record, or specify that the relationship is async (DS.attr({ async: true }))
In chrome inspector I see all data loaded properly.
How can I solve this? Should I preload all models I want to use in templates?
The model function in your route is missing the return, so the error is being thrown when you try to access model.posts because there is no model.
App.ProfileRoute = Ember.Route.extend({
model: function(params) {
return this.get('store').find('user', params.user_id);
}
});
Have you tried to just write {{#each posts}}?
Worked for my project.
Then write {{body}} within the each block.
Let me know, thanks!