3

So I have a Rails API and an Ember application. At the moment, my Ember has a login.hbs template and a login.js controller.

I have done a ember install ember-ajax (https://github.com/ember-cli/ember-ajax).

On entering an email and password, I click on the login button which calls my login.js action loginClicked()

I have the following in my login controller:

// login.js controller
import Ember from 'ember';

export default Ember.Controller.extend({
  email: '',
  password: '',

  actions: {
    loginClicked() {
      // alert("email: " + this.get('email') + "\npassword: " + this.get('password'));

      let params = {
        email: this.get('email'),
        password: this.get('password')
      };

      post('/authenticate', {
        params: params
      });
    }
  }
});

In my login.js route handler, I have injected the ember-ajax service:

// login.js route handler
import Ember from 'ember';

export default Ember.Route.extend({
  ajax: Ember.inject.service()
});

The problem is, my Mac terminal console is outputting an error saying:

controllers/login.js: line 16, col 7, 'post' is not defined.

I have also tried injecting the ember-ajax service into my controller but it made no difference.

Am I doing something wrong ?

1 Answer 1

4

Everything is described into the ember-ajax github page https://github.com/ember-cli/ember-ajax

export default Ember.Controller.extend({
  ajax: Ember.inject.service(),

  actions: {
    loginClicked() {
      let params = {
        email: this.get('email'),
        password: this.get('password')
      };

      return this.get('ajax').request('/authenticate', {
        method: 'POST',
        data: params
      });
    }
  }
});

Basically, to access any property of your controller (component, ...) in ember, you need to get it using this.get('propertyName'). Here you need to use the request method of the ajax property (the injected service).

Sign up to request clarification or add additional context in comments.

1 Comment

Yes, I just realised I needed to type this.get('ajax').post(...) rather than just post(...) :D

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.