在ASP.NET MVC中使用Knockout實踐03,巧用data參數

來源:互聯網
上載者:User

標籤:style   blog   http   io   color   os   ar   使用   java   

使用Knockout,當通過建構函式建立View Model的時候,建構函式的參數個數很可能是不確定的,於是就有了這樣的一個解決方案:向建構函式傳遞一個object類型的參數data。

 

<input data-bind="value: name"/><hr/>
<select data-bind="options: categories, value: category" ></select><hr/>
@section scripts
{
    <script src="~/Scripts/knockout-2.2.0.js"></script>
    <script type="text/javascript">
        $(function() {         
            $.getJSON(‘@Url.Action("GetFirstProduct","Home")‘, function (data) {
                product.name(data.Name);
                product.category (data.Category);
            });
        });
        var categories = ["小說", "散文", "傳記"];
        var Product = function (data) {
            data = data || {};
            this.name = ko.observable();
            this.category = ko.observable();
            this.categories = categories;
            this.initialize(data);
        };
        ko.utils.extend(Product.prototype, {
            initialize: function(data) {
                this.name(data.name);
                this.category(data.category);
            }
        });
        var product = new Product({
            name: "預設值",
            category: "傳記"
        });
        //綁定
        ko.applyBindings(product);
    </script>
}

以上,通過建構函式建立Product的時候,只用到了一個參數,data。在執行個體化Product的時候,給Product各個成員賦預設值。另外,還為Product的原型添加了一個擴充方法,用於初始化。

 

當頁面載入完畢,向控制器發送非同步請求,返回給Product的值不是初始值。

 

如果想返回到Product初始值的狀態,如何做到呢?

 

可以分3步完成:
1、給Product增加一個屬性origionData,用來存放初始狀態
2、給Product的原型添加一個擴充方法,用來返回到初始狀態,即把origionData屬性值作為初始化方法的參數
3、在頁面上添加一個按鈕,綁定Product的擴充方法

 

 

<input data-bind="value: name"/><hr/>
<select data-bind="options: categories, value: category" ></select><hr/>
<button data-bind="click:revert">返回初始狀態</button>
@section scripts
{
    <script src="~/Scripts/knockout-2.2.0.js"></script>
    <script type="text/javascript">
        $(function() {         
            $.getJSON(‘@Url.Action("GetFirstProduct","Home")‘, function (data) {
                product.name(data.Name);
                product.category (data.Category);
            });
        });
        var categories = ["小說", "散文", "傳記"];
        var Product = function (data) {
            data = data || {};
            this.name = ko.observable();
            this.category = ko.observable();
            this.categories = categories;
            this.origionData = data;
            this.initialize(data);
        };
        ko.utils.extend(Product.prototype, {
            initialize: function(data) {
                this.name(data.name);
                this.category(data.category);
            },
            revert: function() {
                this.initialize(this.origionData);
            }
        });
        var product = new Product({
            name: "預設值",
            category: "傳記"
        });
        //綁定
        ko.applyBindings(product);
    </script>
}


點擊"返回初始狀態"按鈕,回到Product的初始狀態。

 

總結:使用建構函式建立View Model,當建構函式的參數數量不確定的情況下,可考慮使用data作為參數。

在ASP.NET MVC中使用Knockout實踐03,巧用data參數

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.