visoft
9/25/2018 - 6:53 PM

Ember Sample Counter

Ember Sample Counter

{{counter-component}}
{{outlet}}
{{count}}

<button {{action "stopCounter"}}>Stop</button>
<button {{action "startCounter"}}>Start</button>
import Component from '@ember/component';
import { later } from '@ember/runloop';

export default Component.extend({
  count: 0,
  timerStatus: 1,

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

  updateCounter() {
    if (this.timerStatus) {
      this.incrementProperty('count');
      later(this, this.updateCounter, 1000);
    }
  },

  actions: {
    startCounter() {
      this.set('timerStatus', 1);
      this.updateCounter();
    },

    stopCounter() {
      this.set('timerStatus', 0);
    }
  }
});