EmberJS-Template Specifying a Target



Description

It is possible that {{action}} helper will send the action directly to the view's controller. The target option is used for an alternative target. This is most commonly used for to send an actions to a view instead of a controller.

Syntax

<button {{action "ActionName" parametrs target=view}}>Click</button>

Example

<!DOCTYPE html>
<html>
   <head>
      <title>Emberjs Specifying a Target</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">
         {{view Ember.CollectionView contentBinding="model" itemViewClass="App.View"}}
      </script>

      <script type="text/x-handlebars" data-template-name="index">
         {{#if view.isShowVisible}}
            {{view.content.tag}}<br/><br/>
            <!-- action directly send to the view's controller -->
            <button {{action "User" target="view"}}>Edit</button>
         {{else}}
            {{input type="text" value=view.content.tag}}<br/><br/>
            <button {{action "User" target="view"}}>Done</button>
         {{/if}}
      </script>

      <script type="text/javascript">
         App = Ember.Application.create();
         App.ApplicationRoute = Ember.Route.extend({
            //return the tag value
            model: function() {
            //specifying the primary value as tag:
               return [{tag:'Tutorialspoint'}];
            }
         });
         //creating the target view
         App.View = Ember.View.extend({
            templateName: 'index',
            isShowVisible: true,
            actions: {
               User: function(){
               //toggling the boolean property
                  this.toggleProperty('isShowVisible');
               }
            }
         });
      </script>
   </body>
</html>

Output

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

  • Save above code in temp_action_target.htm file

  • Open this HTML file in a browser.

Advertisements