ember-data belongsTo not working

xorinzor

I have the following model:

and when I call this.store.find('history');

a call to http:://www.example.com/api/histories/ is made and this JSON response is returned:

{
   "tracks":[
      {
         "id":83,
         "title":"Untitled",
         "length":148,
         "artist_ids":[

         ],
         "album_ids":[

         ]
      },
      {
         "id":85,
         "title":"You want it",
         "length":262,
         "artist_ids":[

         ],
         "album_ids":[

         ]
      },
      {
         "id":81,
         "title":"Untitled",
         "length":133,
         "artist_ids":[

         ],
         "album_ids":[

         ]
      },
      {
         "id":78,
         "title":"Untitled",
         "length":345,
         "artist_ids":[

         ],
         "album_ids":[

         ]
      },
      {
         "id":80,
         "title":"Untitled",
         "length":225,
         "artist_ids":[

         ],
         "album_ids":[

         ]
      },
      {
         "id":73,
         "title":"Untitled",
         "length":366,
         "artist_ids":[

         ],
         "album_ids":[

         ]
      },
      {
         "id":77,
         "title":"Untitled",
         "length":161,
         "artist_ids":[

         ],
         "album_ids":[

         ]
      },
      {
         "id":82,
         "title":"Untitled",
         "length":384,
         "artist_ids":[

         ],
         "album_ids":[

         ]
      },
      {
         "id":76,
         "title":"Untitled",
         "length":245,
         "artist_ids":[

         ],
         "album_ids":[

         ]
      },
      {
         "id":79,
         "title":"Untitled",
         "length":479,
         "artist_ids":[

         ],
         "album_ids":[

         ]
      },
      {
         "id":75,
         "title":"Untitled",
         "length":328,
         "artist_ids":[

         ],
         "album_ids":[

         ]
      },
      {
         "id":84,
         "title":"Untitled",
         "length":259,
         "artist_ids":[

         ],
         "album_ids":[

         ]
      },
      {
         "id":74,
         "title":"Untitled",
         "length":329,
         "artist_ids":[

         ],
         "album_ids":[

         ]
      }
   ],
   "albums":[

   ],
   "artists":[

   ],
   "histories":[
      {
         "id":1382220844,
         "time_played":"2013-10-20 00:14:04",
         "user_id":null,
         "track_id":83
      },
      {
         "id":1382220581,
         "time_played":"2013-10-20 00:09:41",
         "user_id":null,
         "track_id":85
      },
      {
         "id":1382220449,
         "time_played":"2013-10-20 00:07:29",
         "user_id":null,
         "track_id":81
      },
      {
         "id":1382220103,
         "time_played":"2013-10-20 00:01:43",
         "user_id":null,
         "track_id":78
      },
      {
         "id":1382219877,
         "time_played":"2013-10-19 23:57:57",
         "user_id":null,
         "track_id":80
      },
      {
         "id":1382219511,
         "time_played":"2013-10-19 23:51:51",
         "user_id":null,
         "track_id":73
      },
      {
         "id":1382219351,
         "time_played":"2013-10-19 23:49:11",
         "user_id":null,
         "track_id":77
      },
      {
         "id":1382218968,
         "time_played":"2013-10-19 23:42:48",
         "user_id":null,
         "track_id":82
      },
      {
         "id":1382218723,
         "time_played":"2013-10-19 23:38:43",
         "user_id":null,
         "track_id":76
      },
      {
         "id":1382218243,
         "time_played":"2013-10-19 23:30:43",
         "user_id":null,
         "track_id":79
      },
      {
         "id":1382217915,
         "time_played":"2013-10-19 23:25:15",
         "user_id":null,
         "track_id":75
      },
      {
         "id":1382217657,
         "time_played":"2013-10-19 23:20:57",
         "user_id":null,
         "track_id":84
      },
      {
         "id":1382217327,
         "time_played":"2013-10-19 23:15:27",
         "user_id":null,
         "track_id":74
      },
      {
         "id":1382217195,
         "time_played":"2013-10-19 23:13:15",
         "user_id":null,
         "track_id":81
      },
      {
         "id":1382216849,
         "time_played":"2013-10-19 23:07:29",
         "user_id":null,
         "track_id":78
      }
   ]
}

Now Both the Store.History and Store.Track records are gettings stored (see screenshot below)

enter image description here

However, When I check a record from the Store.History the "track" attribute returns null

enter image description here

I have checked the Store.Track records and these contain the same ID's as presented in the JSON result

EDIT: per request, these are my models:

var attr = DS.attr,
    belongsTo = DS.belongsTo,
    hasMany = DS.hasMany;

Shoutzor.Album = DS.Model.extend({
    artist: belongsTo('artist'),
    title: attr('string'),
    cover: attr('string')
});

Shoutzor.Artist = DS.Model.extend({
    name: attr('string'),
    profileimage: attr('string')
});

Shoutzor.User = DS.Model.extend({
    name:           attr('string'),
    firstname:      attr('string'),
    email:          attr('string'),
    joined:         attr('date'),
    last_active:    attr('date')
});

Shoutzor.Track = DS.Model.extend({
    title: attr('string'),
    length: attr('number'),
    artist: hasMany('artist'),
    album: hasMany('album'),

    /* Convert the length in seconds to a string like '01:55' */
    convertedLength: function() {
        var sec_num = parseInt(this.get('length'), 10); // don't forget the second parm
        var hours   = Math.floor(sec_num / 3600);
        var minutes = Math.floor((sec_num - (hours * 3600)) / 60);
        var seconds = sec_num - (hours * 3600) - (minutes * 60);

        if (hours   < 10 && hours > 0) {hours   = "0"+hours;}
        if (minutes < 10 && minutes > 0) {minutes = "0"+minutes;}
        if (seconds < 10) {seconds = "0"+seconds;}
        var time    = ((hours != 0) ? hours + ':' : '') + ((minutes != 0) ? minutes +':' : '') + seconds;

        return time;
    }.property('length')
});

Shoutzor.History = DS.Model.extend({
    track: belongsTo('track'),
    user: belongsTo('user'),
    time_played: attr('date'),

    print_time: function() {
        var d = new Date(this.get('time_played'));

        var hours   = (d.getHours() < 10 ? "0" : '') + d.getHours(),
            minutes = (d.getMinutes() < 10 ? "0" : '') + d.getMinutes(),
            seconds = (d.getSeconds() < 10 ? "0" : '') + d.getSeconds();

        return  hours + ":" + minutes + ":" + seconds;
    }.property('time_played')
});

Any help is greatly appreciated.

Kingpin2k

Your history json should be like this in ember data 1.0, https://github.com/emberjs/data/blob/master/TRANSITION.md

{
     "id":1382217657,
     "time_played":"2013-10-19 23:20:57",
     "user":null,
     "track":84
  },

Remove the _id

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Ember not persisting belongsTo data

From Dev

ember data only with a belongsto relationship

From Dev

Ember data - set belongsTo to null

From Dev

Nullable belongsTo relationship in ember-data?

From Dev

Ember Data 1.0.0 belongsTo Relationship in JSON

From Dev

Ember Data belongsTo Association (JSON format?)

From Dev

Ember Data. Save model with "belongsTo" relationship

From Dev

Ember Data 1.0.0 belongsTo Relationship in JSON

From Dev

Ember Data embedded belongsTo relationship returning null

From Dev

ember / ember-data belongsTo and hasMany not updating as expected

From Dev

Ember data sending ids as strings when saving belongsTo

From Dev

ember-data making extra GET requests for belongsTo relations

From Dev

Properly create record with Ember Data that has belongsTo relationship

From Dev

Ember Data: Change belongsTo parent ID sent to the server

From Dev

Ember Data belongsTo async relationship omitted from createRecord() save() serialization

From Dev

Properly create record with Ember Data that has belongsTo relationship

From Dev

ember-data showing field from belongsTo relation after save

From Dev

Ember Data belongsTo async relationship omitted from createRecord() save() serialization

From Dev

How to use belongsTo in Ember

From Dev

Ember BelongsTo Assertion Failed

From Dev

ember data rest adapter not working

From Dev

findQuery() is not working in ember-data?

From Dev

Ember resolving async belongsTo to null

From Dev

Laravel belongsTo not working

From Dev

belongsTo cakephp relation not working

From Dev

Delete belongsTo association not working

From Dev

Laravel Eloquent belongsTo not working

From Dev

belongsTo() function not working

From Dev

Laravel belongsTo foreignkey not working

Related Related

  1. 1

    Ember not persisting belongsTo data

  2. 2

    ember data only with a belongsto relationship

  3. 3

    Ember data - set belongsTo to null

  4. 4

    Nullable belongsTo relationship in ember-data?

  5. 5

    Ember Data 1.0.0 belongsTo Relationship in JSON

  6. 6

    Ember Data belongsTo Association (JSON format?)

  7. 7

    Ember Data. Save model with "belongsTo" relationship

  8. 8

    Ember Data 1.0.0 belongsTo Relationship in JSON

  9. 9

    Ember Data embedded belongsTo relationship returning null

  10. 10

    ember / ember-data belongsTo and hasMany not updating as expected

  11. 11

    Ember data sending ids as strings when saving belongsTo

  12. 12

    ember-data making extra GET requests for belongsTo relations

  13. 13

    Properly create record with Ember Data that has belongsTo relationship

  14. 14

    Ember Data: Change belongsTo parent ID sent to the server

  15. 15

    Ember Data belongsTo async relationship omitted from createRecord() save() serialization

  16. 16

    Properly create record with Ember Data that has belongsTo relationship

  17. 17

    ember-data showing field from belongsTo relation after save

  18. 18

    Ember Data belongsTo async relationship omitted from createRecord() save() serialization

  19. 19

    How to use belongsTo in Ember

  20. 20

    Ember BelongsTo Assertion Failed

  21. 21

    ember data rest adapter not working

  22. 22

    findQuery() is not working in ember-data?

  23. 23

    Ember resolving async belongsTo to null

  24. 24

    Laravel belongsTo not working

  25. 25

    belongsTo cakephp relation not working

  26. 26

    Delete belongsTo association not working

  27. 27

    Laravel Eloquent belongsTo not working

  28. 28

    belongsTo() function not working

  29. 29

    Laravel belongsTo foreignkey not working

HotTag

Archive