0

アルバムと曲の関係があります。同時に曲を求めている新しいアルバムビューで、最初にアルバムモデルを保存してから、曲のコレクションを保存してアルバムに添付しようとしています。

私の歌のコレクションの定義:

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 が正しく返されました。私はその過程で何かが欠けていますか?または、このように一度にすべての新しい曲をサーバーで作成することはできませんか?

4

1 に答える 1

2

各曲を個別に保存する必要があります。sync直接呼び出されることは意図されておらず、メソッド「read」のみがコレクションで動作することを意図しています。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);                         
    }
 });
于 2013-04-08T21:53:59.767 に答える