Knockoutjs:Component and Custom Elements(翻译文章)
Knockoutjs 的Components 是一种自定义的组件,它以一种强大、简介的方式将你自己的ui代码组织成一种单独的、可重用的模块,自定义的组件(Component)有以下特点:
- ko.components.register('like-widget', {
- viewModel: function(params) {
- // Data: value is either null, 'like', or 'dislike'
- this.chosenValue = params.value;
- // Behaviors
- this.like = function() { this.chosenValue('like'); }.bind(this);
- this.dislike = function() { this.chosenValue('dislike'); }.bind(this);
- },
- template:
- '<div class="like-or-dislike" data-bind="visible: !chosenValue()">\
- <button data-bind="click: like">Like it</button>\
- <button data-bind="click: dislike">Dislike it</button>\
- </div>\
- <div class="result" data-bind="visible: chosenValue">\
- You <strong data-bind="text: chosenValue"></strong> it\
- </div>'
- });
通常情况下,你需要引入外部文件来加载viewModel和模板,而不是像上面这样写到同一个文件里。稍后我们讲解怎么以引入外部文件的方式加载viewModel和template。
- <ul data-bind="foreach: products">
- <li class="product">
- <strong data-bind="text: name"></strong>
- <like-widget params="value: userRating"></like-widget>
- </li>
- </ul>
viewModelCode:
- function Product(name, rating) {
- this.name = name;
- this.userRating = ko.observable(rating || null);
- }
- function MyViewModel() {
- this.products = [
- new Product('Garlic bread'),
- new Product('Pain au chocolat'),
- new Product('Seagull spaghetti', 'like') // This one was already 'liked'
- ];
- }
- ko.applyBindings(new MyViewModel());
- ko.components.register('like-or-dislike', {
- viewModel: { require: 'files/component-like-widget' },
- template: { require: 'text!files/component-like-widget.html' }
- });
需要的文件:
files/component-like-widget.js 和files/component-like-widget.html 。你是不是发现,这比直接在组件的定义里面包含viewModel和template要整洁、方便很多。files/component-like-widget.js code:
- define(['knockout'], function(ko) {
- function LikeWidgetViewModel(params) {
- this.chosenValue = params.value;
- }
- LikeWidgetViewModel.prototype.like = function() {
- this.chosenValue('like');
- };
- LikeWidgetViewModel.prototype.dislike = function() {
- this.chosenValue('dislike');
- };
- return LikeWidgetViewModel;
- });
files/component-like-widget.html code:
- <div class="like-or-dislike" data-bind="visible: !chosenValue()">
- <button data-bind="click: like">Like it</button>
- <button data-bind="click: dislike">Dislike it</button>
- </div>
- <div class="result" data-bind="visible: chosenValue">
- You <strong data-bind="text: chosenValue"></strong> it.
- And this was loaded from an external file.
- </div>
现在like-or-dislike插件可以像上面的例子一样使用,使用component binding的方式,或者自定义元素的方式。
下面是源码:
view :
- <ul data-bind="foreach: products">
- <li class="product">
- <strong data-bind="text: name"></strong>
- <like-or-dislike params="value: userRating"></like-or-dislike>
- </li>
- </ul>
- <button data-bind="click: addProduct">Add a product</button>
viewModel:
- function Product(name, rating) {
- this.name = name;
- this.userRating = ko.observable(rating || null);
- }
- function MyViewModel() {
- this.products = ko.observableArray(); // Start empty
- }
- MyViewModel.prototype.addProduct = function() {
- var name = 'Product ' + (this.products().length + 1);
- this.products.push(new Product(name));
- };
- ko.applyBindings(new MyViewModel());
在你第一次点击“Add product” 按钮之前,打开浏览器的开发工具Network,你会发现组件的.js/.html文件第一次是按需加载的,加载之后就一直存在以备下次复用。
Knockoutjs源出处:http://knockoutjs.com/documentation/component-overview.html
Knockoutjs:Component and Custom Elements(翻译文章)的更多相关文章
- Web Components之Custom Elements
什么是Web Component? Web Components 包含了多种不同的技术.你可以把Web Components当做是用一系列的Web技术创建的.可重用的用户界面组件的统称.Web Com ...
- window 属性:自定义元素(custom elements)
概述 Web Components 标准非常重要的一个特性是,它使开发者能够将HTML页面的功能封装为 custom elements(自定义标签),而往常,开发者不得不写一大堆冗长.深层嵌套的标 ...
- HTML Custom Elements (v1)
HTML Custom Elements (v1) https://developers.google.com/web/fundamentals/web-components/customelemen ...
- [大牛翻译系列]Hadoop 翻译文章索引
原书章节 原书章节题目 翻译文章序号 翻译文章题目 链接 4.1 Joining Hadoop(1) MapReduce 连接:重分区连接(Repartition join) http://www.c ...
- Java 破解谷歌翻译api,可以实现程序自动化翻译文章
1 原理:查看谷歌翻译网站,输入需要翻译的文字,选择语言得到翻译后的文字,发送异步请求参数返回结果.java使用httpclient发送请求,实现使用代码翻译文章的功能. 2 下载代码后,测试入口 ...
- 自定义元素(custom elements)
记录下自定义html自定义元素的相关心得: 浏览器将自定义元素保留在 DOM 之中,但不会任何语义.除此之外,自定义元素与标准元素都一致 事实上,浏览器提供了一个HTMLUnknownElement, ...
- [HTML5] Render Hello World Text with Custom Elements
Custom elements are fun technology. In this video, you will learn how to set one up and running in l ...
- 使用custom elements和Shadow DOM自定义标签
具体的api我就不写 官网上面多 如果不知道这个的话也可以搜索一下 目前感觉这个还是相当好用的直接上码. <!DOCTYPE html> <html lang="en&q ...
- HTML Custom Elements & valid name
HTML Custom Elements & valid name valid custom element name https://html.spec.whatwg.org/multipa ...
随机推荐
- 【Unity3d游戏开发】浅谈UGUI中的Canvas以及三种画布渲染模式
一.Canvas简介 Canvas画布是承载所有UI元素的区域.Canvas实际上是一个游戏对象上绑定了Canvas组件.所有的UI元素都必须是Canvas的自对象.如果场景中没有画布,那么我们创建任 ...
- C#进阶系列——使用Advanced Installer制作IIS安装包(二:配置安装包依赖项和自定义dll)
前言:上篇C#进阶系列——使用Advanced Installer制作IIS安装包(一:配置IIS和Web.config)介绍了下使用Advanced Installer配置IIS和Web.confi ...
- JAVA_file(1)
1.基本概念的理解 绝对路径:绝对路径就是你的主页上的文件或目录在硬盘上真正的路径,(URL和物理路径)例如:C:xyz est.txt 代表了test.txt文件的绝对路径.http://www.s ...
- Applovin Interview (面经)
职位:SDE Intern positon 地点: San Jose 轮电面:self introduction what's your interest Concept of "Concu ...
- Linux驱动技术(四) _异步通知技术
异步通知的全称是"信号驱动的异步IO",通过"信号"的方式,放期望获取的资源可用时,驱动会主动通知指定的应用程序,和应用层的"信号"相对应, ...
- Iterator——迭代接口
迭代对于JAVA的来说绝对不陌生.我们常常使用JDK提供的迭代接口进行Java集合的迭代. Iterator iterator = list.iterator(); while(iterator.ha ...
- ConnectString ()函数的介绍
ConnectString ()函数的介绍: connectstring 函数主要负责数据库的连接工作 Public Function ConnectString() As String ...
- 响应者链-----iOS
正文 以触摸事件为例,说一下响应者链 当发生触摸事件后,Runloop监听到事件,会将其事件打包成一个UIEvent事件,并放入当前活动UIApplication的事件队列中,再会传给UIWindow ...
- jQuery validata插件实现(每周一插件系列)
大家好,第一次写有点正规的博客,以前都是随手复制几下.为了打LOL,我写快点,代码我都复制在最下面了,并且写了大量的注释. 首先我写jquery插件,喜欢这么写(好处有很多,以后在讲,哈哈,看过jQu ...
- oracle 用系统用户以SYSDBA身份登陆
最近发现很多人问我 这么直接用系统OS用户 登陆 oracle : 1.首先通过用管理身份打开DOS命令窗口: 然后使用命令: sqlplus / as sysdba 即可: 其实这个命令和用sys用 ...