2

私は次のモデルを持っています:

this.store.find('history'); を呼び出すと、

http:://www.example.com/api/history/ への呼び出しが行われ、次の 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 からレコードを確認すると、「トラック」属性は 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