Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Examples should include the example in the demo. #92

Open
NullVoxPopuli opened this issue Aug 14, 2016 · 3 comments
Open

Examples should include the example in the demo. #92

NullVoxPopuli opened this issue Aug 14, 2016 · 3 comments

Comments

@NullVoxPopuli
Copy link

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).

@pcambra
Copy link

pcambra commented Nov 30, 2016

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);
        },
      },

@NullVoxPopuli
Copy link
Author

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.

@pcambra
Copy link

pcambra commented Dec 5, 2016

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants