Jacob van Lingen
Jacob van Lingen

Reputation: 9537

Ember route has action

I am building a mixin for different routes to handle a save action. Sometimes after saving I want to do extra things, so I want to send a saveCallback action back to the route.

Is it possible to ask to a route if a action is defined? Or else: can I retrieve all functions of a route, so I could check myself if a route is defined?

Code mixin:

export default Ember.Mixin.create({
    actions: {
        save: {
            var self = this;

            this.get('model').save()
            .then(function(result) {
                //do stuff

                //Something like if(self.hasDefined('saveCallBack')) {
                    self.send('saveCallback')
                }
            }, function(errorReason) {
                //error handling
            });
        }
    }
}

I can't just fire the send action, as a Nothing handled the action 'saveCallback' error is thrown if the action is not defined.

Upvotes: 0

Views: 135

Answers (1)

GJK
GJK

Reputation: 37369

I can't find the issue now, but I read an issue on Github at some point about adding a canSendAction (or something similar) function for this reason. For now, just define the same action on your mixin, and have it bubble.

export default Ember.Mixin.create({
    actions: {
        save: function() {
            var self = this;

            this.get('model').save().then(function() {
                self.send('saveCallback');
            });
        },

        saveCallback: function() {
            return true;
        }
    }
});

EDIT: I'm being dumb. Just return true and the action should bubble up the hierarchy and will be caught by another action if it exists.

Upvotes: 2

Related Questions