2013-04-08 35 views
0

我有一個專輯和歌曲的關係。在新專輯視圖中,我要求同時播放歌曲,我試圖先保存專輯模型,然後保存歌曲集合並將其附加到專輯中。骨幹 - 同步沒有檢測到我的收藏URL

我的歌曲集的定義:

define(function(require){ 
    var Song = require('models/songModel'); 

    Songs = Backbone.Collection.extend({ 
    url: 'songs/',   
    model: Song,   
    }); 

    return Songs; 
}); 

創建我的歌曲集是這樣的:

this.songCollection = new Songs(); 
//In some other view that saves the songs files and returns a hash 
that.songCollection.add({title:file.name,song_file:response['file_hash']}); 

然後保存我的相冊樣板和成功嘗試保存歌曲集添加新專輯pk到歌曲集合中的所有模型:

that = this; 
this.model.save(null,{         
    success: function(model,response){ 
     that.songCollection.each(function(song){                          
      song.set('album',model.get('id')); 
     }); 
     that.songCollection.sync('create');          
    },    
    error: function(response){       
     console.log(response);       
    } 
}); 

但是,它返回一個A 'url' property or function must be specified,但我確實已經指定了,正如你以前看到的。我還嘗試在同步調用之前將其記錄下來,並正確返回該URL。我在這個過程中錯過了什麼?或者我無法像這樣立即在服務器中創建所有這些新歌曲?

+0

嘗試設置模型(不是集合)的'urlRoot'屬性。 – craftsman 2013-04-08 21:49:14

+0

我也試過,但它一直返回相同的錯誤。 – Nocturn 2013-04-08 21:51:53

回答

2

您需要單獨保存每首歌曲。 sync並不意味着直接調用,只有'read'方法才能用於集合。在collection.fetch()期間調用sync('read')

that = this; 
this.model.save(null,{         
    success: function(model,response){ 
     that.songCollection.each(function(song){                          
      song.save({album: model.get('id')}); 
     });         
    },    
    error: function(response){       
     console.log(response);       
    } 
}); 
+1

謝謝!這是問題。我以爲你可以一次保存整個藏品。 – Nocturn 2013-04-10 16:18:32