【发布时间】: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