KnockoutJS 3.X API 第四章之?dāng)?shù)據(jù)控制流component綁定
一個(gè)例子

UI源碼:
<h4>First instance, without parameters</h4>
<div data-bind='component: "message-editor"'></div>
<h4>Second instance, passing parameters</h4>
<div data-bind='component: {
name: "message-editor",
params: { initialText: "Hello, world!" }
}'></div>
視圖模型源碼:
ko.components.register('message-editor', {
viewModel: function(params) {
this.text = ko.observable(params && params.initialText || '');
},
template: 'Message: <input data-bind="value: text" /> '
+ '(length: <span data-bind="text: text().length"></span>)'
});
ko.applyBindings();
這只是一個(gè)非常簡(jiǎn)單的例子,在開(kāi)發(fā)中,一般都是將View Model和Template寫(xiě)成單獨(dú)外部文件,然后通過(guò)ko的components.register方法注冊(cè)他們,在以后的KO高級(jí)應(yīng)用系列中將會(huì)進(jìn)一步講解。
API
使用component綁定有兩種綁定語(yǔ)法
1. 快速語(yǔ)法:
只傳遞一個(gè)字符串作為組件名稱,比提供任何參數(shù)。例如:
<div data-bind='component: "my-component"'></div>
如果你覺(jué)得這種寫(xiě)法有些死板的話,也可以傳遞一個(gè)監(jiān)控屬性,用其值作為組件名稱。待以后組件名變化的時(shí)候,直接修改監(jiān)控屬性值即可:
<div data-bind='component: observableWhoseValueIsAComponentName'></div>
2.完整語(yǔ)法:
提供完整的組件參數(shù),參數(shù)如下:
name - 注入組件的名稱??墒褂米址蚴潜O(jiān)控屬性。
params - 一組參數(shù)對(duì)象。通常,這是一個(gè)包含多個(gè)參數(shù)的鍵值對(duì)。
例如:
<div data-bind='component: {
name: "shopping-cart",
params: { mode: "detailed-list", items: productsList }
}'></div>
備注1:僅模板式的component
通常的component綁定具有ViewModel和Template,但是這并不是必須的,有些時(shí)候一個(gè)component可能只包含一個(gè)Template。例如:
ko.components.register('special-offer', {
template: '<div class="offer-box" data-bind="text: productName"></div>'
});
可以使用注入的方式,將視圖模型注入給Template:
<div data-bind='component: {
name: "special-offer-callout",
params: { productName: someProduct.name }
}'></div>
在或者使用客戶元素(以后的高級(jí)章節(jié)講解)進(jìn)行注入視圖模型。
<special-offer params='productName: someProduct.name'></special-offer>
如上例子,HTML標(biāo)記為模板名稱,其屬性params中注入視圖模型。
備注2:component虛擬綁定
如同之前章節(jié)的虛擬綁定一樣,同樣是使用<!-- ko -->和<!-- /ko -->這種方式實(shí)現(xiàn)虛擬綁定,來(lái)達(dá)到不更改DOM元素的目的
<!-- ko component: "message-editor" --> <!-- /ko -->
傳參的例子:
<!-- ko component: {
name: "message-editor",
params: { initialText: "Hello, world!", otherParam: 123 }
} -->
<!-- /ko -->
備注3:傳遞標(biāo)記到component綁定
<div data-bind="component: { name: 'my-special-list', params: { items: someArrayOfPeople } }">
<!-- Look, here's some arbitrary markup. By default it gets stripped out
and is replaced by the component output. -->
The person <em data-bind="text: name"></em>
is <em data-bind="text: age"></em> years old.
</div>
如上例子中,既有component綁定,也有一些DOM元素,當(dāng)綁定后,my-special-list組件將會(huì)和這些DOM元素組成一個(gè)新的UI界面。在未來(lái)高級(jí)章節(jié)中,我們將會(huì)提到一個(gè)帶有DOM標(biāo)記的自定義companent綁定的例子。盡情期待。先賣個(gè)關(guān)子~。
內(nèi)存管理(了解即可,切勿鉆牛角尖)
您的視圖模型類可能有一個(gè)dispose函數(shù)。如果得以運(yùn)行,KO將調(diào)用這個(gè)函數(shù)在內(nèi)存中刪除組件,并從DOM中刪除。
在一下情況,您必須使用dispose以釋放垃圾收回資源。例如:
setInterval 回調(diào)后,需要明確清除。
使用clearInterval(handle)去清除他們,否則視圖模型在內(nèi)存常駐。
ko.computed 回調(diào)后,直到明確設(shè)置成從它們的依賴接收通知。
如果一個(gè)依賴關(guān)系是外部的對(duì)象,那么一定要使用.dispose()來(lái)釋放計(jì)算監(jiān)控屬性,否則(也可能你的視圖模型)將在內(nèi)存常駐。另外,可以考慮使用一個(gè)pureComputed,以避免人工處理的需求。
Subscriptions 回掉后,需要明確清除。
如果您已經(jīng)預(yù)訂到外部觀察時(shí),一定要使用.dispose(),否則回調(diào)(也可能您的視圖模型)將在內(nèi)存中常駐。
例如:
var someExternalObservable = ko.observable(123);
function SomeComponentViewModel() {
this.myComputed = ko.computed(function() {
return someExternalObservable() + 1;
}, this);
this.myPureComputed = ko.pureComputed(function() {
return someExternalObservable() + 2;
}, this);
this.mySubscription = someExternalObservable.subscribe(function(val) {
console.log('The external observable changed to ' + val);
}, this);
this.myIntervalHandle = window.setInterval(function() {
console.log('Another second passed, and the component is still alive.');
}, 1000);
}
SomeComponentViewModel.prototype.dispose = function() {
this.myComputed.dispose();
this.mySubscription.dispose();
window.clearInterval(this.myIntervalHandle);
// this.myPureComputed doesn't need to be manually disposed.
}
ko.components.register('your-component-name', {
viewModel: SomeComponentViewModel,
template: 'some template'
});
以上所述是小編給大家介紹的KnockoutJS 3.X API 第四章之?dāng)?shù)據(jù)控制流component綁定,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
- KnockoutJS 3.X API 第四章之?dāng)?shù)據(jù)控制流foreach綁定
- KnockoutJS 3.X API 第四章之?dāng)?shù)據(jù)控制流if綁定和ifnot綁定
- KnockoutJS 3.X API 第四章之?dāng)?shù)據(jù)控制流with綁定
- KnockoutJS 3.X API 第四章之click綁定
- KnockoutJS 3.X API 第四章之事件event綁定
- KnockoutJS 3.X API 第四章之表單submit、enable、disable綁定
- KnockoutJS 3.X API 第四章之表單value綁定
- BootstrapTable與KnockoutJS相結(jié)合實(shí)現(xiàn)增刪改查功能【二】
- BootstrapTable與KnockoutJS相結(jié)合實(shí)現(xiàn)增刪改查功能【一】
- KnockoutJS 3.X API 第四章之表單textInput、hasFocus、checked綁定
相關(guān)文章
js實(shí)現(xiàn)簡(jiǎn)單的隨機(jī)點(diǎn)名器
這篇文章主要為大家詳細(xì)介紹了js實(shí)現(xiàn)簡(jiǎn)單的隨機(jī)點(diǎn)名器,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-09-09
javascript 動(dòng)態(tài)改變層的Z-INDEX的代碼style.zIndex
javascript 動(dòng)態(tài)改變層的Z-INDEX的代碼style.zIndex...2007-08-08
微信小程序如何在頁(yè)面跳轉(zhuǎn)時(shí)進(jìn)行頁(yè)面導(dǎo)航
小程序能夠在不同的頁(yè)面進(jìn)行跳轉(zhuǎn)切換,路由起到了至關(guān)重要的作用,下面這篇文章主要給大家介紹了關(guān)于微信小程序如何在頁(yè)面跳轉(zhuǎn)時(shí)進(jìn)行頁(yè)面導(dǎo)航的相關(guān)資料,需要的朋友可以參考下2022-09-09
使用JavaScript實(shí)現(xiàn)Java的List功能(實(shí)例講解)
使用JavaScript實(shí)現(xiàn)Java的List功能(實(shí)例講解)。需要的朋友可以過(guò)來(lái)參考下,希望對(duì)大家有所幫助2013-11-11
微信小程序 SOTER 生物認(rèn)證DEMO 指紋識(shí)別功能
這篇文章主要介紹了微信小程序 SOTER 生物認(rèn)證DEMO指紋識(shí)別功能,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-12-12
JavaScript算法教程之sku(庫(kù)存量單位)詳解
這篇文章主要給大家介紹了JavaScript算法教程之sku(庫(kù)存量單位)的相關(guān)資料,文中介紹的非常詳細(xì),對(duì)大家具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面跟著小編一起來(lái)學(xué)習(xí)學(xué)習(xí)吧。2017-06-06
arguments對(duì)象驗(yàn)證函數(shù)的參數(shù)是否合法
Javascript函數(shù)中的參數(shù)對(duì)象arguments是個(gè)對(duì)象,而不是數(shù)組。但它可以類似數(shù)組那樣通過(guò)數(shù)字下表訪問(wèn)其中的元素,而且它也有l(wèi)ength屬性標(biāo)識(shí)它的元素的個(gè)數(shù)。2015-06-06

