EmberJS-Components Sending a Primary Action



Description

In the primary action, the components send one kind of an action. For instance, when you clicked on a button, it sends an action to the component and this is known as primary action.

Syntax

{{my-button action="actions"}}

Example

<!DOCTYPE html>
<html>
   <head>
      <title>Emberjs Sending a Primary Action</title>
      <!-- CDN's -->
      <script src="https://cdnjs.cloudflare.com/ajax/libs/handlebars.js/3.0.1/handlebars.min.js"></script>
      <script src="https://code.jquery.com/jquery-2.1.3.min.js"></script>
      <script src="https://cdnjs.cloudflare.com/ajax/libs/ember.js/1.10.0/ember.min.js"></script>
      <script src="https://builds.emberjs.com/tags/v1.10.0-beta.3/ember-template-compiler.js"></script>
      <script src="https://builds.emberjs.com/release/ember.debug.js"></script>
      <script src="https://builds.emberjs.com/beta/ember-data.js"></script>
   </head>
   <body>
      <script type="text/x-handlebars" data-template-name="index">
         <p>{{my-comp title=title action="compFunc"}}</p>
      </script>

      <script type="text/x-handlebars" data-template-name="components/my-comp">
         <h2>Click button for primary action</h2>
         <input type="button" value="Click Me" {{action "compFunc"}} /><br/>
         <p><b>{{title}}</b></p>
      </script>

      <script type="text/javascript">
         App = Ember.Application.create();

         App.MyCompComponent = Ember.Component.extend({
            //defining the action hook to set the title property value
            actions: {
               compFunc: function() {
                  this.set('title', " Say Hello To Tutorialspoint...");
                  //This method sends the specified action when the component is used in a template
                  this.sendAction();
               }
            }
         });
      </script>
   </body>
</html>

Output

Let's carry out the following steps to see how above code works −

  • Save above code in comp_prmry_act.htm file

  • Open this HTML file in a browser.

Advertisements