2

フィクスチャ データを emberjs モデルに動的にロードするのに問題があります。これを行うにはどうすればよいですか?下部にある addArtist 関数は 1 つの曲を正しく追加しますが、すべての曲をデータ ストアに正しくロードするためにこれを適応させる方法がわかりません。それぞれを個別に追加するよりも簡単な方法はありますか?

App.Store = DS.Store.extend({
    revision: 13,
    adapter: 'DS.FixtureAdapter'
});

App.Artist = DS.Model.extend({
    name: DS.attr('string'),
    tracks: DS.hasMany('App.Tracks')
});

App.Tracks = DS.Model.extend({
    videoid: DS.attr('string'),
    title: DS.attr('string'),
    duration: DS.attr('number')
});

App.Artist.FIXTURES = [];
App.Tracks.FIXTURES = [];

App.loadFixtures = function(){
    $.getJSON('/artists', function(data){
        $.each(data.artists, function(i,v){
            App.Artist.createRecord(v);
        });
    });
    $.getJSON('/tracks', function(data){
        $.each(data.tracks, function(i,v){
            App.Tracks.createRecord(v);
        });
    });
};

App.addArtist = function(){
    var artist = App.Artist.createRecord({
        id: 3,
        name: 'Justin Martin'
    });
    var track = App.Tracks.createRecord({
        id: 300,
        title: 'Jungle Mix',
        duration: 200
    });
    artist.get('tracks').pushObject(track);
};

サンプルの JSON 応答:

{"artists":[
{"id":1,"name":"The (International) Noise Conspiracy","track_ids":[1,2,3,4]},
{"id":2,"name":"0SM","track_ids":[5]},
{"id":3,"name":"2am","track_ids":[6,7,8]}
]}

{"tracks":[
{"id":1,"videoid":"FyjmCg_VMU0","title":"Smash It Up","duration":197},
{"id":2,"videoid":"jKXWm9yi4DY","title":"Only Lovers Left Alive","duration":162},
{"id":3,"videoid":"fwbleH55CCk","title":"Up For Sale","duration":211},
{"id":4,"videoid":"50JdKhIB1EQ","title":"A New Morning, Changing Weather","duration":270},
{"id":5,"videoid":"bd6ve0ydHVo","title":"The Landing feat. Alex G - Original Mix","duration":322},
{"id":6,"videoid":"RnwVtbMW4x4","title":"Anxious","duration":238},
{"id":7,"videoid":"Yo0zsqa06ZE","title":"I Did Wrong","duration":305},
{"id":8,"videoid":"_9ydBAgg130","title":"I Love You (feat. Baekchan & Joohee)","duration":219}
]}

アーティストの応答に track_ids を含める必要がありますか?それとも、このメソッドを呼び出したときに追加されますか?

artist.get('tracks').pushObject(track);
4

0 に答える 0