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>‘
- });
- <ul data-bind="foreach: products">
- <li class="product">
- <strong data-bind="text: name"></strong>
- <like-widget params="value: userRating"></like-widget>
- </li>
- </ul>
- 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());
在这个例子里面,组件通过Product的viewModel中的可监控属性:userRating来进行显示、编辑。
- 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;
- });
- <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
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。