[英]Backbone - sync doesn't detect my collection 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。 我在這個過程中遺漏了什么? 或者我不能像這樣立刻在我的服務器上創建所有這些新歌?
您需要單獨保存每首歌曲。 sync
不是直接調用的,只有方法'read'才能用於集合。 在collection.fetch()
期間調用sync('read')
collection.fetch()
。
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);
}
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.