有一個模型叫顧客:我我如何調用一個函數在一個ViewModel淘汰賽
function customer(id, name, age, comments) {
var self = this;
self.Id = id;
self.Name = name;
self.Age = age,
self.Comments = comments;
self.addCustomer = function() {
$.ajax({
url: "/api/customer/",
type: 'post',
data: ko.toJSON(this),
contentType: 'application/json',
success: function (result) {
//SOMETHINGS WRONG HERE
customerVM();
}
});
}
}
後,我添加客戶的客戶名單犯規自動更新。在模型中調用customerVM()會進入viewModel函數,但永遠不會進入getCustomers函數,所以我必須調用viewModel錯誤。那就是我從調試中看到的。
顯示的列表功能是在視圖模型:
function customerVM() {
var self = this;
self.customers = ko.observableArray([]);
self.getCustomers = function() {
self.customers.removeAll();
$.getJSON("/api/customer/", function (data) {
$.each(data, function (key, val) {
self.customers.push(new customer(val.Id, val.Name, val.Age, val.Comments));
});
});
};
}
我需要添加一個客戶後,不知何故調用GetCustomers的。我該怎麼做?
這裏是客戶html的
<table >
<thead>
<tr>
<th>Id</th>
<th>Name</th>
<th>Age</th>
<th>Comments</th>
</tr>
</thead>
<tbody data-bind="foreach: customers" >
<tr>
<td data-bind="text: Id"></td>
<td data-bind="text: Name"></td>
<td data-bind="text: Age"></td>
<td data-bind="text: Comments"></td>
</tr>
</tbody>
</table>
<br />
<input type="button" id="btnGetCustomers" value="Get Customers" data-bind="click: getCustomers" />
在模型中調用customerVM()會進入viewModel函數,但永遠不會進入getCustomers函數,所以我必須調用viewModel錯誤。那就是我從調試中看到的 –