0

I'm in a route called "game".

In an action I call Ember.run.debounce for a function, but in that function I can't call other functions.

app/routes/game.js:

import Ember from 'ember';
export default Ember.Route.extend({

  model() {
    ...
  },

  saveGame: function () {
    this.save().then(() => {
      this.updateOtherGames(); //<------ ERROR ERROR ERROR ERROR
    })
  },

  updateOtherGames(game) {

  },

  actions: {
    playGame(game) {
      ...
      Ember.run.debounce(game, this.saveGame, 5000);
    }
  }
})

Why I can't call updateOtherGames?

Why this in the saveGame function is only referring to game nd not the route?

1 Answers1

1

The correct usage of debounce is:

Ember.run.debounce(this, this.saveGame, game, 5000);

Here is the API.

After correcting it, calling updateOtherGames will work.

ykaragol
  • 6,139
  • 3
  • 29
  • 56
  • Do you agree with me that the documentation on this point is a bit confusing? I will try your method soon. –  Apr 16 '17 at 12:11
  • Definition of `target` is _target of method to invoke_. At sample code, `target` shown as `context`. Yes a bit confusing. Actually, `target` means who will be `this` in that function context. – ykaragol Apr 16 '17 at 15:08
  • A guy on ember forum answered this: https://discuss.emberjs.com/t/ember-2-call-functions-in-route-using-ember-run-debounce/12776/2 What is correct? Your or this one? –  Apr 16 '17 at 15:15
  • Actually, it is same. That answer uses a fat arrow function call and passes the argument. Mine uses debounce 3th parameter... Both is ok. – ykaragol Apr 16 '17 at 15:41
  • For fat arrow have a look at [this answer](http://stackoverflow.com/questions/37097598/nesting-functions-in-js/37097783#37097783). This answer may help you to understand better. – ykaragol Apr 16 '17 at 15:44