EmberJS 模板指定目标

2018-01-03 19:01 更新

描述

{{action}}助手可能会将操作直接发送到视图的控制器。目标选项用于备用目标。这最常用于将操作发送到视图而不是控制器。

语句

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

例子

<!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>

输出

让我们执行以下步骤,看看上面的代码如何工作:

  • 将上面的代码保存在temp_action_target.html文件中

  • 在浏览器中打开此HTML文件。

以上内容是否对您有帮助:
在线笔记
App下载
App下载

扫描二维码

下载编程狮App

公众号
微信公众号

编程狮公众号