我被困住了,希望能得到一些帮助!
我的 ViewModel 上有一个 create 方法,它调用了一个轻而易举的 CreateEntity 方法来将新项目添加到 Office 类型的可观察数组中。这一切都很好,可观察数组的长度也相应增加 - 但是!
出于某种奇怪的原因,表格行没有相应地刷新-我做错了什么???
这是表格标记:
<table class="table table-striped table-bordered table-condensed" data-bind='visible: offices().length > 0'>
<thead>
<tr>
<td class="thead">Office Name</td>
<td class="thead">Support Tel.</td>
<td class="thead">Support Email</td>
<td class="thead">Support Fax</td>
<td class="thead">Ftp URL</td>
<td class="thead" />
</tr>
</thead>
<tbody data-bind='foreach: offices'>
<tr class="">
<td>
<input data-bind='value: officeName' />
</td>
<td>
<input data-bind='value: supportNo' />
</td>
<td>
<input data-bind='value: supportEmail' />
</td>
<td>
<input data-bind='value: supportFax' />
</td>
<td>
<input data-bind='value: supportFtp' />
</td>
<td>
<div class="button-bar">
<button class="btn btn-danger"
data-bind="click: deleteOffice, disable: hasChanges">
Delete
</button>
</div>
</td>
</tr>
</tbody>
</table>
这是我的视图模型代码
define(['services/officesEntityService',
'durandal/plugins/router',
'durandal/system',
'durandal/app',
'services/logger'],
function (officesEntityService, router, system, app, logger) {
var offices = ko.observableArray();
var isSaving = ko.observable(false);
var isDeleting = ko.observable(false);
var activate = function () {
return officesEntityService.getOffices(offices);
};
...
var addOffice = function () {
offices.push(officesEntityService.createOffice());
}
...
var vm = {
offices: offices,
isSaving: isSaving,
isDeleting:isDeleting,
activate: activate,
goBack: goBack,
hasChanges: hasChanges,
cancel: cancel,
canSave: canSave,
save: save,
canAdd:canAdd,
addOffice:addOffice,
deleteOffice: deleteOffice,
canDeactivate: canDeactivate,
title: 'Offices Administration'
};
return vm;
});
最后,我的 Breeze Data Service 公开了这个在 VM 中使用的函数,用于将新办公室推送到 observable 数组中:
var createOffice = function () {
return manager.createEntity('Office', { officeId: jQuery.Guid.New(), officeName: 'New Office'});
};