本篇体验使用ko.computed(fn)计算、组合View Model成员、Select元素的绑定、使用构造器创建View Model、通过View Model的原型(Prototype)为View Model添加扩展方法。

□ 使用ko.computed(fn)计算成员

有时候,我们希望把View Model中的几个成员组合起来成为一个新成员,使用ko.computed(fn)可实现。

接着上一篇,为productViewModel这个json对象增加一个计算成员。

<div data-bind="text:formatted"></div> <hr/>
<input type="text" data-bind="value:name"/>

@section scripts
{
    <script src="~/Scripts/knockout-2.2.0.js"></script>
    <script type="text/javascript">
        $(function() {

            $.getJSON('@Url.Action("GetFirstProduct","Home")', function (data) {
                productViewModel.name(data.Name);
                productViewModel.category(data.Category);
            });
        });

        var productViewModel = {
            id: ko.observable(""),
            name: ko.observable("初始值"),
            price: ko.observable(""),
            category: ko.observable("")
        };

        //为productViewModel增加一个计算成员
        productViewModel.formatted = ko.computed(function() {
            return productViewModel.name() + "--" + productViewModel.category();
        });

        //绑定
        ko.applyBindings(productViewModel);
    </script>


以上,对于在View Model中的成员,如果已经被赋予observable后,必须通过类似name()的方式获取成员值。

□ Select元素的绑定

对于Select元素,它的options属性应该绑定一个数组,它的value属性绑定一个选中值。

<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) {
                productViewModel.name(data.Name);
                productViewModel.category(data.Category);
            });
        });

        var productViewModel = {
            id: ko.observable(""),
            name: ko.observable("初始值"),
            price: ko.observable(""),
            category: ko.observable(""),
            categories: ["小说","散文","传记"]
        };

        //为productViewModel增加一个计算成员
        productViewModel.formatted = ko.computed(function() {
            return productViewModel.name() + "--" + productViewModel.category();
        });

        //绑定
        ko.applyBindings(productViewModel);
    </script>
}


□ 使用构造器创建View Model

<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 Product = function(name, category, categories) {
            this.name = ko.observable(name);
            this.category = ko.observable(category);
            this.categories = categories;

            this.formatted = ko.computed(function() {
                return this.name() + "--" + this.category();
            }, this);
        };

        var product = new Product("默认值", "默认值", ["小说", "散文", "传记"]);

        //绑定
        ko.applyBindings(product);
    </script>
}


□ 为View Model原型(Prototype)扩展方法

以上,ko.computed中使用了一个匿名函数,如果把这个匿名函数作为Product的扩展方法,该如何做到呢?

<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 Product = function(name, category, categories) {
            this.name = ko.observable(name);
            this.category = ko.observable(category);
            this.categories = categories;

            this.formatted = ko.computed(this.getFormatted, this);
        };



        ko.utils.extend(Product.prototype, {
            getFormatted: function() {
                return this.name() + "--" + this.category();
            }
        });

        var product = new Product("默认值", "默认值", ["小说", "散文", "传记"]);

        //绑定
        ko.applyBindings(product);
    </script>
}

以上,通过ko.utils.extend方法,为Product的原型添加了扩展方法getFormatted。

在ASP.NET MVC中使用Knockout实践02,组合View Model成员、Select绑定、通过构造器创建View Model,扩展View Model方法的更多相关文章

  1. 在ASP.NET MVC中使用Knockout实践01,绑定Json对象

    本篇体验在ASP.NET MVC下使用Knockout,将使用EF Code First创建数据库.最后让Knockout绑定一个Json对象. 创建一个领域模型. namespace MvcAppl ...

  2. 在ASP.NET MVC中使用Knockout实践08,使用foreach绑定集合

    本篇体验使用 foreach 绑定一个Product集合. 首先使用构造创建一个View Model. var Product = function(data) { this.name = ko.ob ...

  3. 在ASP.NET MVC中使用Knockout实践05,基本验证

    本篇体验View Model验证.Knockout的subscribe方法能为View Model成员注册验证规则. @{ ViewBag.Title = "Index"; Lay ...

  4. 在ASP.NET MVC中使用Knockout实践09,自定义绑定

    Knockout真正强大之处在于绑定机制,通过data-bind属性值体现绑定,不仅可以绑定值,还可以绑定事件,甚至可以自定义绑定. 从一个例子看Knockou的绑定机制 假设想给一个button元素 ...

  5. 在ASP.NET MVC中使用Knockout实践07,自定义验证信息的位置与内容

    在前两篇中,体验了Knockout的基本验证和自定义验证.本篇自定义验证信息的显示位置与内容. 自定义验证信息的显示位置 通常,Knockout的验证信息紧跟在input后面,通过validation ...

  6. 在ASP.NET MVC中使用Knockout实践06,自定义验证、异步验证

    在上一篇中体验了Knockout.Validation的基本验证,本篇体验自定义验证和异步验证. 自定义验证规则 ko.validation有一个rules属性,专门用来存放验证规则,它是一个键值对集 ...

  7. 在ASP.NET MVC中使用Knockout实践04,控制View Model的json格式内容

    通常,需要把View Model转换成json格式传给服务端.但在很多情况下,View Model既会包含字段,还会包含方法,我们只希望把字段相关的键值对传给服务端. 先把上一篇的Product转换成 ...

  8. 在ASP.NET MVC中使用Knockout实践03,巧用data参数

    使用Knockout,当通过构造函数创建View Model的时候,构造函数的参数个数很可能是不确定的,于是就有了这样的一个解决方案:向构造函数传递一个object类型的参数data. <inp ...

  9. 关于ASP.NET MVC中Response.Redirect和RedirectToAction的BUG (跳转后继续执行后面代码而不结束进程)以及处理方法

    关于ASP.NET MVC中Response.Redirect和RedirectToAction的BUG (跳转后继续执行后面代码而不结束进程)以及处理方法   在传统的ASP.NET中,使用Resp ...

随机推荐

  1. Linux获取/dev/input目录下的event对应的设备【转】

    转自:https://blog.csdn.net/qq_21792169/article/details/51458855 当我们在Linux操作系统下使用input子系统时,当我们先插鼠标,在插上摄 ...

  2. Vmware中Linux或macOS客户端如何回收硬盘空间

    Vmware对于Windows的客户端,使用GUI操作硬盘回收和整理磁盘即可.对于Linux或macOS客户端,需要在安装Vmware Tools之后,在客户端OS的终端Terminal里输入命令进行 ...

  3. 【前端vue开发】vue开发输入姓名,电话,公司表单提交组件

    <template> <div id="parti-info"> <div> <span>您的姓名:</span> &l ...

  4. 解决华为手机无法输出Debug级别log的问题

    近期购入了新款的华为手机荣耀8,手感.性能.颜值都非常好.作为android开发工程师,自然会用到真机进行日常的调试.然而,这部手机并没有这么“听话“!反复尝试开启开发者选项中的设置项,依旧无法输出L ...

  5. SNMP中MIB2所有主要节点

    系统组:system组包含以下对象集(.1.3.6.1.2.1.1): 对象名:sysDescr(1) OID:system.1 对象类型:DisplayString[255] 访问模式:只读 描述: ...

  6. 掩膜 rcnn

    更多讨论,参考知乎:https://www.zhihu.com/question/57403701

  7. java adapter(适配器)惯用方法

    如果现在有一个Iterable类,你想要添加一种或多种在foreach语句中使用这个类的方法,例如方向迭代,应该怎么做呢? 如果之间继承这个类,并且覆盖iterator()方法,你只能替换现有的方法, ...

  8. 【转】MySQL安全配置介绍

    一.前言 很多文章中会说,数据库的权限按最小权限为原则,这句话本身没有错,但是却是一句空话.因为最小权限,这个东西太抽象,很多时候你并弄不清楚具体他需要哪些权限. 现在很多mysql用着root账户在 ...

  9. WebStrom配置node.js

    Webstrom的注册码: WebStorm 7.0.1注册码 user name:newasp 注册码: ===== LICENSE BEGIN ===== 16417-12042010 00001 ...

  10. 如何将文本编辑器嵌入框架--以Umeditor&CodeIgniter框架为例

    转:http://blog.csdn.net/u013332865/article/details/52066211 最近接到一个给某私立贵族(小,初,高 12年只是学费近200W)学校做一个网站,时 ...