Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
35
rated 0 times [  37] [ 2]  / answers: 1 / hits: 16933  / 12 Years ago, wed, march 21, 2012, 12:00:00

I just started using knockout.js and it works great with normal bidings. I have a problem with observableArray.



I want to create an observableArray and assign to it a JSON data from Google Feed API. Here is the JSON format 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



<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. The observable feedHead is OK.



Also I dont have any JS error. Any help..


More From » json

 Answers
11

Try declaring ( where you have the // Data )



self.allEntries = ko.observableArray([]);


then in the load...



self.allEntries(result.feed.entries);

[#86708] Monday, March 19, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
finn

Total Points: 154
Total Questions: 88
Total Answers: 82

Location: Lithuania
Member since Mon, Nov 16, 2020
4 Years ago
;