【问题标题】:ObservableArray not reflecting data updateObservableArray 不反映数据更新
【发布时间】:2011-05-24 21:08:56
【问题描述】:

我正在使用非常漂亮的 KnockoutJS 库创建一个应用程序,但我遇到了障碍。在 html 页面上,我有一个普通的 <select> 控件,我想使用从 Web 服务返回的 JSON 数据加载该控件。

我定义observable数组如下:

var laborRow = function () {
    this.positions = ko.observableArray([]);
};

当页面加载时,会进行 ajax 调用并返回数据。在回调中,我执行以下操作:

    success: function (msg) {
        laborRow.positions = msg;
    }

根据 KO 文档,我希望我会设置这样的结果:

laborRow.positions(msg);

但是,这只会引发一个错误,指出“laborRow.positions in not a function”

html中的模板如下:

<tbody data-bind='template: {name: "laborRowTemplate", foreach: laborLine}'> </tbody> 
</div>
  <script type="text/html" id="laborRowTemplate"> 
        <tr>

          <td><select data-bind='options: positions, optionsText: "Title", optionsCaption: "select", value: selectedPosition '></select></td>

        </tr>
    </script>

laborRow 对象是绑定到页面的 ViewModel 上的属性。无论出于何种原因,这都行不通。要添加另一个皱纹,如果我添加代码以窥视 observableArray 并打印出一些数据,数据就在那里。所以加载成功了。

任何想法将不胜感激。

我的示例案例的完整代码:

var laborRow = function () {
    this.positions = ko.observableArray([]);    
};

var projectEstimate = function () {
    this.laborLine = ko.observableArray([new laborRow()]);

};

var projectViewModel = new projectEstimate();
ko.applyBindings(projectViewModel);

//and the code in the callback function on ajax success

 success: function (msg) {
                laborRow.positions = msg;
                //laborRow.positions(msg); **this does not work - error is laborRow.positions is not a function**
            },

还有html:

 <tbody data-bind='template: {name: "laborRowTemplate", foreach:
laborLine}'> </tbody>

  <script type="text/html" id="laborRowTemplate">
        <tr>
          <td><select data-bind='options: positions, optionsText:
"Title",  optionsCaption: "select", value: selectedPosition '></
select></td>

        </tr>
    </script> 

最后,感谢下面 Sean 的 cmets,我可以通过修改回调中的代码来使其工作,如下所示:

success: function (msg) {
    projectViewModel.laborLine()[(projectViewModel.laborLine().length-1)].positionList(msg);
}

【问题讨论】:

    标签: javascript knockout.js


    【解决方案1】:

    问题是您实际上还没有创建模型:

    var laborRow = function () {
        this.positions = ko.observableArray([]);
        // will only be called if you call var some_var = new laborRow()
    };
    

    将你的函数改为一个裸对象(如Knockout docs所示):

    var laborRow = {
        positions: ko.observableArray([])
    };
    

    您将可以致电laborRow.positions(msg); 并让它工作。


    编辑

    基于新代码,laborRow 仍未实例化 - 如果您在代码中的其他位置设置 var laborRow(可能是围绕 ajax 请求),那么您需要确保您的调用堆栈看起来像这样:

    projectViewModel.laborLine()[0].positions() 
    // This will return the array you're looking for.
    // The key is that laborLine is a `getter` not an attribute
    

    我曾多次被“ko 变量是 getters 而不是 attributes”的错误所困扰......您的代码可能会发生这种情况吗?

    【讨论】:

    • 感谢肖恩的回复。我已经创建了模型。在实际的 viewModel 中,我有一个 observableArray,它在创建时会以一个新的 laborRow 对象开始生命:this.laborLine = ko.observableArray([new laborRow()]);
    • @Alex -- 太棒了......你能再发布一点你的代码吗?正如您所描述的,目前一切应该正常工作。
    • @Sean...感谢 Sean 的所有帮助...您的最后一个建议成功了...我会将您标记为答案并编辑我的帖子以分享工作代码...谢谢再次
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-07-25
    • 1970-01-01
    • 1970-01-01
    • 2016-03-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多