Knockout自定義綁定創(chuàng)建方法
概述
除了上一篇列出的KO內(nèi)置的綁定類型(如value、text等),你也可以創(chuàng)建自定義綁定。
注冊你的binding handler
ko.bindingHandlers.yourBindingName = { init: function(element, valueAccessor, allBindings, viewModel, bindingContext) { // This will be called when the binding is first applied to an element // Set up any initial state, event handlers, etc. here }, update: function(element, valueAccessor, allBindings, viewModel, bindingContext) { // This will be called once when the binding is first applied to an element, // and again whenever any observables/computeds that are accessed change // Update the DOM element based on the supplied values here. } };
接下來你就可以在任意dom元素上使用的自定義綁定了:
<div data-bind="yourBindingName: someValue"> </div>
注意:你不必在你的handler里把init和update的callback都提供,可以提供任意一個。
update callback
顧名思義,當你的監(jiān)控屬性observable更新的時候,ko會自動調(diào)用你的update回調(diào)。
它有以下參數(shù):
element:使用這個綁定的dom元素;
valueAccessor : 通過調(diào)用valueAccessor()可以獲得當前綁定的model屬性值,調(diào)用ko.unwrap(valueAccessor())能夠更方便的獲取observable的值和普通值;
allBindings : 綁定到這個dom元素上的model的所有屬性值,例如調(diào)用callBindings.get('name') 返回綁定的name屬性值(不存在返回undefined),或者調(diào)用allBindings.has('name')判斷name是否綁定到了當前的dom中;
viewModel : 在Knockout.3x中以棄用,可用bindingContext.$data或者bindingContext.$rawData來獲取當前的viewModel;
bindingContext : 綁定上下文,可調(diào)用bindingContext.$data、 bindingContext.$parent, bindingContext.$parents等獲取數(shù)據(jù);
接下來看一個例子,你也許希望使用visible綁定來控制元素的可見性,并且加上動畫效果,這時你可以創(chuàng)建你的自定義綁定:
ko.bindingHandlers.slideVisible = { update: function(element, valueAccessor, allBindings) { // First get the latest data that we're bound to var value = valueAccessor(); // Next, whether or not the supplied model property is observable, get its current value var valueUnwrapped = ko.unwrap(value); // Grab some more data from another binding property var duration = allBindings.get('slideDuration') || 400; // 400ms is default duration unless otherwise specified // Now manipulate the DOM element if (valueUnwrapped == true) $(element).slideDown(duration); // Make the element visible else $(element).slideUp(duration); // Make the element invisible } };
然后你可以這樣使用這個自定義綁定:
<div data-bind="slideVisible: giftWrap, slideDuration:600">You have selected the option</div> <label><input type="checkbox" data-bind="checked: giftWrap" /> Gift wrap</label> <script type="text/javascript"> var viewModel = { giftWrap: ko.observable(true) }; ko.applyBindings(viewModel); </script>
init callback
ko將為每個使用綁定的dom元素調(diào)用你的init函數(shù),它有兩個主要用途:
(1)為dom元素設(shè)置初始化狀態(tài);
(2)注冊一些事件處理程序,例如:當用戶點擊或者修改dom元素時,你可以改變監(jiān)控屬性的狀態(tài);
ko將使用和update回調(diào)完全相同一組參數(shù)。
繼續(xù)前面的例子,你也許想讓slideVisible在頁面第一次顯示的時候就設(shè)置該元素的可見性狀態(tài)(沒有任何動畫效果),而動畫效果是在以后改變的時候執(zhí)行,你可以按照下面的方式來做:
ko.bindingHandlers.slideVisible = { init: function(element, valueAccessor) { var value = ko.unwrap(valueAccessor()); // Get the current value of the current property we're bound to $(element).toggle(value); // jQuery will hide/show the element depending on whether "value" or true or false }, update: function(element, valueAccessor, allBindings) { // Leave as before } };
giftWrap被初始化定義為false(ko.observable(false)),關(guān)聯(lián)的DIV會在初始化的時候隱藏,之后用戶點擊checkbox時才讓DIV顯示。
你現(xiàn)在已經(jīng)知道如何使用update回調(diào)了,當observable值改變的時候你可以更新dom元素。我們現(xiàn)在可以用另外的方法來做,比如當用戶有某個action操作時,也能引起你的observable值更新,例如:
ko.bindingHandlers.hasFocus = { init: function(element, valueAccessor) { $(element).focus(function() { var value = valueAccessor(); value(true); }); $(element).blur(function() { var value = valueAccessor(); value(false); }); }, update: function(element, valueAccessor) { var value = valueAccessor(); if (ko.unwrap(value)) element.focus(); else element.blur(); } };
現(xiàn)在你可以通過元素的“focusedness”綁定來讀寫你的observable值了。
<p>Name: <input data-bind="hasFocus: editingName" /></p> <!-- Showing that we can both read and write the focus state --> <div data-bind="visible: editingName">You're editing the name</div> <button data-bind="enable: !editingName(), click:function() { editingName(true) }">Edit name</button> <script type="text/javascript"> var viewModel = { editingName: ko.observable() }; ko.applyBindings(viewModel); </script>
以上內(nèi)容是小編給大家分享的Knockout自定義綁定創(chuàng)建方法,希望大家喜歡。
相關(guān)文章
JavaScript實現(xiàn)99乘法表及隔行變色實例代碼
最近做了個項目是要求實現(xiàn)99乘法表隔行變色,本文給大家分享通過多種方式實現(xiàn)js 99 乘法表,感興趣的朋友一起看看吧2016-02-02JavaScript數(shù)組特性與實踐應(yīng)用深入詳解
這篇文章主要介紹了JavaScript數(shù)組特性與實踐應(yīng)用,較為深入而詳細的分析了javascript數(shù)組的功能、屬性、使用方法及操作注意事項,需要的朋友可以參考下2018-12-12JavaScript中你不得不知道的Promise高級用法分享
在JavaScript中,Promise是一種解決異步編程問題的重要方式,本文主要來和大家探討一下23個Promise的高級用法,每一個都在JavaScript的海洋中航行,讓開發(fā)者們能夠以更高效、優(yōu)雅的方式處理異步操作,希望對大就有所幫助2023-12-12TypeScript 安裝使用及基本數(shù)據(jù)類型
這篇文章主要介紹了TypeScript 安裝使用及基本數(shù)據(jù)類型,本文分步驟給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-03-03關(guān)于實現(xiàn)代碼語法標亮 dp.SyntaxHighlighter
關(guān)于實現(xiàn)代碼語法標亮 dp.SyntaxHighlighter...2007-02-02小程序開發(fā)實現(xiàn)access_token統(tǒng)一管理
本文主要介紹了小程序開發(fā)實現(xiàn)access_token統(tǒng)一管理,文中通過示例代碼介紹的非常詳細,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-07-07