日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷解決方案
Knockout應(yīng)用開發(fā)指南之HelloWorld

本章展示的4個(gè)例子主要是利用了Knockout的基本語法特性,讓大家感受到使用Kncokout的快感。

10年積累的做網(wǎng)站、網(wǎng)站設(shè)計(jì)經(jīng)驗(yàn),可以快速應(yīng)對(duì)客戶對(duì)網(wǎng)站的新想法和需求。提供各種問題對(duì)應(yīng)的解決方案。讓選擇我們的客戶得到更好、更有力的網(wǎng)絡(luò)服務(wù)。我雖然不認(rèn)識(shí)你,你也不認(rèn)識(shí)我。但先制作網(wǎng)站后付款的網(wǎng)站建設(shè)流程,更有共青城免費(fèi)網(wǎng)站建設(shè)讓你可以放心的選擇與我們合作。

1 Hello world

這個(gè)例子里,2個(gè)輸入框都被綁定到data model上的observable變量上?!癴ull name”顯示的是一個(gè)dependent observable,它的值是前面2個(gè)輸入框的值合并一起的結(jié)果。

無論哪個(gè)輸入框更新,都會(huì)看到“full name” 顯示結(jié)果都會(huì)自動(dòng)更新。查看HTML源代碼可以看到我們不需要聲明onchange事件。Knockout知道什么時(shí)候該更新UI。

代碼: View

 
 
 
 
  1. First name: 

  2. Last name: 

  3. Hello,  !

代碼: View model

 
 
 
 
  1. // 這里是聲明的view model
  2. var viewModel = {
  3.     firstName: ko.observable("Planet"),
  4.     lastName: ko.observable("Earth")
  5. };
  6. viewModel.fullName = ko.dependentObservable(function () {
  7.     // Knockout tracks dependencies automatically. 
  8.     //It knows that fullName depends on firstName and lastName,           
  9.     //because these get called when evaluating fullName.
  10.     return viewModel.firstName() + " " + viewModel.lastName();
  11. });
  12. ko.applyBindings(viewModel); // This makes Knockout get to work

2 Click counter

這個(gè)例子展示的創(chuàng)建一個(gè)view model并且綁定各種綁定到HTML元素標(biāo)記上,以便展示和修改view model的狀態(tài)。

Knockout根據(jù)依賴項(xiàng)。在內(nèi)部,hasClickedTooManyTimes在numberOfClicks上有個(gè)訂閱,以便當(dāng)numberOfClicks改變的時(shí)候,強(qiáng)制hasClickedTooManyTimes重新執(zhí)行。相似的,UI界面上多個(gè)地方引用hasClickedTooManyTimes,所以當(dāng)hasClickedTooManyTimes 改變的時(shí)候,也講導(dǎo)致UI界面更新。

不需要手工聲明或者訂閱這些subscription訂閱,他們由KO框架自己創(chuàng)建和銷毀。參考如下代碼實(shí)現(xiàn):

代碼: View

 
 
 
 
  1. You've clicked   times
     
  2. Click me 
  3.     That's too many clicks! Please stop before you wear out your fingers.
  4.     Reset clicks

代碼: View model

 
 
 
 
  1. var clickCounterViewModel = function () {
  2.     this.numberOfClicks = ko.observable(0); 
  3.     this.registerClick = function () {
  4.         this.numberOfClicks(this.numberOfClicks() + 1);
  5.     } 
  6.     this.hasClickedTooManyTimes = ko.dependentObservable(function () {
  7.         return this.numberOfClicks() >= 3;
  8.     }, this);
  9. };
  10. ko.applyBindings(new clickCounterViewModel());

3 Simple list

這個(gè)例子展示的是綁定到數(shù)組上。

注意到,只有當(dāng)你在輸入框里輸入一些值的時(shí)候,Add按鈕才可用。參考下面的HTML代碼是如何使用enable 綁定。

代碼: View

 
 
 
 
  1.     New item:
  2.     
  3.      0">Add
  4.     

    Your items:

  5.      

代碼: View model

 
 
 
 
  1. var viewModel = {};
  2. viewModel.items = ko.observableArray(["Alpha", "Beta", "Gamma"]);
  3. viewModel.itemToAdd = ko.observable("");
  4. viewModel.addItem = function () {
  5.     if (viewModel.itemToAdd() != "") {
  6.         viewModel.items.push(viewModel.itemToAdd());
  7.         // Adds the item. Writing to the "items" observableArray causes any associated UI to update.
  8.         viewModel.itemToAdd("");                  
  9.         // Clears the text box, because it's bound to the "itemToAdd" observable
  10.     }
  11. }
  12. ko.applyBindings(viewModel);

4 Better list

這個(gè)例子是在上個(gè)例子的基礎(chǔ)上添加remove item功能(multi-selection)和排序功能。 “remove”和“sort”按鈕在不能用的時(shí)候會(huì)變成disabled狀態(tài)(例如,沒有足夠的item來排序)。

參考HTML代碼是如何實(shí)現(xiàn)這些功能的,另外這個(gè)例子也展示了如何使用匿名函數(shù)來實(shí)現(xiàn)排序。

代碼: View

 
 
 
 
  1.     Add item: 
  2.      0">Add
  3. Your values:

  4.  
  5.      0">Remove
  6.      1">Sort

代碼: View model

 
 
 
 
  1. // In this example, betterListModel is a class, and the view model is an instance of it.
  2. // See simpleList.html for an example of how to construct a view model without defining a class for it. Either technique works fine.
  3. var betterListModel = function () {
  4.     this.itemToAdd = new ko.observable("");
  5.     this.allItems = new ko.observableArray(["Fries", "Eggs Benedict", "Ham", "Cheese"]);
  6. // Initial items
  7. this.selectedItems = new ko.observableArray(["Ham"]);                               
  8. // Initial selection 
  9.     this.addItem = function () {
  10.         if ((this.itemToAdd() != "") && (this.allItems.indexOf(this.itemToAdd()) < 0))
  11.     // Prevent blanks and duplicates
  12.         this.allItems.push(this.itemToAdd());
  13.         this.itemToAdd(""); // Clear the text box
  14.     } 
  15.     this.removeSelected = function () {
  16.         this.allItems.removeAll(this.selectedItems());
  17.         this.selectedItems([]); // Clear selection
  18.     }
  19. };
  20. ko.applyBindings(new betterListModel());

原文:http://www.cnblogs.com/TomXu/archive/2011/11/30/2257067.html

【系列文章】

  1. 用JavaScript評(píng)估用戶輸入密碼的強(qiáng)度(Knockout版)
  2. Knockout應(yīng)用開發(fā)指南之創(chuàng)建自定義綁定
  3. Knockout應(yīng)用開發(fā)指南之模板綁定
  4. Knockout應(yīng)用開發(fā)指南之綁定語法
  5. Knockout應(yīng)用開發(fā)指南之監(jiān)控屬性(Observables)
  6. Knockout應(yīng)用開發(fā)指南之入門介紹

名稱欄目:Knockout應(yīng)用開發(fā)指南之HelloWorld
網(wǎng)站地址:http://m.5511xx.com/article/coeejod.html