Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save joshlevinson/1f451a039e357fad2fc6 to your computer and use it in GitHub Desktop.
Save joshlevinson/1f451a039e357fad2fc6 to your computer and use it in GitHub Desktop.

Revisions

  1. @aceofspades aceofspades revised this gist Feb 2, 2015. 1 changed file with 1 addition and 2 deletions.
    3 changes: 1 addition & 2 deletions gistfile1.js
    Original file line number Diff line number Diff line change
    @@ -51,9 +51,8 @@ export default Ember.Mixin.create({
    },

    // Trigger action explicitly
    delegateAction(actionName) {
    delegateAction(actionName, ...contexts) {
    'use strict';
    var contexts = Array.prototype.slice.call(arguments, 1);
    Ember.Logger.debug("delegateAction", actionName, contexts);
    this.triggerAction({
    action: actionName,
  2. @aceofspades aceofspades revised this gist Feb 2, 2015. 1 changed file with 4 additions and 4 deletions.
    8 changes: 4 additions & 4 deletions gistfile1.js
    Original file line number Diff line number Diff line change
    @@ -33,18 +33,18 @@ export default Ember.Mixin.create({
    if (!this._actions || !this._actions[actionName]) {
    if (this.get('delegateAllActions')) {
    // Delegate all actions
    Ember.Logger.debug("Action delegated implicitly by #{this.toString()}:", actionName);
    Ember.Logger.debug(`Action delegated implicitly by ${this.toString()}:`, actionName);
    return this.delegateAction.apply(this, arguments);
    } else {
    Ember.Logger.warn("Actions not delegated by #{this.toString()}:", actionName);
    Ember.Logger.warn(`Actions not delegated by ${this.toString()}:`, actionName);
    }

    var delegatedActions = this.get('_delegatedActions');
    if (delegatedActions.contains(actionName) && this.sendAction) {
    Ember.Logger.info("Action delegated by #{this.toString()}:", actionName);
    Ember.Logger.info(`Action delegated by ${this.toString()}:`, actionName);
    return this.delegateAction.apply(this, arguments);
    } else {
    Ember.Logger.warn("Action not delegated by #{this.toString()}:", actionName);
    Ember.Logger.warn(`Action not delegated by ${this.toString()}:`, actionName);
    }
    }
    return this._super.apply(this, arguments);
  3. @aceofspades aceofspades created this gist Feb 2, 2015.
    65 changes: 65 additions & 0 deletions gistfile1.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,65 @@
    /* jshint esnext: true */

    import Ember from 'ember';

    // When mixed into an Ember.Component class, actions can be explicitly or implicitly delegated to the parent.
    // In general this makes the most sense for an app-level component, where actions are not mututated as they bubble
    // up to higher levels.
    export default Ember.Mixin.create({

    // When true, all actions will be implicitly passed on to the parent
    delegateAllActions: false,

    // Explicitly delegated actions
    // example: delegatedActions: ['select', 'show', 'edit', 'destroy']
    // May also be declared a as a comma-separated attribute in your template
    // Example: {{my-component delegatedActions="select,show,edit"}}
    delegatedActions: [],

    _delegatedActions: Ember.computed('delegatedActions', function() {
    'use strict';
    var delegatedActions = this.get('delegatedActions');
    if (typeof(delegatedActions) === 'string') {
    return delegatedActions.split(',').map(s => s.trim());
    } else {
    return delegatedActions;
    }
    }),

    // Override send to call delegatedActions as appropriate
    // If an action handler is defined, event will not be delegated
    send(actionName) {
    'use strict';
    if (!this._actions || !this._actions[actionName]) {
    if (this.get('delegateAllActions')) {
    // Delegate all actions
    Ember.Logger.debug("Action delegated implicitly by #{this.toString()}:", actionName);
    return this.delegateAction.apply(this, arguments);
    } else {
    Ember.Logger.warn("Actions not delegated by #{this.toString()}:", actionName);
    }

    var delegatedActions = this.get('_delegatedActions');
    if (delegatedActions.contains(actionName) && this.sendAction) {
    Ember.Logger.info("Action delegated by #{this.toString()}:", actionName);
    return this.delegateAction.apply(this, arguments);
    } else {
    Ember.Logger.warn("Action not delegated by #{this.toString()}:", actionName);
    }
    }
    return this._super.apply(this, arguments);
    },

    // Trigger action explicitly
    delegateAction(actionName) {
    'use strict';
    var contexts = Array.prototype.slice.call(arguments, 1);
    Ember.Logger.debug("delegateAction", actionName, contexts);
    this.triggerAction({
    action: actionName,
    actionContext: contexts
    });
    return;
    }

    });