2016-12-09 51 views
0

我正在做一個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: '&nbsp;', 
     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); 
    } 
} 

};

+0

你能顯示你的視圖模型的代碼? – gkb

+0

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

+0

上一頁下一頁:顯示您正在使用的自定義綁定以及data [j] .Media [a]中的對象格式(應該具有「排除」屬性的對象) – user3297291

回答

0

從錯誤消息看,它看起來像valueBinding上的value屬性,不是函數/可觀察的。
var options = { state: valueBinding.value(), ... } { value: Exclude, options: {size: 'mini'}被傳遞到結合,所以value僅僅是一個正被所述AllMedia觀察到陣列上推該目的,即self.AllMedia.push(data[j].Media[a])Exclude參考。

如果Exclude不是函數/可觀察的,那麼它應該被訪問爲valueBinding.value。如果您不知道它是否是可觀察的,則可以使用ko.unwrap(valueBinding.value)來安全地獲取該值。

0

那麼答案很簡單,這是因爲我在做推動,我在做 self.AllMedia.push(data [j] .Media [a]);
self.AllMedia是一個可觀察數組,Array中的元素需要是使開關可以工作的函數。

解決方案:

var media = { 
        Id: ko.observable(data[j].Media[a].Id), 
        Default: ko.observable(data[j].Media[a].Default)} 

,然後按媒體

相關問題