2012-05-14 7 views
14

私は数日間この作業をしようとしていましたが、コレクションに属するモデルモデルのデータの最初のフェッチのためのurl属性を持っています)、リストビューで簡単にバインドできるようにコレクションにバブリングされた破棄イベントのみが発生します。しかし、実際のDELETE要求や任意の要求をサーバーにまったく送信することはありません。私が見たどこでも、モデルがコレクションに接続されていない場合は、コレクションのurl attrまたはurlRootのいずれかを使用するすべての人が表示されます。私はモデル< console.log(this.model.url())をチェックするために実際のthis.model.destroy()の前にテストしました。Backbone.js model.destroy()DELETEリクエストを送信していません

私は、バックボーンの破棄方法や同期方法を上書きしていません。また、各モデルには、(データベースレコードからの)コレクションのフェッチによって生成されるid属性があります。

リストアイテムビューで破棄が行われ、コレクションの「破棄」イベントがリストビューでバインドされます。すべてうまくいきます(イベント処理)が、サーバーへの要求はありません。

私は、backbone.jsがそれを自動的に行うことを望んでいました。これは、ドキュメンテーションが暗示していることだけでなく、あらゆる場所にある多くの例でもありました。

いくつかの有用な情報を与えることができます。

FYI: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属性が設定されています。また、初期フェッチや、モデルに加えられた変更を保存するための変更イベントが発生したときにも機能します。しかし、リストアイテムビューのdestroyイベントは、 "destroy"イベントを正常にトリガしますが、 'DELETE' HTTPリクエストを送信しません。

+1

バックボーンのバージョンは、いくつかのコードを投稿すると常に役立ちます.jsFiddleは常に素晴らしいです –

+0

はい、私たちに問題を再現する非常に簡単なコード例を教えてください。 – fguillen

+1

あなたのモデルの拡張機能にapiのベースURLが含まれていることは確かですか? – kinakuta

答えて

39

モデルにIDがありますか?そうでない場合、HTTP要求は送信されません。 - nikoshr May 14 at 18:03

ありがとうございます! Nikoshrの小さなコメントはまさに私が必要としたものでした。私は最後の5時間をこれを乱して過ごしました。モデルのデフォルトにIDを追加するだけでした。

+2

デフォルト値にid属性を追加すると、Backbone.Model.isNew()とその後の.save()のPOSTとPUTが壊れます。 – GijsjanB

+1

これはなぜ受け入れられないのですか:/(Y) – Tamil

+0

私はオズだと思います。 Stackoverflowの初心者で、彼はそれをする方法を知らない。 –

関連する問題