EmberJS 模板助手依賴關(guān)系

2018-01-04 12:54 更新

描述

如果要渲染屬性,則需要通過更改其中一個屬性來更新輸出。當(dāng)任何從屬密鑰更改時,輸出將自動更新。

語句

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>

輸出

讓我們執(zhí)行以下步驟,看看上面的代碼如何工作:

  • 將上面的代碼保存在tmp_writing_helper_dep.html文件中

  • 在瀏覽器中打開此HTML文件。

以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號