【问题标题】:How do I render a View in Durandal with data returned from a jsonp call?如何使用从 jsonp 调用返回的数据在 Durandal 中呈现视图?
【发布时间】:2013-12-26 19:34:29
【问题描述】:

我无法查看从 JSONP 调用返回到 restful api 服务的数据。我正在使用杜兰达尔。我不确定它是否没有呈现是因为我使用了错误的数据绑定,或者是因为 jsonp 还是其他原因。

这是我的 clients.js 文件中的内容:

define(['plugins/http', 'durandal/app', 'knockout'], function (http, app, ko) {
return {
        displayName: 'Clients',
        clients: ko.observableArray([]),
        activate: function () {
            var that = this;

            return http.jsonp('http://site/api/clients/1', 'jsoncallback').then(function (response) {
                that.clients(response.items);
            });

        }
    };
}); 

返回的数据如下:

{
Id: 1,
Company: "Exosis",
email: "alisasandoval@exosis.com",
phone: "+1 (824) 431-2547",
address: "234 Ruby Street, Beechmont, Connecticut, 5450",
about: "Consectetur dolore excepteur ex sit nostrud.Voluptate tempor dolore minim do aliqua duis consequat nostrud amet. Nisi consequat eu Lorem ipsum.",
clientStartDate: "1990-09-18",
isActive: true,
clientType: "multiple"
}

我不明白我应该如何连接(数据绑定)到返回的数据。我已经尝试了所有我能想到的变化。 这是clients.hmtl文件:

<body> 
    <section>
        <h2 data-bind="text: displayName"></h2>
        <div data-bind="foreach: clients">
                <div data-bind="text: Id"></div>
                <div data-bind="text: Company"> </div>
        </div>

    </section>
</body>

感谢您的任何反馈。

更新 所以我将clients.js更改为 that.clients.push(response.items); 在调试器中我有 客户:数组[1] 0:未定义

【问题讨论】:

  • 你有什么错误吗?
  • 没有错误。什么都没有出现。
  • 对我来说看起来不错,但我建议下载 chrome 敲除上下文调试器,它可以帮助您查看要绑定的内容:chrome.google.com/webstore/detail/knockoutjs-context-debugg/…
  • 如果注释掉激活函数,“Clients”是否会显示在 displayName 绑定中?
  • 确保在调用activate()this 的上下文是正确的。

标签: javascript api jsonp durandal


【解决方案1】:

不确定是否可以在初始化后替换数组。尝试使用Knockout's pseudo-array functions(不是本机push)对每个元素进行pushing。

您可能需要等待确认,我无法立即测试替换,但我积极推动宣传的作品。

还可以先使用本地数据进行测试,并在重新启用 JSON-P 功能后监控您的网络流量。

当我在做的时候;如果可以(不相关),请考虑 CORS。

【讨论】:

  • 推送有帮助。然而,我仍然被一个我似乎无法查看的数组所困扰。我想知道是不是因为 jsonp 数据被包装在客户端类中。
猜你喜欢
  • 2017-09-30
  • 2012-10-22
  • 1970-01-01
  • 1970-01-01
  • 2010-11-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多