indexiatech / ember-idx-button

An ambitious button with async & styling support for Ember.js
http://indexiatech.github.io/ember-idx-button
Apache License 2.0
11 stars 3 forks source link

How to use with ember-idx-forms #5

Open silverqx opened 9 years ago

silverqx commented 9 years ago

Hi,

I want to ask, how to use this async button as submit button with ember-idx-forms?

Because when I click on async button, so first is called action, which is defined on on-click property and after that is called submit method at https://github.com/indexiatech/ember-forms/blob/master/addon/form.js#L52 and after that is called submit handler in the controller or route.

If you have ready some working example, pls paste it here.

Thank you for advices.

silverqx commented 9 years ago

So I have resolved it with Deffered.

Wait and see if you have a better solution, before closing this issue.

import Em from "ember";

var _asyncSubmitDeffer = Ember.RSVP.defer();

export default Em.Controller.extend({

  actions: {

    submit: function() {
      var model = this.get('model');

      if (model.get('isDirty')) {
        model.save().then((data) => {
          _asyncSubmitDeffer.resolve();
          this.transitionToRoute('users')
        }).catch(function (reason) {
          _asyncSubmitDeffer.reject('Server error...');
        }).finally(() => {
        });
      } else {
        _asyncSubmitDeffer.resolve();
      }
    },

    asyncSumbit: function(setPromise) {
      return setPromise(_asyncSubmitDeffer.promise);
    }
  }
});