2011-12-21 50 views
2

我有類似如下:子視圖模型拋出異常在Internet Explorer 8

var ChildViewModel = function (viewModel) { 
    // state 
    this.viewModel = viewModel; 
    this.index = ko.dependentObservable(function() { 
     return this.viewModel.selections().indexOf(this); 
    }, this); 
    this.remove = function() { 
     this.viewModel.removeSelection(this); 
    }; 
    this.moveUp = function() { 
     this.move(-1); 
    }; 
    this.moveDown = function() { 
     this.move(1); 
    }; 
    this.move = function (direction) { 
     var i = this.index(); 
     this.remove(); 
     this.viewModel.selections.splice(i + direction, 0, this); 
    }; 

    // additional properties 

}; 

var viewModel = { 
    selections: ko.observableArray(), 
    removeSelection: function (item) { 
     this.selections.remove(item); 
    }, 
    addSelection: function (event) { 
     var child = new ChildViewModel(this); 
     this.selections.push(child); 
    } 
}; 

ko.applyBindings(viewModel); 

當我打電話addSelection,我已經得到了KnockoutJS圖書館內Object doesn't support this property or method例外。我的應用程序在Firefox 3.6和Chrome中運行良好。我在IE8中遇到了異常。我正在使用KnockoutJS的版本2.0 1.3 Beta

我在做什麼錯?

+0

你在使用淘汰賽的縮小版嗎?如果是這樣,也許給未壓縮的版本一個鏡頭,看看哪一行在敲除錯誤,它可能會提供一個線索可能的原因。 – 2011-12-21 13:14:25

+0

@AlexKey - 我會更新我的問題。 – 2011-12-21 13:16:08

+0

@Daniel:我的錯誤 - 我沒有意識到Knockout實現了'indexOf()'。 – 2011-12-21 13:43:10

回答

2

好吧,我有2個不同的問題。

首先,我刪除了獲取數組值的方法,而不是使用observable數組。

// Bad code for IE8 
this.viewModel.selections().indexOf(this); 

// this works 
this.viewModel.selections.indexOf(this); 

此外,我設置for屬性。 IE抱怨說for是一個關鍵字。

// Bad code for IE8 
data-bind="attr: { for : logicalOperatorAndFieldId }" 

// Good code for IE8 
data-bind="attr: { 'for' : logicalOperatorAndFieldId }" 
+0

+1,我也遇到了一個與IE8有關的問題,聲稱'class'是關鍵字:'data-bind =「attr:{class:InitialClass}'使用字符串解決了它:'data-bind =「attr:{'class':InitialClass}' – danludwig 2012-03-13 17:06:48

+0

我只是在attr綁定中用'for'碰到IE8問題。爲我節省了很多時間,所以謝謝! – Ben 2012-04-18 13:35:15

2

不要使用自我結束標籤,如<span data-bind='text: Text' />。當你使用敲除時,它也會在IE8,IE7中造成問題。

+0

這不回答我的問題。 – 2012-01-17 14:19:19