Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can the handler functions deal with non-rest adapters? #121

Closed
jonocodes opened this issue Aug 18, 2015 · 3 comments
Closed

Can the handler functions deal with non-rest adapters? #121

jonocodes opened this issue Aug 18, 2015 · 3 comments

Comments

@jonocodes
Copy link

I have a custom adapter which uses a different path for a PUT:

export default DS.ActiveModelAdapter.extend({
    updateRecord: function(store, type, snapshot) {

      if (type.typeKey === 'person') {
        var data = {};
        var serializer = store.serializerFor(type.typeKey);
        serializer.serializeIntoHash(data, type, snapshot);
        return this.ajax(this.buildURL(type.typeKey), "PUT", { data: data });    // NOTE: id is not used here
      }

      return this._super(store, type, snapshot);
    }
});

However it seams this wont work with handleUpdate because it has a hardcoded buildUrl that looks like it follows the RestAdapter only:
https://github.com/danielspaniel/ember-data-factory-guy/blob/master/addon/factory-guy-test-helper.js#L348

Is there a good way to override this in Factory Guy?

@danielspaniel
Copy link
Collaborator

Yes @jonocodes, you could do this:

// file -> tests/helpers/custom-test-helper.js

import FactoryGuyTestHelper from 'ember-data-factory-guy/factory-guy-test-helper';

var CustomTestHelper = FactoryGuyTestHelper.reopen({

 buildURL: function (modelName, id) {
    if ( modelName === 'whatever') {
       // do your own thing
    } else {
       // do what factory guy usually does
       this._super(modelName, id);
    }
  }

});

export default CustomTestHelper;

@jonocodes
Copy link
Author

Thanks for the tip. I ended up doing the following:

import FactoryGuyTestHelper from 'ember-data-factory-guy/factory-guy-test-helper';
import MockUpdateRequest from 'ember-data-factory-guy/mock-update-request';

export default FactoryGuyTestHelper.reopen({

  // customize the helper to follow our custom adapter for PUT

  handleUpdate: function() {

    var args = Array.prototype.slice.call(arguments);
    Ember.assert("To handleUpdate pass in a model instance or a type and an id", args.length>0);

    var options = {};
    if (args.length > 1 && typeof args[args.length - 1] === 'object') {
      options = args.pop();
    }

    var model, type, id;
    var store = this.getStore();

    if (args[0] instanceof DS.Model) {
      model = args[0];
      id = model.id;
      type = model.constructor.typeKey;
    } else if (typeof args[0] === "string" && typeof parseInt(args[1]) === "number") {
      type = args[0];
      id = args[1];
      model = store.getById(type, id);
    }
    Ember.assert("To handleUpdate pass in a model instance or a type and an id",type && id);

    if ( type === 'person') {
      var url = this.buildURL(type);
      return new MockUpdateRequest(url, model, this.mapFind, options);
    }

    return this._super(type, id);
  }

});

@danielspaniel
Copy link
Collaborator

got it @jonocodes, if you come up with a better way to handle this and want to submit a pull request .. feel free.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants