mardi 24 mars 2015

Ember Data one to many relationship not working

I am pretty new to ember but I am wondering if this is an issue with my logic or if there is something not functioning correctly in ember.


When I try model.get('profile') I get null.


I have been able to retrieve a profile model like so: this.store.find('profile', 17); Also I can add a profile_id attribute to the location model and then use it



var location = this.modelFor('location');
var profile = this.store.find('profile', 17);


but from what I can find in the documentation I should be able to use .get('profile') on a location object to get its profile.


The strange thing is that my attachments relationship (location has many attachments) works perfectly.


Am I missing something? What am I doing wrong that is causing this relationship to fail?


I have three models:


locatons->



export default DS.Model.extend({
image: DS.attr('string'),
latitude:DS.attr('string'),
longitude:DS.attr('string'),
outlets:DS.attr('string'),
parking:DS.attr('string'),
internet:DS.attr('string'),
credit_cards:DS.attr('string'),
share_url:DS.attr('string'),
roaster:DS.attr('string'),
about:DS.attr('string'),
name: DS.attr('string'),
address: DS.attr('string'),
images: DS.attr('string'),
attachments: DS.hasMany('attachment'),
profile: DS.belongsTo('profile'),
curator_image: DS.attr('string'),
curator_about: DS.attr('string'),
curator_name: DS.attr('string'),
style_image_url: function(){
return "background-image:url('" + this.get("image") + "')";
}.property("image"),
style_profile_image_url: function(){
return "background-image:url('" + this.get("curator_image") + "')";
}.property("curator_image"),
});


attachments ->



export default DS.Model.extend({
location: DS.belongsTo('location'),
image:DS.attr('string')
});


profiles ->



export default DS.Model.extend({
location: DS.hasMany('location'),
name:DS.attr('string'),
about:DS.attr('string'),
image:DS.attr('string'),
});


here is a sample response from the server: locations.json



{
attachments: [
{
id: 254,
image: "http://ift.tt/1CWzYEd"
},
{
id: 250,
image: "http://ift.tt/1IsxThQ"
}],
locations: [
{
id: 12,
latitude: "45.550346",
longitude: "-122.666584",
address: "3808 North Williams Avenue, Portland, OR 97212, USA",
outlets_text: "Yes",
internet_text: "No",
roaster: "Ristretto Roasters",
parking_text: "Yes",
credit_cards: true,
image: "http://ift.tt/1CWA0vN",
name: "Ristretto",
twitter_username: null,
curator_image: "http://ift.tt/1CWzYEf",
curator_name: "Jack White",
curator_about: "this is a test this is a test this is a test this is a test this is a test this is a test this is a test this is a test this is a test this is a test this is a test this is a test this is a test ",
cprofile_id: 17,
attachment_ids: [
30
]
},
...


Routes: /routes/location.js



export default Ember.Route.extend({
model: function(params) {
return this.store.find('location', params.location_id);
}
});


/routes/location/index



export default Ember.Route.extend({
model: function() {
return this.modelFor('location').get('profile');
}
});




Aucun commentaire:

Enregistrer un commentaire