加载从阿贾克斯一个knockout.js observableArray()()调用 [英] loading a knockout.js observableArray() from .ajax() call

查看:163
本文介绍了加载从阿贾克斯一个knockout.js observableArray()()调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这令我费解。它必须是一些小事情,我没有看到。我试图加载一个非常简单的 observableArray 淘汰赛中与Ajax调用。

this puzzles me. it must be something small i'm not seeing. i'm trying to load a very simple observableArray in knockout with an ajax call.

的javascript

// we bind the array to the view model property with an empty array.
var data = [];   
var viewModel = {
    vendors: ko.observableArray(data)
};
ko.applyBindings(viewModel);

$(function () {
    // on this click event, we popular the observable array
    $('#load').click(function () {
        // WORKS. Html is updated appropriately.
        viewModel.vendors([{ "Id": "01" },{ "Id": "02" },{ "Id": "03" }]);

        // DOES NOT WORK. Fiddler2 shows the same exact json string come back 
        // as in the example above, and the success function is being called.
        $.ajax({
            url: '/vendors/10',
            dataType: 'json',
            success: function (data) {
                viewModel.vendors(data);
            }
        });
    });
});

HTML

<button id="load">Load</button>
<ul data-bind="template: { foreach: vendors }">
    <li><span data-bind="text: Id"></span></li>
</ul>


问:为什么成功的Ajax调用,谁的数据变量值匹配的字节为字节的硬盘类型值,不会触发HTML刷新?


Question: Why does the successful ajax call, who's data variable value matches byte-for-byte the hard typed value, not trigger the html refresh?

推荐答案

没有原因,这是行不通的罚款。由于这证明了。

There is no reason this would not work fine. As this demonstrates.

http://jsfiddle.net/madcapnmckay/EYueU/

我会检查阿贾克斯后实际上是返回JSON数据和JSON是一个数组,并且它被正确解析。

I would check that the ajax post is actually returning json data and that that json is an array and that it's being parsed correctly.

我不得不调整Ajax调用来获取小提琴AJAX处理程序才能正常工作。

I had to tweak the ajax call to get the fiddle ajax handlers to work correctly.

更没有什么我能想到的。

Nothing more I can think of.

希望这有助于。

这篇关于加载从阿贾克斯一个knockout.js observableArray()()调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆