Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why use this.get('serviceName') instead of this.serviceName?

In Ember.JS, Is there a good reason to be doing this:

import Service, { inject } from '@ember/service';

export default Service.extend({
ajax: inject(),
getAll() {
    return this.get('ajax').request(`api/users/`, {
        method: 'GET',
        contentType: 'application/json'
    });
}
});

As opposed to this?

import Service, { inject } from '@ember/service';

export default Service.extend({
ajax: inject(),
getAll() {
    return this.ajax.request(`api/users/`, {
        method: 'GET',
        contentType: 'application/json'
    });
}
});

The second method looks cleaner IMO, but I'm wondering if there's a good functional reason to be using .get() over just referencing the service directly.

like image 325
PercivalMcGullicuddy Avatar asked Feb 27 '19 15:02

PercivalMcGullicuddy


1 Answers

the future is this.whateverProperty.

this.get was implemented in a time where JS lacked a lot of features, and a lot of documentation has yet to be updated.

The official ember guides are already updated:

https://guides.emberjs.com/release/applications/services/#toc_accessing-services

as of Ember 3.1, you can use native getters everywhere.

like image 65
NullVoxPopuli Avatar answered Sep 18 '22 08:09

NullVoxPopuli