EmberJS 自定义视图的元素

2018-01-03 16:11 更新

自定义视图的元素

您还可以通过在Ember.View中设置tagName,classNames和classNameBindings属性来自定义视图的元素。

   Ember.View.extend({
      className:'NameofClass'
      classNameBindings:'AttributeName'
      tagName:'NameofTag'
      attributeBindings:'AttributeName'
      attrvalue:'AttributeValue'
   });

例子

<!DOCTYPE html>
<html>
   <head>
      <title>Emberjs Customizing a View's Element</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 "custom"}}
      </script>

      <script type="text/x-handlebars" data-template-name="custom">
         <h1>Customizing the view</h1>
         <!-- using font tag by customizing its attribute in a sView -->
         <font>I am a Custom View my color is blue</font>
      </script>

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

         // Customizing a view's elements:
         App.CustomView = Ember.View.extend({
            //defining name of the template as 'custom'
            templateName: 'custom',
            tagName: 'font',
            //binding the 'color' attribute to the 'font' tag
            attributeBindings:['color'],
            //defining value for 'color' attribute
            'color': "blue"
         });
      </script>
   </body>
</html>

输出

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

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

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

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

扫描二维码

下载编程狮App

公众号
微信公众号

编程狮公众号