3

如何在不触发任何订阅的情况下设置可观察属性?

我有一个场景是页面加载,进行 ajax 调用以获取一些数据,循环数据,然后将当前选定的项目设置为可观察的。我希望能够在没有任何订阅的情况下设置这个 observable,因为第一次设置这个 observable 被认为是它的初始状态,并且订阅不应该在初始状态下执行。

function PlanViewModel() {
    var self = this;

    self.plans = ko.observableArray();
    self.selectedItem = ko.observable();

    self.getAllPlans = function () {
        $.ajax({
            url: "/Backoffice/Home/GetAllPlans",
            type: "POST",
            data: {},
            context: this,
            success: function (result) {
                var planList = this.plans;
                // clear the plan list
                planList.removeAll();
                $.each(result.plans, function () {
                    var planDetail = new PlanDetail(this, self);
                    if (this.IsSelected) {
                        self.selectedItem(planDetail); // how do I set this without the subscriptions firing?
                    }
                    planList.push(planDetail);
                });
            },
            error: function (result) {
                alert("An error occured getting plans.");
            }
        });
    }

    self.selectedItem.subscribe(function (newItem) {
        newItem.repositoryUpdateSelectedPlan();
    } .bind(self));
}
4

2 回答 2

5

你可以像这样重组你的代码:

function PlanViewModel() {
    var self = this;

    self.plans = ko.observableArray();

    self.getAllPlans = function () {
        $.ajax({
            // …
            success: function (result) {
                // …
                $.each(result.plans, function () {
                    var planDetail = new PlanDetail(this, self);
                    if (this.IsSelected) {
                        self.selectedItem = ko.observable(planDetail);
                    }
                    planList.push(planDetail);
                });
                if (self.selectedItem === undefined) { 
                    self.selectedItem = ko.observable(); 
                }
                self.selectedItem.subscribe(function (newItem) {
                    newItem.repositoryUpdateSelectedPlan();
                }.bind(self));
            },
            // …
        });
    }
}

也就是说,只有 在达到您想要的初始状态后才开始 Knockout。

于 2012-07-01T14:33:22.647 回答
-1

谢谢,我沿着这条路线走,并对其进行了一些修改。selectedItem observable 必须从一开始就在模型上定义,因为它在所有地方都用于绑定,但我确实像您建议的那样移动了订阅部分,效果很好。

function PlanViewModel() {
    var self = this;

    var selectedItemSubscription = null;

    self.plans = ko.observableArray();
    self.selectedItem = ko.observable();

    self.getAllPlans = function () {
        $.ajax({
            url: "/Backoffice/Home/GetAllPlans",
            type: "POST",
            data: {},
            context: this,
            success: function (result) {
                var planList = this.plans;
                // clear the plan list
                planList.removeAll();
                $.each(result.plans, function () {
                    var planDetail = new PlanDetail(this, self);
                    if (this.IsSelected) {
                        if (selectedItemSubscription != null)
                            selectedItemSubscription.dispose();

                        self.selectedItem(planDetail);
                    }
                    planList.push(planDetail);
                });

                selectedItemSubscription = self.selectedItem.subscribe(function (newItem) {
                    newItem.repositoryUpdateSelectedPlan();
                }.bind(self));
            },
            error: function (result) {
                alert("An error occured getting plans.");
            }
        });
    }
}
于 2012-07-01T15:25:33.397 回答