backbone定义集合的自定义方法

时间:2017-03-03 16:19:52

标签: javascript backbone.js ampersand.js

我想为fetch()方法创建自定义包装器方法,如下所示:

import { TestBed, fakeAsync, inject, tick } from '@angular/core/testing';
import { MockBackend } from '@angular/http/testing';
import { Http, BaseRequestOptions, Response, ResponseOptions } from '@angular/http';
import { Observable } from 'rxjs/Rx';
import { ItemService } from './item.service';
import { Item } from './item';


describe('ItemService', () => {
  let mockResponse, matchingItem, connection;

  beforeEach(() => {
    TestBed.configureTestingModule({
      providers: [
        ItemService,
        MockBackend,
        BaseRequestOptions,
        {
          provide: Http,
          useFactory: (backend, defaultOptions) => new Http(backend, defaultOptions),
          deps: [MockBackend, BaseRequestOptions]
        },
        // { provide: XHRBackend, useClass: MockBackend }        
      ]
    });

    const items = [
      {
        "itemId":2,
        "itemName":"test item1",
        "itemDescription":"hello hello"        
      },
      {
        "itemId":1,
        "itemName":"name2124111121",
        "itemDescription":"description212412112"        
      }
    ];
    mockResponse = new Response(new ResponseOptions({body: {data: items}, status: 200}));
  });

  describe('getItems', () => {

    //Subscribing to the connection and storing it for later
    it('should return all the items',inject([ItemService, MockBackend], (service: ItemService, backend: MockBackend) => {

        backend.connections.subscribe(connection => {
          connection.mockRespond(mockResponse);
        });
        service.getItems({isActive: true, sortColumn: "lastModifiedDateUtc", sortOrder: "desc"})
        .subscribe((items: Item[]) => {
          expect(items.length).toBe(2);
        });
      }));
    });
  });

});

在findAll()方法中我想要更改url路径和http方法, 但是上面的代码发送 GET请求

1 个答案:

答案 0 :(得分:0)

这可能是一个Ampersand问题,看看这段代码: 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的内容。