簡體   English   中英

定義定義收集的自定義方法

[英]backbone define custom method for collection

我想為fetch()方法創建自定義包裝器方法,如下所示:

let personCollection = Collection.extend({
    mainIndex: 'id',
    url: 'https://jsonplaceholder.typicode.com/posts',
    model:Person,

    findAll:function(){
      return this.fetch({url:this.url, type: 'POST'});
    }

});

我想在findAll()方法中更改url路徑和http方法,但是上面的代碼發送GET請求

看下面的代碼,這可能是一個“&”號問題: https : //github.com/AmpersandJS/ampersand-sync/blob/master/core.js#L73

  // If passed a data param, we add it to the URL or body depending on request type
  if (options.data && type === 'GET') {
      // make sure we've got a '?'
      options.url += includes(options.url, '?') ? '&' : '?';
      options.url += qs.stringify(options.data);
      //delete `data` so `xhr` doesn't use it as a body
      delete options.data;
  }

這里的type變量來自methodMap即使您可能已在選項中將其覆蓋。

但是,如果沒有調試,則無法確定。 您應該逐步檢查它,看看發送到xhrImplementation內容是什么。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM