ember-data belongsTo不工作

时间:2013-10-19 22:22:39

标签: javascript json ember.js ember-data

我有以下型号:

当我调用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记录(见下面的截图)

enter image description here

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

enter image description here

我检查了Store.Track记录,其中包含与JSON结果中显示的ID相同的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')
});

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:5)

您的历史记录json在ember数据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