【问题标题】:In a program using knockout.js with multiple ViewModels, how can know one specific view model is changed by change in the property?在使用带有多个 ViewModel 的 knockout.js 的程序中,如何知道一个特定的视图模型会因属性的变化而改变?
【发布时间】:2017-07-09 23:33:42
【问题描述】:

我正在使用带有敲除的 asp.net mvc 进行数据绑定。 我有三个视图模型,如下所示:

function PersonViewModel() {
        this.firstName = ko.observable("@Model.FirstName");
        this.lastName = ko.observable("@Model.LastName");
    }
    function ContactViewModel() {
        this.homePhone = ko.observable("@Model.HomePhone");
        this.mobile = ko.observable("@Model.Mobile");
    }

    function AddressViewModel() {
        this.city = ko.observable("@Model.City");
        this.street = ko.observable("@Model.Street");
    }

 var pvm = new PersonViewModel();
    var avm = new AddressViewModel();
    var cvm = new ContactViewModel();
    var pNode = $("#personal-information").get(0);
    var aNode = $("#address-information").get(0);
    var cNode = $("#contact-information").get(0);
    ko.applyBindings(pvm, pNode);
    ko.applyBindings(avm, aNode);
    ko.applyBindings(cvm, cNode);

HTML如下:

<div id="personal-information">
    <input data-bind="value: firstName" type="text" >
    <input data-bind="value: lastName" type="text" >
</div>
<div id="contact-information">
    <input data-bind="value: homePhone" type="text" >
    <input data-bind="value: mobile" type="text" >
</div>
<div id="address-information">
    <input data-bind="value: city" type="text" >
    <input data-bind="value: street" type="text" >
</div>

这些输入字段的默认值取自数据库中的 3 个不同的表。我想编辑这些值并更新这些表中的数据。

如果我只更改 PersonViewModel 中的输入值,我想发出一个 ajax 请求,该请求将只为 person 表调用更新查询。 地址和联系 ViewModel 也是如此。我知道如何发出 ajax 请求。

但我的问题是:使用 knockoutJs,我怎么知道只有那些特定的 ViewModel 已更新,以便我可以保留其余部分?

【问题讨论】:

    标签: jquery asp.net-mvc mvvm knockout.js


    【解决方案1】:

    对于每个视图模型,您可以创建一个其他可观察对象可以订阅的方法。例如,对于 PersonViewModel:

    function PersonViewModel() {
        this.firstName = ko.observable("@Model.FirstName");
        this.lastName = ko.observable("@Model.LastName");
    
        this.updatePerson = function () { /* AJAX */ };
    
        // subscriptions
        this.firstName.subscribe(this.updatePerson);
        this.lastName.subscribe(this.updatePerson);
     }
    

    因此,只要 firstNamelastName 的值发生变化,就会触发 updatePerson 方法。

    其他视图模型的想法相同

    function ContactViewModel() {
        this.homePhone = ko.observable("@Model.HomePhone");
        this.mobile = ko.observable("@Model.Mobile");
    
        this.updateContact = function () { /* AJAX */ };
    
        // subscriptions
        this.homePhone.subscribe(this.updateContact);
        this.mobile.subscribe(this.updateContact);
    }
    
    function AddressViewModel() {
        this.city = ko.observable("@Model.City");
        this.street = ko.observable("@Model.Street");
    
        this.updateAddress = function () { /* AJAX */ };
    
        // subscriptions
        this.city.subscribe(this.updateAddress);
        this.street.subscribe(this.updateAddress);
    }
    

    【讨论】:

    • 这很有帮助。谢谢拉斐尔。适用于我想做的事情。但是有没有办法在不为每个可观察对象调用 subscribe(this.update...) 的情况下做到这一点?我现在正在寻找一个简单的调整来单独处理这一切,而不是为每个可观察对象使用相同的代码。
    • 你可以使用一个计算的 observable 来“监听”其他几个 observable 的变化,然后只订阅这个计算的 observable 到 update 方法 -- stackoverflow.com/a/9278782/1942895
    • 现在我发现这仅适用于 ,当我使用
    猜你喜欢
    • 2017-07-10
    • 1970-01-01
    • 2018-04-26
    • 1970-01-01
    • 2014-10-06
    • 2017-09-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多