awj100
unread,Mar 16, 2012, 10:30:18 AM3/16/12Sign in to reply to author
Sign in to forward
You do not have permission to delete messages in this group
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to KnockoutJS
I'll try to keep this description as simple as possible.
I'm making a jQuery AJAX request to a server, and in the 'success'
callback I have
success: function (data, textStatus) {
var jsonData = $.parseJSON(data);
jsonInitialUpload = jsonData;
ko.applyBindings(new AccountViewModel());
}
Accompanying this I have
function AccountViewModel() {
var self = this;
self.fields = ko.observableArray([]);
var mappedFields = $.map(jsonInitialUpload.Account.Fields,
function(item) { return new Field(item) });
self.fields(mappedFields);
}
function Field(data) {
this.FriendlyName = ko.observable(data.FriendlyName);
}
And then in the markup I have
<table>
<tbody>
<tr data-bind="foreach: fields">
<th data-bind="text: FriendlyName"></th>
</tr>
</tbody>
</table>
The array which is returned from the server contains 21 objects, so I
would expect to see a single <tr> containing 21 <th> elements. But
instead, when I use Firefox or Opera (not Chrome, IE or Safari - these
work as expected), I see a single <tr> containing 21 <th> elements
containing the first value in the array, followed by 21 <th> elements
containing the second value in the array, followed by 21 <th> elements
containing the third value in the array, and so on.
In other words, the 'foreach' renders each element in the array
fields().length times before iterating to the next element in the
array.
For example, here is a short snippet of the first 20-something <th>
elements:
<tr data-bind="foreach: fields">
<th data-bind="text: FriendlyName">Valuation date</th>
<th data-bind="text: FriendlyName">Valuation date</th>
<th data-bind="text: FriendlyName">Valuation date</th>
<th data-bind="text: FriendlyName">Valuation date</th>
<th data-bind="text: FriendlyName">Valuation date</th>
<th data-bind="text: FriendlyName">Valuation date</th>
<th data-bind="text: FriendlyName">Valuation date</th>
<th data-bind="text: FriendlyName">Valuation date</th>
<th data-bind="text: FriendlyName">Valuation date</th>
<th data-bind="text: FriendlyName">Valuation date</th>
<th data-bind="text: FriendlyName">Valuation date</th>
<th data-bind="text: FriendlyName">Valuation date</th>
<th data-bind="text: FriendlyName">Valuation date</th>
<th data-bind="text: FriendlyName">Valuation date</th>
<th data-bind="text: FriendlyName">Valuation date</th>
<th data-bind="text: FriendlyName">Valuation date</th>
<th data-bind="text: FriendlyName">Valuation date</th>
<th data-bind="text: FriendlyName">Valuation date</th>
<th data-bind="text: FriendlyName">Valuation date</th>
<th data-bind="text: FriendlyName">Valuation date</th>
<th data-bind="text: FriendlyName">Valuation date</th>
<th data-bind="text: FriendlyName">Banking relationship</th>
<th data-bind="text: FriendlyName">Banking relationship</th>
<th data-bind="text: FriendlyName">Banking relationship</th>
<th data-bind="text: FriendlyName">Banking relationship</th>
While debugging I've placed a <textarea> on the page and bound this to
the same 'fields' observableArray, like so:
<textarea name="_flds" data-bind="value: ko.toJSON(fields)"></
textarea>
In this <textarea> I can see that the 'fields' observableArray is
correct - the JSON object contains one of each expected element, and
21 in total.
So can anyone explain to me why the 'foreach' iterator is rendering
[fields().length x each element in the 'fields' observableArray]
before moving on to the next element in the 'fields' observableArray?
I.e., why it is rendering 441 <th> elements rather than 21?
Please note, as I mentioned above, this problem is only occurring in
Firefox and Opera. Chrome, IE and Safari render 21 <th> elements, one
for each item in the 'fields' observableArray.