【问题标题】:Backbone - sync doesn't detect my collection url骨干网 - 同步未检测到我的收藏网址
【发布时间】:2013-04-08 21:45:54
【问题描述】:

我有专辑和歌曲的关系。在新专辑视图中我同时要求歌曲,我试图先保存专辑模型,然后保存歌曲集并将其附加到专辑中。

我的歌曲集定义:

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。我在这个过程中遗漏了什么?或者我不能像这样一次在我的服务器中创建所有这些新歌曲?

【问题讨论】:

  • 尝试设置模型的 'urlRoot' 属性(不是集合)。
  • 我也试过了,但总是返回同样的错误。

标签: javascript jquery backbone.js backbone-relational


【解决方案1】:

您需要单独保存每首歌曲。 sync 并不意味着直接调用,只有方法“读取”用于处理集合。 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);                         
    }
 });

【讨论】:

  • 谢谢!这就是问题所在。我认为您可以一次保存整个收藏集。
猜你喜欢
  • 2012-09-14
  • 1970-01-01
  • 2021-06-29
  • 2012-07-03
  • 1970-01-01
  • 2013-08-03
  • 1970-01-01
  • 2015-01-04
  • 2017-07-13
相关资源
最近更新 更多