【问题标题】:Update Knockout Observable Array from another view model从另一个视图模型更新 Knockout Observable Array
【发布时间】:2017-02-06 03:49:49
【问题描述】:

这似乎是一个复杂的场景。但我会尽量简单地分解它。

在 viewmodel 中,我有一个绑定到下拉列表的 Observable 数组。

(function () {
    UserMgmt.PeopleViewModel = WebFramework.BaseViewModel.inherits({

    initializeViewModel: function (options) {
        this.initializeBinding(options);
    },
    initializeBinding: function (options) {
        .......
        .......
        this.intGroups = ko.observableArray([]);
        .......
        getGroupsForSite: function (cb) {
            this.setListValue('/GetGroupsForSite', { siteId: '123', startIndex: 0, maxRecordCount: 20 }, this.intGroups, cb);
        },
        setListValue: function (api, data, observable, cb) {
            this.postDataRequest(api, data, function (err, result) { //Gets teh data from service
                if (!err && result) {
                    observable(result.data);
                    if (cb) { cb(); }
                }
            });
        },
    ....
)},

现在从这个页面打开一个弹出窗口,它有一个不同的视图模型,你可以在那里保存一个新的组信息。

但在关闭模式弹出窗口时,该 previpus 页面的 dropdown value needs to updated with this saved value

我可以通过 observableArray 做到吗?

所以,我可以从这个新的视图模型更新,而无需重新加载整个前一页,它只更新下拉列表?

这是第二个视图模型 ....

   UserMgmt.IntrusionGroupListViewModel = WebFramework.BaseViewModel.inherits({
    initializeViewModel: function (options) {
        this.initializeBinding(options);
    },
    initializeBinding: function (options) {
        this.ErrorMessage = ko.observable("");
        this.IsError = ko.observable(false);


        SaveNewGroup: function () {

            debugger;
            //this.setListValue('/GetGroupsForSite', { siteId: '123', startIndex: 0, maxRecordCount: 20 }, this.intGroups, cb);
        }
}

我怎样才能实现所需的功能?

SaveNewGroup() 中应该包含什么才能使其工作?

【问题讨论】:

    标签: javascript mvvm knockout.js


    【解决方案1】:

    实例化两个视图模型的代码应将第一个视图模型或其intGroups 成员作为options 之一传递给第二个视图模型。然后第二个视图模型可以访问它需要的东西。您可以修改 observable 数组,更改将显示在视图中。

    如果由于某种原因没有代码知道这两个视图模型,您将需要使用Postbox 在两者之间进行通信。

    【讨论】:

      猜你喜欢
      • 2019-01-23
      • 2017-04-11
      • 2011-08-09
      • 2017-12-13
      • 2017-03-21
      • 2015-10-25
      • 2018-01-18
      • 2015-05-04
      • 2013-04-22
      相关资源
      最近更新 更多