2

我有以下模型:

当我调用 this.store.find('history');

调用 http:://www.example.com/api/histories/ 并返回此 JSON 响应:

{
   "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
      }
   ]
}

现在 Store.History 和 Store.Track 记录都被存储(见下面的截图)

在此处输入图像描述

但是,当我从 Store.History 检查记录时,“track”属性返回 null

在此处输入图像描述

我检查了 Store.Track 记录,这些记录包含与 JSON 结果中相同的 ID

编辑:根据要求,这些是我的模型:

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')
});

任何帮助是极大的赞赏。

4

1 回答 1

5

你的历史 json 在 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
  },

删除_id

于 2013-10-19T23:06:27.413 回答