mike-north / ember-api-actions

Trigger API actions in ember.js apps
http://mike.works/ember-api-actions/
MIT License
331 stars 50 forks source link

API actions can't be called through ember-data relationship proxies #191

Open SaladFork opened 7 years ago

SaladFork commented 7 years ago

Assume you have models Person and Post, with the latter having a relationship to the former through author:

// models/person.js
import DS from 'ember-data';

export default DS.Model.extend({
  name: DS.attr('string')
});
// models/post.js
import DS from 'ember-data';

export default DS.Model.extend({
  title: DS.attr('string'),

  author: DS.belongsTo('person')
});

If we define an API action on Person:

 // models/person.js
 import DS from 'ember-data';
+import { memberAction } from 'ember-api-actions'; 

 export default DS.Model.extend({
   name: DS.attr('string'),

+  highFive: memberAction({ path: '/highFive' })
 });

Trying to call it through the relationship a few different ways seems to fail:

// Assume we have a post with an author
let post;

// Let's get its author
let author = post.get('author');
  // => <DS.PromiseObject:ember1234>
author.get('name');
  // => "Foo Author"

// Let's high five the author
author.highFive();
  // => Uncaught TypeError: author.highFive is not a function

// Hmm?
author.highFive;
  // => undefined

// Alright let's use `.get` to jump through the proxy
author.get('highFive');
  // => function (payload) { ...

// Great let's use it
author.get('highFive')();
  // => Uncaught TypeError: Cannot read property 'adapterFor' of undefined

Error thrown from this line.

kjhangiani commented 7 years ago

Your relationship is async, thus your initial let author = post.get('author') is returning a promise, which you cannot call an action on.

However, if you changed it to

post.get('author').then((author) => {
 author.highFive();
});

I'm guessing it will work fine

sahilpaudel-pe commented 3 years ago

I have this function called on click of button

runTestCases(ruleSetVersion) {
      ruleSetVersion.runTestCasesNow().then(response => {
        this.sendAction('runTestCases', response)
      }).then(() => this.set('model', null))
    },

it is never getting resolved.

 runTestCasesNow: memberAction({
    path: 'run_test_cases',
    type: 'post', // HTTP POST request
    urlType: 'findRecord', // Base of the URL that's generated for the action
    after(response) {
      console.log("It is resolved", response) // not this one as well
    }
  }),

Basically, the after-hook is not getting triggered