简体   繁体   English

ObservableArray绑定不会更新

[英]ObservableArray binding won't update

Edit: JSFiddle with comments 编辑: JSFiddle与评论

I'm developing my first SPA using knockoutjs. 我正在使用基因敲除开发我的第一个SPA。 My situation is: 我的情况是:

  • I have a list of items being displayed from which the user can select an item 我有一个显示的项目列表,用户可以从中选择一个项目
  • With an item selected, the user can make changes to the selected item 选择一个项目后,用户可以对所选项目进行更改
  • After confirming the changes, the SPA sends the updated data to the web api 确认更改后,SPA将更新的数据发送到Web API
  • However, the list displaying all my entries does not reflect the updates made to the item 但是,显示我所有条目的列表并不反映对该项目所做的更新

I created a simple fiddle.js ( see here ). 我创建了一个简单的fiddle.js( 请参阅此处 )。 It shows my problem better than 1000 words. 它比1000个单词更好地显示了我的问题。 I left out pagination logic for simplicity, but the observable for my list needs to be a computed for various reasons. 为了简单起见,我省略了分页逻辑,但是出于各种原因,我的列表的可观察值必须进行计算。

ViewMode. ViewMode。

var ViewModel = function() {

    var self = this;
    self.selectedItem = ko.observable();

    self.items = ko.observableArray([
    {
        name: "Item A",
        price: "12.99"
    },
    {
        name: "Item B",
        price: "13.99"
    },
    {
        name: "Item C",
        price: "90.99"
    }]);

    self.paginated = ko.computed(function() {
        // This is where I do some pagination and filtering to the content
        // It's left out here for simplicity. The binding for the list needs
        // to be a computed though.
        return self.items();
    });

    self.selectItem = function(item) {
        self.selectedItem(item);
    };

    self.save = function(item) {
        // Sending data to web api...

        // After the saving, the displaying list does not update to reflect the changes
        // I have made. However, switching entries and checking the changed item shows
        // that my changes have been saved and are stored in the observable.
    }

};

ko.applyBindings(new ViewModel());

View 视图

<!-- ko foreach: paginated -->
<a href="#" data-bind="text: name, click: $parent.selectItem"></a><br />
<!-- /ko -->
<br />
<br />
<div data-bind="visible: selectedItem">
    <!-- ko with: selectedItem -->
    <form>
        <input type="text" data-bind="value: name" />
        <input type="text" data-bind="value: price" />
        <br />
        <button type="button" data-bind="click: $parent.save">Save</button>
    </form>
    <!-- /ko -->
</div>

I hope you can help me out, I don't want to reload all the data from the server for the sake of performance and speed. 希望您能帮到我,我不想为了性能和速度而从服务器重新加载所有数据。

you have to make the properties of the objects in your array observable properties in order to reflect the changes to the UI. 您必须使数组中对象的属性成为可观察的属性,以反映对UI的更改。

self.items = ko.observableArray([
{
    name: ko.observable("Item A"),
    price: ko.observable("12.99")
},
{
    name: ko.observable("Item B"),
    price: ko.observable("13.99")
},
{
    name: ko.observable("Item C"),
    price: ko.observable("90.99")
}]);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM