mardi 5 mars 2019

Ember referencing property or getter

I'm a bit confused as to when to reference a property and when to use a getter, and why.

For example, a controller might inject a service for an uptodate location property:

export default Controller.extend({
    maps        : service(),

    location    : computed('maps.location', function() {
        return this.get('maps').getLocation()
    }),

But getLocation in the service is a simple getter:

getLocation() {
    return this.get('location')
},

Why not use one of these:

this.get('maps').location
this.get('maps').get('location')
this.get('maps.location')

and prevent having to write getters for every parameter? In the spirit of convention over configuration, isn't writing getters a bit redundant?

Simply put, when looking at examples and tutorials, I see different patterns:

  • service.property
  • service.get('property')
  • service.getProperty()

What is correct, when and why?




Aucun commentaire:

Enregistrer un commentaire