Return a promise from a controller action in Ember?

I have a component that needs to communicate with a controller and eventually perform some clean up after the controller says everything is ok (ie, jQuery "un"-initialization). I think the best way to accomplish this is with a promise so that the component can clean up after the controller completes its task. But how can a controller action return a promise? Alternatively, can a component call a dynamic method directly on a controller?

For example, lets say I have a ModalDialogComponent.

App.ModalDialogComponent = Ember.Component.extend
  didInsertElement: -> 

    save: ->

    # some other actions are omitted

  closeModal: ->

And I can instantiate the component inside a template named foo,

{{modal-form save="save" ...}}

And implement the save method on FooController

App.FooController = Ember.ObjectController.extend
  save: ->
    # how can we tell the component that this was successful?

As you can see, I only want the closeModal function to execute if the save action was successful. In other words, only close the modal if the record was saved successfully.

Is this possible, or am I going about it completely wrong?



send and sendAction are one way streets, that being said, you can send a defer to the action and expect it to resolve/reject it.

var defer = Ember.RSVP.defer();


  defer.resolve('hello world');

Yours would look a bit like this

var defer = Ember.RSVP.defer(),
    self = this;


this.sendAction('save', defer);

save action

actions: {
  save: function(defer){

    // if succeeded 

    // or if failure occurs 

Be careful, you want to make sure you don't leave out the reject route, you'd hate to have the modal stuck up there because the save failed and there wasn't a failure method.

Sorry I didn't convert to coffee script, I figure you'll either understand or convert and understand and I won't have given you a wrong answer.


Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.