Javascript js刷新数据

Javascript js刷新数据,javascript,knockout.js,Javascript,Knockout.js,我已经写了以下五段代码,我陷入了这一点——我要求服务器提供一些数据,而且,我收到了这段数据。现在,我只想更新我的收藏 function GameViewModel() { var self = this; self.colors = []; // I had to create additional lists that contain the same data as observableArrays. Is it necessery? self.storages

我已经写了以下五段代码,我陷入了这一点——我要求服务器提供一些数据,而且,我收到了这段数据。现在,我只想更新我的收藏

function GameViewModel() {
    var self = this;

    self.colors = [];   // I had to create additional lists that contain the same data as observableArrays. Is it necessery?
    self.storages = [];

    self.colorList = ko.observableArray([]);
    self.storageList = ko.observableArray([]);

    $.getJSON("api/Color", function (allData) {
        _.each(allData, function (color) {
            var newColor = new Color(color.Name, color.ColorHash, color.ColorHex, color.Amount, color.HourIncrement, color.IncrementBonus, color.VisitIncrement, color.VisitFrequency);
            self.colorList.push(newColor);
            self.colors.push(newColor);
        });

        $.getJSON("api/Storage", function (storageData) {
            _.each(storageData, function (storage) {
                var foundColor = _.find(allData, function (color) { return color.ColorHash == storage.ColorHash; });
                var newStorage = new Capacity(storage.ColorHash, foundColor.ColorHex, foundColor.Amount, storage.ActualStorage);
                self.storageList.push(newStorage);
                self.storages.push(newStorage);
            });
        });
    });

    self.enlargeStorage = function (storage) {
        $.ajax({
            type: "GET",
            url: "api/Storage",
            data: { color: storage.colorHash },
            beforeSend: function (xhr) {
                xhr.overrideMimeType("text/plain; charset=x-user-defined");
            }
        })
        .done(function (returnObj) {
            for (var i = 0; i < self.storages.length; i++) {
                if (self.storages[i].colorHash == returnObj.ColorHash) {
                    self.storages[i].actualMaxAmount = returnObj.ActualStorage;
                    break;
                }
            }

            self.storageList = ko.observableArray(self.storages);
        });
    };
};
ko.applyBindings(new GameViewModel());
函数GameViewModel(){
var self=这个;
self.colors=[];//我必须创建包含与ObservalArrays相同数据的其他列表。是否有必要?
self.storages=[];
self.colorList=ko.observearray([]);
self.storageList=ko.observearray([]);
$.getJSON(“api/Color”,函数(allData){
_.每个(所有数据、功能(颜色){
var newColor=新颜色(Color.Name、Color.ColorHash、Color.ColorHex、Color.Amount、Color.HourIncrement、Color.IncrementBonus、Color.VisitIncrement、Color.VisitFrequency);
self.colorList.push(newColor);
self.colors.push(newColor);
});
$.getJSON(“api/Storage”,函数(storageData){
_.每个(存储数据、功能(存储){
var foundColor=u2;.find(allData,函数(color){return color.ColorHash==storage.ColorHash;});
var newStorage=新容量(storage.ColorHash、foundColor.ColorHex、foundColor.Amount、storage.ActualStorage);
self.storageList.push(新闻存储);
self.storage.push(新闻存储);
});
});
});
self.enlargeStorage=功能(存储){
$.ajax({
键入:“获取”,
url:“api/存储”,
数据:{color:storage.colorHash},
发送前:函数(xhr){
xhr.overrideMimeType(“text/plain;charset=x-user-defined”);
}
})
.完成(功能(返回OBJ){
对于(var i=0;i
查看代码:

 <ul data-bind="foreach: storageList">
        <li>
            <table class="colorStorageContainer">
                <tr>
                    <td>
                        <table class="colorStorageSize">
                            <tr>
                                <td data-bind="style: { backgroundColor: colorHex }"></td>
                                <td>Capacity</td>
                                <td data-bind="text: actualMaxAmount"></td>
                                <td data-bind="text: actualAmountPerctentage"></td>
                                <td><button data-bind="click: $parent.enlargeStorage">Enlarge</button></td>
                            </tr>
                        </table>
                    </td>
                </tr>
  • 容量 扩大
。。。

数据不会刷新。看起来什么都没发生。调试器不显示任何异常

如何刷新self.storageList?

这就是问题所在:

self.storageList = ko.observableArray(self.storages);
应该是:

self.storageList(self.storages);
你不应该换耳环。因为只有旧的(原始的observableArray)绑定到视图

您不需要两个版本的数据。 试试这个:

.done(function (returnObj) {
    // notice the (), this evaluates the observableArray and get the inner array
    var storages =  self.storageList();

     for (var i = 0; i < storages.length; i++) {
        if (storages[i].colorHash == returnObj.ColorHash) {
            storages[i].actualMaxAmount = returnObj.ActualStorage;
            break;
        }
     }
});
.done(函数(returnObj){
//注意()
var storages=self.storageList();
对于(var i=0;i
我发现这个问题:

 if (self.storages[i].colorHash == returnObj.ColorHash)

JavaScript无法比较这两个值。returnObj有两个属性,调试器显示这个值,但若我把鼠标放在ColorHash上,类型是未定义的。我尝试过使用u.find()下划线方法,但我认为这是不刷新的原因。

是否确实调用了api/Storage的回调,并且storageData包含数组?100%确定。我在api/存储代码中设置断点,在self.storageList=ko.observableArray(self.storages);。两个断点都会触发,我可以在客户端看到接收到的数据。您可以发布视图吗?谢谢您的编辑:)为什么要创建两个版本的数组?我看不出这两行之间有什么区别,但我复制了第二行,它不会改变任何内容。@Fka这应该可以,但请尝试以下操作:
self.storageList(ko.utils.unwrapObservable(self.storages))
@Armand storages是不可观察的,所以它不需要打开包装。@Fka是的,我在进一步检查您的操作后看到了