EmberJS 模板助手依赖关系

2018-01-04 12:54 更新

描述

如果要渲染属性,则需要通过更改其中一个属性来更新输出。当任何从属密钥更改时,输出将自动更新。

语句

Ember.Handlebars.registerBoundHelper( function(params)
{
   //do the stuff
}

例子

<!DOCTYPE html>
<html>
   <head>
      <title>Emberjs Helper Dependencies</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">
         {{outlet}}
      </script>

      <script type="text/x-handlebars" data-template-name="index">
         <ul>
            {{#each item in model}}
               <li>{{item}}</li>
               {{calls test this}}
            {{/each}}
         </ul>
      </script>

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

         App.IndexRoute = Ember.Route.extend({
            //creating the array.
            //any of the dependent key changes, the output will updated automatically
            model: function() {
               return ['Orange', 'Apple', 'Banana'];
            },
            actions:{
               test:function(){
                  document.write('test');
               }
            }
         });

         //register the handlebars that uses the calls as property name
         Ember.Handlebars.registerHelper('calls', function(property, options) {
            var action = Handlebars.Utils.escapeExpression(this, arguments);
            //listing the values using HTML tag
            return new Ember.Handlebars.SafeString("<li "+new Ember.Handlebars.SafeString(action)+">Adding Fruits</li>");
         });
      </script>
   </body>
</html>

输出

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

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

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

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

扫描二维码

下载编程狮App

公众号
微信公众号

编程狮公众号