5.7 components-sending Actions from Your application

Source: Internet
Author: User

I. Overview

1. When a component is used in a template, it has the ability to send the controller and routes action to the template. These allow components to notify application when a major event occurs, such as clicking on a particular element of a component.

2. Like the {{action}}handlebars helper, the actions from the component first go to the controller of the template. If the controller does not implement a handler for this action, it will bubble to the template's route and then rise to the routing level. To learn more about bubbling behavior, see action budding.

3. Components are designed to be reusable in different parts of your application. In order to achieve this reusability, it is important that when a component is used in a template, the actions that are sent by your component can be made.

4. In other words, if you are writing a button component, you do not want to send a click actionbecause it is ambiguous and may conflict with other components on the page. Idea, when your button is clicked, you want to allow others to use the component to specify which action to send.

5. Fortunately, the component has another sendaction () method that allows them to send the specified actions when a component is used in a template.

Second, sending a primary action

1. Many components send only one type of action. For example, a button component might send an action when clicked, and this is the primary action.

2. In order to set the primary action of a component, set its action property in handlebars:

{{My-button action= ' Showuser '}}

This tells the My-button component that it should send the showuser actionwhen it departs its primary action.

3. How do you trigger the operation of sending a component primary action? After the related event, you can call the sendaction () method without parameters:

App/components/my-button.js

default Ember.Component.extend ({  click () {    this. sendaction ();  }});

In the example above, when the component is clicked, theMy-button component will put Showuser action.

Iii. sending parameters with an action

1. You may want to provide an additional context for the route or controller to handle an action. For example, a button component might want to tell a controller that not only one piece of data has been deleted, but there are other.

2. In order to use the primary action to send parameters, call the sendaction () attachment an ' action ' string as the first parameter and the other extraneous parameters are immediately followed:

this. sendaction (' action ', param1, param2);

For example, the illusion that we are creating a todo list that allows the user to delete a TODO:

App/routes/index.js

default Ember.Route.extend ({  model () {    return  {      todos: [{        "learn Ember.js" )      }, {        "Walk the Dog"      }]    };  },  actions: {    Deletetodo (todo) {       varthis. Modelfor (' index '). Todos;      Todos.removeobject (TODO);}}  );

App/templates/index.hbs

{{#each model.todos as |todo|}}   < P > <  {{action ' Deletetodo ' Todo}}>Delete</button></  p>{{/each}}

3. We want to update this app, so the user must be sure that this is what they intend to do between actually deleting a todo. We will implement a component that checks first and the user two times before completing the action.

In this component, we set out primary action, and we will deliver the additional parameters that the user of a component can specify:

App/components/confirm-button.js

default Ember.Component.extend ({  actions: {    showconfirmation () {      this. Toggleproperty (' Isshowingconfirmation');    },    confirm () {      this. Toggleproperty (' Isshowingconfirmation ');        This this. get (' param '));}}  );

App/templates/components/confirm-button.hbs

{{#if isshowingconfirmation}}   <  {{action ' confirm '}}>Click again to confirm</button> { {else}}  <{{action ' showconfirmation '}}>{{ Title}}</button>{{/if}}

Now we can update our original template and replace {{action}}with our new build.

4. Note that we have sent the specified action by setting the component's action property, and we have specified which argument should be passed as a parameter by setting the component's param property.

Iv. Sending Multipe actions

1. Depending on the complexity of your component, you may want to allow users to specify several different actions for the different events generated by your component. For example, suppose you are writing a form component that the user can submit or cancel. Depending on which button the user clicks, you want to send a different Aciton to your controller or route.

2. You can specify which action is to be transmitted by passing the name of the event to SendAction () as the first argument.

{{User-form submit= "CreateUser" cancel= "Cancelusercreation"}}

In this case, you can send the createUser actionvia this.sendaction (' Submit ') , or by calling This.sendaction (' Cancel ') send cancelusercreation action.

V. Actions that aren ' t specified

If the user uses your component without specifying an action for a particular event, calling SendAction () will have no effect. For example, you define a component that triggers the primary action when clicked:

App/components/my-button.js

default Ember.Component.extend ({  click () {    this. sendaction ();  }});

If the user clicks on it, using this component without delegating a primary action will have no response.

{{My-button}}

Vi. thinking about component actions

1. In general, you should consider the actions of the component as translating a primitive event (the original event) (such as a mouse click or a <audio> pause event) to meaningful actions in the application.

2. This allows your routes and controllers to implement the Acton processor, similar to a name like this Deletetodo or songdidpause rather than a vague name clike or pause, for other developers, when reading the code outside the context, the name may be blurry.

3. Another way to consider component actions is as a public API for your component. Consider which event in the component can trigger actions in other applications, which is the primary way for other developers to use your component. In general, it is better to keep these leave more general, which makes the components more flexible and reusable.

5.7 components-sending Actions from Your application

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.