zoukankan      html  css  js  c++  java
  • Knockoutjs:Component and Custom Elements(翻译文章)

    Knockoutjs 的Components 是一种自定义的组件,它以一种强大、简介的方式将你自己的ui代码组织成一种单独的、可重用的模块,自定义的组件(Component)有以下特点:

    1.可以替代单独的widgit或者控制逻辑,或者你自己application的整个模块;
    2.包含自己的view,通常也包含了自己的viewModel(这个viewModel也可以不进行定义)
    3.可以预加载,可以通过AMD或者其他模块系统的方式根据需要异步加载
    4.可以接收参数,根据需要选择性的对这些参数进行修改并返回,或者调用回调函数
    5.组件可以进行组合,或者从别的组件进行继承
    6.能够进行打包,跨项目复用
    7.允许你定义自己的逻辑来进行js文件的配置和加载
    这种模式对大型应用来讲是非常有利的,因为这种自定义组件的模式,通过清晰的组织和封装简化了开发复杂度,通过根据需要增量式的加载你自己的应用代码和模板大大提高了运行时的性能。
     
    自定义元素:是一种使用自定义组件的非常便利的方式。你不必一定要用一对<div data-bind=""></div>套在你需要绑定的标签的外层来进行数据的绑定,而是用你自己描述的标记语言,比如<voting-button><product-editor>,这种形式,标签里的是你自定义的元素名称。Knockout在这一点上也兼容老版本的浏览器,IE6兼容。
     
    例子一:一个 like/dislike的小插件
    首先,你需要用来ko.components.register注册一个component。一个组件的定义需要有一个viewModel和一个template:
     
    1. ko.components.register('like-widget', {  
    2.     viewModel: function(params) {  
    3.         // Data: value is either null, 'like', or 'dislike'  
    4.         this.chosenValue = params.value;  
    5.            
    6.         // Behaviors  
    7.         this.like = function() { this.chosenValue('like'); }.bind(this);  
    8.         this.dislike = function() { this.chosenValue('dislike'); }.bind(this);  
    9.     },  
    10.     template:  
    11.         '<div class="like-or-dislike" data-bind="visible: !chosenValue()">  
    12.             <button data-bind="click: like">Like it</button>  
    13.             <button data-bind="click: dislike">Dislike it</button>  
    14.         </div>  
    15.         <div class="result" data-bind="visible: chosenValue">  
    16.             You <strong data-bind="text: chosenValue"></strong> it  
    17.         </div>'  
    18. });  
    通常情况下,你需要引入外部文件来加载viewModel和模板,而不是像上面这样写到同一个文件里。稍后我们讲解怎么以引入外部文件的方式加载viewModel和template。
    现在,通过进行组件绑定(component binding)或者自定义元素的方式在你的view(通常是html文档)里使用上面的自定义组件了。
    viewCode:
     
    1. <ul data-bind="foreach: products">  
    2.     <li class="product">  
    3.         <strong data-bind="text: name"></strong>  
    4.         <like-widget params="value: userRating"></like-widget>  
    5.     </li>  
    6. </ul>  
     
    viewModelCode:
     
    1. function Product(name, rating) {  
    2.     this.name = name;  
    3.     this.userRating = ko.observable(rating || null);  
    4. }  
    5.    
    6. function MyViewModel() {  
    7.     this.products = [  
    8.         new Product('Garlic bread'),  
    9.         new Product('Pain au chocolat'),  
    10.         new Product('Seagull spaghetti', 'like') // This one was already 'liked'  
    11.     ];  
    12. }  
    13.    
    14. ko.applyBindings(new MyViewModel());  

    在这个例子里面,组件通过Product的viewModel中的可监控属性:userRating来进行显示、编辑。
     
    例子二:从外部文件加载like/dislike组件
    在大多数的应用中,一般都会将组件的viewModel和模板放在外部文件中,如果你使用require.js这样的模块加载器来配置加载knockout来获取外部ADM模块的话,那么就可以通过bundle/minified的方式来进行预加载,或者按需增量加载。
    下面是一个使用require.js的配置示例:
     
    1. ko.components.register('like-or-dislike', {  
    2.     viewModel: { require: 'files/component-like-widget' },  
    3.     template: { require: 'text!files/component-like-widget.html' }  
    4. });  
    需要的文件:
    为了实现改组件(component)的功能,需要文件files/component-like-widget.js 和files/component-like-widget.html 。你是不是发现,这比直接在组件的定义里面包含viewModel和template要整洁、方便很多。
    files/component-like-widget.js code:
     
    1. define(['knockout'], function(ko) {  
    2.   
    3.     function LikeWidgetViewModel(params) {  
    4.         this.chosenValue = params.value;  
    5.     }  
    6.   
    7.     LikeWidgetViewModel.prototype.like = function() {  
    8.         this.chosenValue('like');  
    9.     };  
    10.   
    11.     LikeWidgetViewModel.prototype.dislike = function() {  
    12.         this.chosenValue('dislike');  
    13.     };  
    14.   
    15.     return LikeWidgetViewModel;  
    16.   
    17. });  
    files/component-like-widget.html code:

     

    1. <div class="like-or-dislike" data-bind="visible: !chosenValue()">  
    2.     <button data-bind="click: like">Like it</button>  
    3.     <button data-bind="click: dislike">Dislike it</button>  
    4. </div>  
    5.   
    6. <div class="result" data-bind="visible: chosenValue">  
    7.     You <strong data-bind="text: chosenValue"></strong> it.  
    8.     And this was loaded from an external file.  
    9. </div>  

    现在like-or-dislike插件可以像上面的例子一样使用,使用component binding的方式,或者自定义元素的方式。

    下面是源码:

    view :


     

    1. <ul data-bind="foreach: products">  
    2.     <li class="product">  
    3.         <strong data-bind="text: name"></strong>  
    4.         <like-or-dislike params="value: userRating"></like-or-dislike>  
    5.     </li>  
    6. </ul>  
    7. <button data-bind="click: addProduct">Add a product</button>  


    viewModel:

     

    1. function Product(name, rating) {  
    2.     this.name = name;  
    3.     this.userRating = ko.observable(rating || null);  
    4. }  
    5.    
    6. function MyViewModel() {  
    7.     this.products = ko.observableArray(); // Start empty  
    8. }  
    9.    
    10. MyViewModel.prototype.addProduct = function() {  
    11.     var name = 'Product ' + (this.products().length + 1);  
    12.     this.products.push(new Product(name));  
    13. };  
    14.    
    15. ko.applyBindings(new MyViewModel());  


    在你第一次点击“Add product” 按钮之前,打开浏览器的开发工具Network,你会发现组件的.js/.html文件第一次是按需加载的,加载之后就一直存在以备下次复用。

    Knockoutjs源出处:http://knockoutjs.com/documentation/component-overview.html

  • 相关阅读:
    2017.11.2 JavaWeb----第六章 Servlet技术
    2017.11.1 微型计算机原理与接口技术-----第七章 中断系统与8237A DMA控制器
    2017.10.31 微型计算机组成原理与接口技术------- 第六章 存储器
    2017.10.30 软件工程------ 软件测试
    选择排序
    快速排序(基础版)
    [经验]java 高级面试解析
    [经验]java 高级面试
    链表翻转的图文讲解(递归与迭代两种实现)yet
    手把手教你如何自定义DAO框架(重量级干货)(yet)
  • 原文地址:https://www.cnblogs.com/sonofelice/p/4493911.html
Copyright © 2011-2022 走看看