Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

201
Visualizações
Ember - Loading an URL dynamically for button

New to ember, I wonder what is the best way to go about this:

component.js
import Component from '@ember/component';
import { inject as service } from '@ember/service';

export default Component.extend({
  ajax: service(),

  url: '',

  async fetchUrl() {
    try {
      let result = await this.ajax.request(
        `api/urlgenerator`
      );
      this.url = result.link;
    } catch (err) {
      this.errorHandler.handle(err);
    }
  },

  init() {
    this._super(...arguments);
    this.fetchUrl();
  },
});

and my template

{{#card/banner}}
  <Banner
    @text="Template Button"
    @action={{hash
      text="Enable Launcher"
      onAction=(track-action
        (route-action "openUrl" this.url)
      )
    }}
  />
{{/card/banner}}

openUrl is simple a global method responsible for opening an external URL

--

Not too sure if this is the right way to go about it, right now it is not working, since maybe due to ember component lifecycle, the variable gets rendered with the initial value only (empty '')

However, If I hardcode the url for example, it works fine.

Can someone give me some advice on it?

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

In classic component style you would use this.set('url', result.link) instead of this.url = result.link. However in modern ember I would do it like this:

export default class MyComponent extends Component {
  @service ajax;

  @tracked url = '';

  constructor() {
    super(...arguments);
    this.fetchUrl();
  }
  async fetchUrl() {
    try {
      let result = await this.ajax.request(
        `api/urlgenerator`
      );
      this.url = result.link;
    } catch (err) {
      this.errorHandler.handle(err);
    }
  }
}

notice url must be @tracked.

about 4 years ago · Juan Pablo Isaza Relatório

0

In case it helps someone, I used this approach The Guide to Promises in Computed Properties

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda