Ember.ViewTargetActionSupport Class packages/ember-views/lib/mixins/view_target_action_support.js:5


PRIVATE

Ember.ViewTargetActionSupport is a mixin that can be included in a view class to add a triggerAction method with semantics similar to the Handlebars {{action}} helper. It provides intelligent defaults for the action's target: the view's controller; and the context that is sent with the action: the view's context.

Note: In normal Ember usage, the {{action}} helper is usually the best choice. This mixin is most often useful when you are doing more complex event handling in custom View subclasses.

For example:

1
2
3
4
5
6
7
App.SaveButtonView = Ember.View.extend(Ember.ViewTargetActionSupport, {
  action: 'save',
  click: function() {
    this.triggerAction(); // Sends the `save` action, along with the current context
                          // to the current controller
  }
});

The action can be provided as properties of an optional object argument to triggerAction as well.

1
2
3
4
5
6
7
8
App.SaveButtonView = Ember.View.extend(Ember.ViewTargetActionSupport, {
  click: function() {
    this.triggerAction({
      action: 'save'
    }); // Sends the `save` action, along with the current context
        // to the current controller
  }
});
Show:

Methods

Properties

Show:

apply

(obj) private

Parameters:

obj

Returns:

applied object

create

(arguments) public static

Parameters:

arguments

createChildView

(viewClass, attrs) Ember.View private

Instantiates a view to be added to the childViews array during view initialization. You generally will not call this method directly unless you are overriding createChildViews(). Note that this method will automatically configure the correct settings on the new view instance to act as a child of the parent.

Parameters:

viewClass Class|String
attrs [Object]
Attributes to add

Returns:

Ember.View
new instance

detect

(obj) Boolean private

Parameters:

obj

Returns:

Boolean

removeChild

(view) Ember.View private

Removes the child view from the parent view.

Parameters:

view Ember.View

Returns:

Ember.View
receiver

reopen

(arguments) private

Parameters:

arguments

triggerAction

(opts) Boolean private

Send an action with an actionContext to a target. The action, actionContext and target will be retrieved from properties of the object. For example:

1
2
3
4
5
6
7
8
9
App.SaveButtonView = Ember.View.extend(Ember.TargetActionSupport, {
  target: Ember.computed.alias('controller'),
  action: 'save',
  actionContext: Ember.computed.alias('context'),
  click: function() {
    this.triggerAction(); // Sends the `save` action, along with the current context
                          // to the current controller
  }
});

The target, action, and actionContext can be provided as properties of an optional object argument to triggerAction as well.

1
2
3
4
5
6
7
8
9
10
App.SaveButtonView = Ember.View.extend(Ember.TargetActionSupport, {
  click: function() {
    this.triggerAction({
      action: 'save',
      target: this.get('controller'),
      actionContext: this.get('context')
    }); // Sends the `save` action, along with the current context
        // to the current controller
  }
});

The actionContext defaults to the object you are mixing TargetActionSupport into. But target and action must be specified either as properties or with the argument to triggerAction, or a combination:

1
2
3
4
5
6
7
8
9
App.SaveButtonView = Ember.View.extend(Ember.TargetActionSupport, {
  target: Ember.computed.alias('controller'),
  click: function() {
    this.triggerAction({
      action: 'save'
    }); // Sends the `save` action, along with a reference to `this`,
        // to the current controller
  }
});

Parameters:

opts Object
(optional, with the optional keys action, target and/or actionContext)

Returns:

Boolean
true if the action was sent successfully and did not return false
Show:

_context

private

Private copy of the view's template context. This can be set directly by Handlebars without triggering the observer that causes the view to be re-rendered.

The context of a view is looked up as follows:

  1. Supplied context (usually by Handlebars)
  2. Specified controller
  3. parentView's context (for a child of a ContainerView)

The code in Handlebars that overrides the _context property first checks to see whether the view has a specified controller. This is something of a hack and should be revisited.

actionContext

private

childViews

Array private

Array of child views. You should never edit this array directly. Instead, use appendChild and removeFromParent.

Default: []

context

Object private

The object from which templates should access properties.

This object will be passed to the template function each time the render method is called, but it is up to the individual function to decide what to do with it.

By default, this will be the view's controller.

controller

Object private

The controller managing this view. If this property is set, it will be made available for use by the template.

target

private