[英]Create ko.observableArray from JSON Object in knockout
I just started using knockout.js and it works great with normal bidings. 我刚刚开始使用knockout.js,它在正常竞标中效果很好。 I have a problem with observableArray.
我有observableArray的问题。
I want to create an observableArray and assign to it a JSON data from Google Feed API. 我想创建一个observableArray并为其分配来自Google Feed API的JSON数据。 Here is the JSON format https://developers.google.com/feed/v1/devguide#resultJson
以下是JSON格式https://developers.google.com/feed/v1/devguide#resultJson
google.load("feeds", "1"); // Loads Google Feed API
function FeedViewModel()
{
// Data
var self = this;
self.allEntries = null;
// Example property, and it works
self.feedHead = ko.observable("BBC News");
var feed = new google.feeds.Feed("feeds.feedburner.com/BBCNews");
feed.setResultFormat(google.feeds.Feed.JSON_FORMAT);
feed.includeHistoricalEntries();
feed.setNumEntries(30);
// Loads feed results
feed.load(function (result) {
if (!result.error) {
self.allEntries = ko.observableArray(result.feed.entries);
// accessing the title from here is OK
alert(self.allEntries()[1].title);
}
});
}
In the above example, accessing the array from the ViewModel is OK but I need to display it in the view (to the browser) using foreach:allEntries 在上面的示例中,从ViewModel访问该数组是正常的,但我需要使用foreach:allEntries在视图中显示它(到浏览器)
<h2 data-bind="text: feedHead">Latest News</h2>
<!-- ko foreach:allEntries -->
<div class="lists">
<a href="#" data-bind="text: title"></a>
</div>
<!-- /ko -->
But nothing the ko foreach loop returns nothing. 但是ko foreach循环没有任何回报。 The observable feedHead is OK.
可观察的feedHead是好的。
Also I dont have any JS error. 我也没有任何JS错误。 Any help..
任何帮助..
Try declaring ( where you have the // Data ) 尝试声明(你有//数据)
self.allEntries = ko.observableArray([]);
then in the load... 然后在负载......
self.allEntries(result.feed.entries);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.