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

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

Open
SaladFork opened this issue May 19, 2017 · 2 comments
Open

Comments

@SaladFork
Copy link

SaladFork commented May 19, 2017

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
Copy link

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
Copy link

sahilpaudel-pe commented Mar 16, 2021

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

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

3 participants