I am new to Durandal. I have little experience with KnockoutJS. I am trying to populate an observable array from json obtained from a php file. Then I try to load an array into a table. There seems to be a problem loading the data into the table the first time the page loads. I execute system.log (of clients) to make sure the data is in the observable array that it is. When I refresh the page, the data is loaded into the table as I want. It seems that the observed array is filling up, but the view is not updating.
I was wondering what I am doing wrong so that it does not load the first time the page loads. Here is a test site that I played with durandal: http://dev.codeplanetapps.com/spa/ . After downloading, click "Clients". This page is http://dev.codeplanetapps.com/spa/#/customers . When this page loads directly, it works fine, but when I download the application from the main page and then switch to clients, it does not load the table correctly. Thanks in advance for any help.
Here is a view:
<div>
<form class="form-inline customerForm">
<span>Add New Customer</span>
<input type="text" data-bind="value: inputName" placeholder="Name" />
<input type="text" class="date input-small" data-bind="value: inputDOB" placeholder="Date of Birth" />
<input type="text" class="input-small" data-bind="value: inputPhone" placeholder="Phone" />
<input type="text" data-bind="value: inputEmail" placeholder="Email" />
<button type="submit" class="btn btn-primary" data-bind="click: submitCustomer">Add</button>
</form>
<table class="table table-hover table-bordered customerTable">
<thead>
<tr>
<th>Name</th>
<th>DOB</th>
<th>Phone Number</th>
<th>Email</th>
<th></th>
</tr>
</thead>
<tbody data-bind="foreach: customers">
<tr>
<td data-bind="text: name"></td>
<td data-bind="text: dob"></td>
<td data-bind="text: phone"></td>
<td data-bind="text: email"></td>
<td><button class="btn btn-danger btn-mini" data-bind="click: $root.removeCustomer">Delete</button></td>
</tr>
</tbody>
</table>
And here is the viewmodel:
define(function(require){
var system = require('durandal/system');
function Customer(data) {
this.name = ko.observable(data.name);
this.dob = ko.observable(data.dob.substring(5,7) + '/'
+ data.dob.substring(8,10) + '/' + data.dob.substring(0,4));
this.phone = ko.observable(data.phone);
this.email = ko.observable(data.email);
};
var inputName = ko.observable('');
var inputDOB = ko.observable('');
var inputPhone = ko.observable('');
var inputEmail = ko.observable('');
var customers = ko.observableArray([]);
return {
inputName: inputName,
inputDOB: inputDOB,
inputPhone: inputPhone,
inputEmail: inputEmail,
customers: customers,
activate: function(data) {
$('.customerNav').addClass('active');
$('.firstNav').removeClass('active');
$('.secondNav').removeClass('active');
$.getJSON(
'php/query.php',
{
mode: 'select',
table: 'customers',
'fields[]': '*',
'values[]': '*'
},
function(data) {
var customer = $.map(data, function(item) {return new Customer(item) });
customers(customer);
}
);
setTimeout(function() {
$('.date').datepicker();
},500);
}
};
});
, jQuery , setTimeout(). datepicker. , - , , .