Quantcast
Viewing all articles
Browse latest Browse all 4826

Is is possible to resolve/reject a promise from an action?

@andrew1 wrote:

I'm creating a dialog service and I want to have a function confirm() that will open a confirmation modal and return a promise. If the user clicks "confirm" I want to promise to resolve. If they click "cancel" I want the promise to reject.

This is what I have so far:

DialogService = Ember.Service.extend(
  {
    open: false
    title: null
    body: null

    confirm: (message) ->
      @set('title', message.title)
      @set('body', message.body)
      @set('open', true)

      return new Ember.RSVP.Promise (resolve, reject) =>
        Ember.run.later(@, (=> @reset(); reject()), 5000)

    reset: ->
     @set('open', false)
     @set('title', null)
     @set('body', null)
  }
)

And, the component that uses it:

DialogComponent = Ember.Component.extend(
  {
    dialog: Ember.inject.service()

    layout: hbs '
      {{#if dialog.open}}
        {{#g-modal dialog=true title=dialog.title}}
          {{dialog.body}}
          <br>
          {{#g-button}}
            Cancel
          {{/g-button}}
          {{#g-button primary=true}}
            Confirm
          {{/g-button}}
        {{/g-modal}}
      {{/if}}
    '

    actions:
      cancel: -> # this should reject the promise
      confirm: -> # this should resolve the promise
      }
    )
  }
)

This opens the dialog for 5 seconds and then closes it and rejects the promise. I'm just stumped as to how to get the cancel action on the component to call reject in the promise.

Any help would be greatly appreciated.

Thanks!

Posts: 3

Participants: 2

Read full topic


Viewing all articles
Browse latest Browse all 4826

Trending Articles