我正在做一個ajax調用並將數據加載到ko.observablearray([])
調用self.AllMedia;
然後數組被加載我正在爲每個介質執行操作。當我做結合ko.applyBindings(self);
我得到一個錯誤,指出:無法處理綁定「bootstrapSwitchOn:function(){return Exclude}」
knockout-3.4.0.js:72 Uncaught TypeError: Unable to process binding "foreach: function(){return AllMedia }"
Message: Unable to process binding "bootstrapSwitchOn: function(){return Exclude }"
Message: valueBinding.value is not a function
HTML:
<div id="draggablePanelList" data-bind="foreach: AllMedia">
<div class="col-sm-3 list-unstyled">
<div class="panel panel-info">
<div class="panel-heading">
<div class="col-sm-4 text-center">
<label @*for="IsDisabled0"*@ class="control-label">Disabled</label>
<input type="checkbox" id="IsDisabled0" data-bind="bootstrapSwitchOn: { value: Exclude, options: {size: 'mini'}}" />
</div>
</div>
</div>
</div>
</div>
視圖模型:
self.load = function() {
self.service.getProductDetail(self.Id(), self.populate);
}
self.populate = function(data) {
ko.mapping.fromJS(data, "", self);
self.loadSubProductsandMedia(data.SubProducts);
}
self.loadSubProductsandMedia = function(data) {
self.AllMedia.removeAll();
for (var j = 0; j < data.length; j++) {
for (var a = 0; a < data[j].Media.length; a++) {
self.AllMedia.push(data[j].Media[a]);
}
}
}
ko.applyBindings(self);
ko.bindingHandlers.bootstrapSwitchOn = {
init: function (element, valueAccessor, allBindingsAccessor, viewModel) {
var valueBinding = valueAccessor();
var options = {
state: valueBinding.value(),
size: null,
animate: true,
disabled: false,
readonly: false,
indeterminate: false,
inverse: false,
radioAllOff: false,
onColor: 'primary',
offColor: 'default',
onText: 'ON',
offText: 'OFF',
labelText: ' ',
handleWidth: 'auto',
labelWidth: 'auto',
baseClass: 'bootstrap-switch',
wrapperClass: 'wrapper',
onInit: function(event, state) { },
onSwitchChange: function (event, state) { }
};
ko.utils.extend(options, valueBinding.options);
$(element).bootstrapSwitch(options);
$(element).on('switchChange.bootstrapSwitch', function (e, data) {
valueBinding.value(data);
}); // Update the model when changed.
},
update: function (element, valueAccessor, allBindingsAccessor, viewModel) {
var vStatus = $(element).bootstrapSwitch('state');
var vmStatus = ko.utils.unwrapObservable(valueAccessor().value);
if (vStatus !== vmStatus) {
$(element).bootstrapSwitch('state', vmStatus);
}
}
};
你能顯示你的視圖模型的代碼? – gkb
self.load = function(){ self.service.getProductDetail(self.Id(),self.populate); } self.populate = function(data){ ko.mapping.fromJS(data,「」,self); self.loadSubProductsandMedia(data.SubProducts); self.loadedData(true); ko.applyBindings(self); } self.loadSubProductsandMedia = function(data){ self.AllMedia.removeAll(); (var a = 0; a
上一頁下一頁:顯示您正在使用的自定義綁定以及data [j] .Media [a]中的對象格式(應該具有「排除」屬性的對象) – user3297291