Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

197
Views
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 answers
Answer question

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 Report

0

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

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!