2012-05-14 64 views
14

我一直在尝试几天得到这个工作,我只是无法弄清楚为什么当我有我的视图摧毁一个模型属于一个集合(正确具有用于模型数据的开始提取的url属性),仅触发通过列表视图轻松绑定到集合的destroy'event'。但它永远不会向服务器发送实际的DELETE请求或任何请求。无处不在,我看到每个人都使用集合的url attr,或者如果模型没有连接到集合,则会使用urlRoot。我甚至在实际的this.model.destroy()之前进行了测试,以检查模型< console.log(this.model.url());Backbone.js model.destroy()不发送DELETE请求

我没有覆盖破坏或同步方法的主干。此外,每个模型都有一个id属性,通过集合的提取(从数据库记录)填充。

销毁发生在列表项视图中,并且集合的“销毁”事件在列表视图中被绑定。所有这一切都很好(事件处理),但问题再一次是没有对服务器的请求。

我希望backbone.js能自动完成。这就是文档所暗示的,以及无处不在的例子。

非常感谢任何能提供有用信息的人。

仅供参考:我正在开发wampserver PHP 5.3.4。

ListItemView = BaseView.extend({ 

    tagName: "li", 

    className: "shipment", 

    initialize: function (options) { 
     _.bindAll(this); 
     this.template = listItemTemplate; 
     this.templateEmpty = listItemTemplateEmpty; 
    }, 

    events: { 
     'click .itemTag' : 'toggleData', 
     'click select option' : 'chkShipper', 
     'click .update' : 'update', 
     'click button.delete' : 'removeItem' 
    }, 

    // .... 

    removeItem: function() { 
     debug.log('remove model'); 

     var id = this.model.id; 

     debug.log(this.model.url()); 

     var options = { 
      success: function(model, response) { 
       debug.log('remove success'); 
       //debug.log(model); 
       debug.log(response); 
       // this.unbind(); 
       // this.remove(); 
      }, 
      error: function(model, response) { 
       debug.log('remove error'); 
       debug.log(response); 
      } 
     }; 

     this.model.destroy(options); 


     //model.trigger('destroy', this.model, this.model.collection, options); 


    } 

}); 


Collection = Backbone.Collection.extend({ 

    model: Model, 

    url: '?dispatch=get&src=shipments', 
    url_put : '?dispatch=set&src=shipments', 

    name: 'Shipments', 

    initialize: function() { 
     _.bindAll(this); 
     this.deferred = new $.Deferred(); 
     /* 
     this.fetch({ 
      success: this.fetchSuccess, 
      error: this.fetchError 
     }); 
     */ 
    }, 

    fetchSuccess: function (collection, response) { 
     collection.deferred.resolve(); 
     debug.log(response); 
    }, 

    fetchError: function (collection, response) { 
     collection.deferred.reject(); 
     debug.log(response); 
     throw new Error(this.name + " fetch failed"); 
    }, 

    save: function() { 
     var that = this; 
     var proxy = _.extend(new Backbone.Model(), 
     { 
      url: this.url_put, 
      toJSON: function() { 
       return that.toJSON(); 
      } 
     }); 
     var newJSON = proxy.toJSON() 
     proxy.save(
      newJSON, 
      { 
       success: that.saveSuccess, 
       error: that.saveError 
      } 
     ); 
    }, 

    saveSuccess: function(model, response) { 
     debug.log('Save successful'); 
    }, 

    saveError: function(model, response) { 
     var responseText = response.responseText; 
     throw new Error(this.name + " save failed"); 
    }, 

    updateModels: function(newData) { 
     //this.reset(newData); 
    } 

}); 



ListView = BaseView.extend({ 

    tagName: "ul", 

    className: "shipments adminList", 

    _viewPointers: {}, 

    initialize: function() { 
     _.bindAll(this); 
     var that = this; 
     this.collection; 
     this.collection = new collections.ShipmentModel(); 
     this.collection.bind("add", this.addOne); 

     this.collection.fetch({ 
      success: this.collection.fetchSuccess, 
      error: this.collection.fetchError 
     }); 


     this.collection.bind("change", this.save); 
     this.collection.bind("add", this.addOne); 
     //this.collection.bind("remove", this.removeModel); 
     this.collection.bind("destroy", this.removeModel); 
     this.collection.bind("reset", this.render); 
     this.collection.deferred.done(function() { 
      //that.render(); 
      that.options.container.removeClass('hide'); 
     });    

     debug.log('view pointers'); 

     // debug.log(this._viewPointers['c31']); 
     // debug.log(this._viewPointers[0]); 

    }, 

    events: { 

    }, 

    save: function() { 
     debug.log('shipments changed'); 
     //this.collection.save(); 
     var that = this; 
     var proxy = _.extend(new Backbone.Model(), 
     { 
      url: that.collection.url_put, 
      toJSON: function() { 
       return that.collection.toJSON(); 
      } 
     }); 
     var newJSON = proxy.toJSON() 
     proxy.save(
      newJSON, 
      { 
       success: that.saveSuccess, 
       error: that.saveError 
      } 
     ); 
    }, 

    saveSuccess: function(model, response) { 
     debug.log('Save successful'); 
    }, 

    saveError: function(model, response) { 
     var responseText = response.responseText; 
     throw new Error(this.name + " save failed"); 
    }, 

    addOne: function(model) { 
     debug.log('added one'); 
     this.renderItem(model); 
     /* 
     var view = new SB.Views.TicketSummary({ 
      model: model 
     }); 
     this._viewPointers[model.cid] = view; 
     */ 
    }, 

    removeModel: function(model, response) { 
     // debug.log(model); 
     // debug.log('shipment removed from collection'); 

     // remove from server 
     debug.info('Removing view for ' + model.cid); 
     debug.info(this._viewPointers[model.cid]); 
     // this._viewPointers[model.cid].unbind(); 
     // this._viewPointers[model.cid].remove(); 
     debug.info('item removed'); 
     //this.render(); 
    }, 

    add: function() { 
     var nullModel = new this.collection.model({ 
      "poNum" : null, 
      "shipper" : null, 
      "proNum" : null, 
      "link" : null 
     }); 
     // var tmpl = emptyItemTmpl; 
     // debug.log(tmpl); 
     // this.$el.prepend(tmpl); 
     this.collection.unshift(nullModel); 
     this.renderInputItem(nullModel);     
    }, 

    render: function() { 
     this.$el.html(''); 
     debug.log('list view render'); 
     var i, len = this.collection.length; 
     for (i=0; i < len; i++) { 
      this.renderItem(this.collection.models[i]); 
     }; 
     $(this.container).find(this.className).remove(); 

     this.$el.prependTo(this.options.container); 

     return this; 
    },   

    renderItem: function (model) { 
     var item = new listItemView({ 
      "model": model 
     }); 

     // item.bind('removeItem', this.removeModel); 

     // this._viewPointers[model.cid] = item; 
     this._viewPointers[model.cid] = item; 
     debug.log(this._viewPointers[model.cid]); 
     item.render().$el.appendTo(this.$el); 
    }, 

    renderInputItem: function(model) { 
     var item = new listItemView({ 
      "model": model 
     }); 
     item.renderEmpty().$el.prependTo(this.$el); 
    } 

}); 

P.S ...再次,有从别处引用的代码。但是请注意:集合确实有一个url属性集。它在初始抓取时以及为保存对模型所做的更改而触发更改事件时都有效。但是,列表项视图中的销毁事件虽然成功触发了“销毁”事件,但不会发送“删除”HTTP请求。

+1

什么版本的骨干,发布一些代码总是有帮助,jsFiddle总是很棒 –

+0

是的请与我们分享一个非常简单的代码示例,重现此问题。 – fguillen

+1

你确定你已经在模型的扩展中包含了你的api的基础url吗? – kinakuta

回答

39

您的模型是否有ID?否则,HTTP请求将不会被发送。 - nikoshr 5月14日18:03

非常感谢! Nikoshr的小评论正是我所需要的。我花了最近5个小时搞乱了这个。我只需要在我的模型中添加一个id到默认值。

+2

要小心,将id属性添加到默认值将会破坏Backbone.Model.isNew(),然后.save()的POST和PUT。 – GijsjanB

+1

为什么这个答案不被接受:/(Y) – Tamil

+0

我认为盎司。在Stackoverflow是一个新手,他不知道如何做到这一点。 –