我在同一模塊視圖中多次編寫相同的表單,但是當我在一個字段中更改數據時,它會反映在每個表單上。在Durandal中編寫單獨的模塊實例
這是一些清理的代碼。正如你可以看到我確信不使用,因爲我要撰寫多次形式Singleton模式...
view.html
<button data-bind="click: append">Append</button>
<div data-bind="foreach: odbcForms">
<div data-bind="compose: { model: $data, activationData: settings }"></div>
</div>
viewmodel.js
define(['knockout', 'forms/odbc'], function (ko, odbcForm) {
var odbcForms = ko.observableArray()
var append = function() {
odbcForms.push(new odbcForm({ hostname: 'v1', db: 'v2' }));
};
return {
odbcForms: odbcForms,
append: append
}
}
形式/ odbc.html
<div>
<form class="form-horizontal" role="form">
<fieldset>
<div class="form-group" data-bind="validationElement: hostname">
<label for="hostname" class="col-sm-2 control-label">ODBC Link Name</label>
<div class="col-xs-4">
<input data-bind="value: hostname" type="text" class="form-control" id="hostname">
</div>
</div>
<div class="form-group" data-bind="validationElement: db">
<label for="db" class="col-sm-2 control-label">Database</label>
<div class="col-xs-4">
<input data-bind="value: db" type="text" class="form-control" id="db">
</div>
</div>
</fieldset>
</form>
</div>
種
形式/ odbc.js
define(['knockout'], function(ko) {
var ctor = function (settings) {
this.settings = settings;
};
ctor.prototype = {
constructor: ctor,
activate: function (settings) {
this.hostname(this.settings.hostname);
this.db(this.settings.db);
},
hostname: ko.observable().extend({
required: true,
minLength: 2
}),
db: ko.observable().extend({
required: true,
minLength: 2
}),
};
return ctor;
}
預先感謝您
你應該看看使用這種東西的小部件http://durandaljs.com/documentation/Creating-A-Widget.html –
我試過但同樣的問題。我正在使用它來動態追加表單到** view.html **: 'widget.create(document。getElementById(「odbc-forms-」+ appendClickCount()),{kind:'odbcForm',hostname:'',db:''});' – romseguy