我需要計算一組日期時間的服務器端,以允許更改日期/時間客戶端。 下面的代碼按照加載服務器數據工作,並將我需要的iso字段添加到服務器上。具有計算屬性的敲除映射嵌套數組
當用戶更改日期/時間輸入字段時,如何使iso字段可動態計算?
淘汰賽JS至今:
var ViewModel = function() {
var self = this;
// ...other first-level observables and funct
function scadenzeNuove(payload) {
ko.mapping.fromJS(payload, {}, this);
this.iso = ko.pureComputed(function() {
var data_re = /^(\d{2})\/(\d{2})\/(\d{4})T(\d{2}):(\d{2})/;
if (!payload.data) {
return false;
};
var data_string = payload.data;
if (!payload.ora) {
data_string = data_string + 'T00:00';
} else {
data_string = data_string + 'T' + payload.ora;
};
var data_match = data_re.exec(data_string) ? data_re.exec(data_string) : false;
if (!data_match) { return false; };
var data = new Date(data_match[3], data_match[2]-1, data_match[1], data_match[4], data_match[5])
return data;
}, this);
};
var scadenzeNuoveMapping = {
create: function(options) {
return new scadenzeNuove(options.data);
},
};
self.scadenzeNuove = ko.observableArray();
self.compScadenze = function(form) {
$.getJSON('{{ compute_scadenza }}', $(form).serialize(), function(data){
ko.mapping.fromJS(data, scadenzeNuoveMapping, self.scadenzeNuove);
});
};
};
var vm = new ViewModel()
ko.applyBindings(vm);
HTML:
<div data-bind="if: scadenzeNuove">
<ul data-bind="foreach: scadenzeNuove">
<li>
<span data-bind="text: iso().toLocaleFormat()"></span>
<input type="text" name="n_data" data-bind="value: data">
<input type="text" name="n_ora" data-bind="value: ora">
<input type="text" name="n_desc" data-bind="value: desc">
</li>
</ul>
</div>
我想的問題是ISO財產d沒有被映射,因爲映射調用發生在...之前...但我不知道如何應用正確的時間... – user2154587
我試圖使用一個裸ko.computed,如[本回復]中所示(https:/ /stackoverflow.com/questions/10906252/knockout-mapping-with-computed-fields?rq=1)但仍然無法正常工作:iso字段不會隨着組件字段值更改而更新。 – user2154587