knockout學習筆記10:demo

來源:互聯網
上載者:User

標籤:

  前面已經介紹了ko的基本用法,結合官方文檔,基本就可以實際應用了。本章作為ko學習的最後一篇,實現一個簡單的demo。主要集中在ko,所以後台資料都是靜態。類似於博園,有一個個人文章的分類列表,一個文章列表。可以在文章最後下載工程原始碼(包括之前demo的代碼)。實現如下:

一、準備資料

  分類資訊Kind:

    public class Kind    {        public string Url { get; set; }        public string Name { get; set; }        public int Count { get; set; }            }

  文章資訊Essay:

    public class Essay    {        public string Url { get; set; }        public string Name { get; set; }        public short Status { get; set; }        public int Comment { get; set; }        public int PageView { get; set; }        public int RssView { get; set; }        public string Date { get; set; }    }

二、準備模板

  主要html:

<div id="container">        <div id="head">部落格園</div>        <div id="content">            <div id="kindField">                <div id="kindHeader">分 類</div>                <ul id="kindList" data-bind="template:{name:‘kindTmpl‘,foreach:kindList}"></ul>            </div>            <div id="essayField">                <div id="essayHeader">隨筆</div>                <table id="essayList" cellpadding="0" cellspacing="0">                    <thead>                        <tr>                            <td>標題</td><td class="w40 tc">發布<br />狀態</td><td class="w40 tc">評論</td><td class="w40 tc">頁面<br />瀏覽</td><td class="w40 tc">RSS<br />閱讀</td><td class="w40 tc">操作</td><td class="w40 tc">操作</td>                        </tr>                    </thead>                    <tbody data-bind="template:{name:‘essayTmpl‘,foreach:essayList}"></tbody>                                                        </table>            </div>        </div>            </div>

  分類模板和文章模板:

<script type="text/tmpl" id="kindTmpl">    <li><a data-bind="attr:{href:Url}"><span data-bind="text:Name"></span>(<span data-bind="text:Count"></span>)</a></li></script><script type="text/tmpl" id="essayTmpl">    <tr>        <td>            <a data-bind="attr:{href:Url,title:Name}">                <span data-bind="text:Name"></span>(<span data-bind="text:Date"></span>) <span data-bind="ifnot:Status">[草稿箱]</span>            </a>        </td>        <td class="tc" data-bind="text:$parent.getStatus($data.Status)"></td>        <td class="tc" data-bind="text:Comment"></td>        <td class="tc" data-bind="text:PageView"></td>        <td class="tc" data-bind="text:RssView"></td>        <td class="tc"><a class="action" data-bind="click:$parent.edit">編輯</a></td>        <td class="tc"><a class="action" data-bind="click:function(){$parent.del($element);}">刪除</a></td>    </tr></script>

三、資料繫結

  前台定義model和viewmodel,然後通過ajax擷取資料,完成綁定。

    function Kind(){        this.Url = "";        this.Name = "";        this.Count = "";        this.getKind = function(){            $.getJSON("../Handlers/GetKind.ashx",function(data){                kindVM.kindList(data);            })        }    }    function KindList(){        this.kindList = ko.observable([]);    }    function Essay(){        this.Url = "";        this.Name = "";        this.Status = 0;        this.Comment = 0;        this.PageView = 0;        this.RssView = 0;        this.Date = "";        this.getEssay = function(){            $.getJSON("../Handlers/GetEssay.ashx",function(data){                        essayVM.essayList(data);                    })        }    }    function EssayList(){        this.essayList = ko.observableArray([]);        this.edit = function(essay){            alert("編輯:" + essay.Url);        }        this.del = function(dom){                  var jTr = $(dom).parents("tr");            jTr.replaceWith("<tr><td style=‘color:red;border:none;‘>刪除成功!</td></tr>");                    }        this.getStatus = function(status){            if(status === 0){                return "未發布";            }            return "發布";        }    }    var kind = new Kind();    var kindVM = new KindList();    var essay = new Essay();    var essayVM = new EssayList();        ko.applyBindings(kindVM,document.getElementById("kindField"));    ko.applyBindings(essayVM,document.getElementById("essayField"));                   kind.getKind();    essay.getEssay();

 

源碼下載

knockout學習筆記10:demo

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.