ember-async-button icon indicating copy to clipboard operation
ember-async-button copied to clipboard

Examples should include the example in the demo.

Open NullVoxPopuli opened this issue 9 years ago • 3 comments

I was trying so many different combinations of making promises and returning them in the action... I discovered in the demo, a callback is used.

http://jsbin.com/qokogasilu/1/edit?html,css,js,output

so, in my code, I now have this:

  actions: {
    // callback required for async button
    register(callback) {
      this.get('pathStore').storeCurrentRoute();

      let promise = this.get('model').save().then(user => {
        this.sendAction('successAction');
        user.unloadRecord();
      });

      callback(promise);
    },
  },

and now my async-button correctly stops being in the pending state upon promise return (presumably -- it's still unclear why there needs to be a callback with a promised passed - esp since this is an anti-pattern in ember).

NullVoxPopuli avatar Aug 14 '16 05:08 NullVoxPopuli

This is probably too late to answer but I was stumbling on this too, I think your code should be more like:

actions: {
        // callback required for async button
        register(callback) {
          this.get('pathStore').storeCurrentRoute();
          const promise = new Ember.RSVP.Promise(function(resolve, reject) {
            this.get('model').save().then(() => {
              this.sendAction('successAction');
              user.unloadRecord();
              Ember.run.later(function() {
                resolve();
              });
            }).catch((reason) => {
              Ember.run.later(function() {
                reject();
              });
            });            
          };
          callback(promise);
        },
      },

pcambra avatar Nov 30 '16 04:11 pcambra

It's never too late. :-).

Why the run.later? would that make the button stay spinning longer than the request? (a request is only being made on save.

NullVoxPopuli avatar Dec 04 '16 17:12 NullVoxPopuli

Not super proficient in Ember yet, run.later helped for a use case I had, I guess you can do resolve/reject directly?

pcambra avatar Dec 05 '16 04:12 pcambra