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 ...
随机推荐
- HDU5842
Lweb and String Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)T ...
- Jcompress: 一款基于huffman编码和最小堆的压缩、解压缩小程序
前言 最近基于huffman编码和最小堆排序算法实现了一个压缩.解压缩的小程序.其源代码已经上传到github上面: Jcompress下载地址 .在本人的github上面有一个叫Utility的re ...
- ASP.NET Core MVC压缩样式、脚本及总是复制文件到输出目录
前言 在.NET Core之前对于压缩样式文件和脚本我们可能需要借助第三方工具来进行压缩,但在ASP.NET MVC Core中则无需借助第三方工具来完成,本节我们来看看ASP.NET Core MV ...
- js面试题-2
// 1.截取字符串 var aa = "abcd"; console.log(aa.substr(,)); var str = "qweda"; consol ...
- php 引入文件 include 和require
php 如何引用文件? 先建一个php 文件,php文件名要和所建的类名相同, 然后直接在php 中用include("")/include"" 和requir ...
- php解析
vim /usr/local/apache/conf/httpd.conf ##修改apache的网页配置文件 → 解析php文件 /usr/local/apache/bin/apache ...
- web前端面试题及答案
1.常用那几种浏览器测试?有哪些内核(Layout Engine)? 答: (Q1) 浏览器:IE,Chrome,FireFox,Safari,Opera. (Q2) 内核:Trident,Ge ...
- SharePoint 2016 文档库的新功能简介
今天,重装了一下SharePoint 2016,想多了解了解,看到一些自己平时没注意的功能,或者新的功能,分享一下给大家. 1.界面上操作的变换,多了一排按钮,更像SharePoint Online了 ...
- ASP.NET MVC5+EF6+EasyUI 后台管理系统(82)-Easyui Datagrid批量操作(编辑,删除,添加)
前言 有时候我们的后台系统表单比较复杂,做过进销存或者一些销售订单的都应该有过感觉 虽然Easyui Datagrid提供了行内编辑,但是不够灵活,但是我们稍微修改一下来达到批量编辑,批量删除,批量添 ...
- iphone在iframe页面的宽度不受父页面影响,避免撑开页面
工作中有个需求,就是产品页面通过iframe引用显示产品协议页,要求不要横向滑动,只需要竖向滑动,但在iphone中引用的iframe会撑开父页的宽度,而在android端浏览器这不会. <di ...