1

I'm working on a web project, and we want to try to use KnockoutJS. To start, I build my app with Chrome (cause on the official, Knockout seems to work with IE8), but when I try my web application in IE8, I got many exceptions :

HTML :

<ul class="nav nav-tabs" id="tabs" data-bind="foreach: items">
    <li data-bind="'id': 'id', css: { active: 'id' == $parent.selectedTab().id }, 'click': $parent.changeTab">
        <a data-toggle="tab" data-bind="text: name, attr: { 'href': 'href' }"></a>
    </li>
</ul>

JS :

var tabs = [
    new TabViewModel(1, "Tab 1", true),
    new TabViewModel(2, "Tab 2", true),
    new TabViewModel(3, "Tab 3", false),
];

function TabViewModel(id, name, enabled) {
    var self = this;

    self.id = ko.observable("test" + id);
    self.name = name;
    self.paneId = "tab" + id;
    self.href = ko.observable("#tab" + id);
    self.displayId = "header_tab" + id;
    self.enabled = ko.observable(enabled);

    ko.bindingHandlers.changeStates = {
        init: function (element, valueAccessor) {
            var enable = valueAccessor();
            if (enable) {
                $(element).removeClass("disabled");
            } else {
                $(element).addClass("disabled");
            }
        },
        update: function (element, valueAccessor) {
            var enable = valueAccessor();
            if (enable) {
                $(element).removeClass("disabled");
            } else {
                $(element).addClass("disabled");
            }
        }
    }
}

function SurveyViewModel(tabs) {
    var self = this;

    self.items = ko.observableArray(tabs);
    self.selectedTab = ko.observable(self.items()[0]);
    self.changeTab = function (tab) {
        if (tab.enabled())
            self.selectedTab(tab);

        return true;
    };
}

ko.applyBindings(new SurveyViewModel(tabs));

I have issues on :

  • 2 line, the 'changesStates' throw exception : Unable to parse bindings. Message: TypeError: Object expected; Bindings value
  • 3 linen with the href property : Error: Unable to parse bindings. Message: TypeError: 'href' is undefined

Thanks in advance

Joffrey Kern
  • 6,449
  • 3
  • 25
  • 25

1 Answers1

6

I am pretty sure it's this:

var tabs = [
    new TabViewModel(1, "Tab 1", true),
    new TabViewModel(2, "Tab 2", true),
    new TabViewModel(3, "Tab 3", false),
];

You have an extra comma at the end. IE isn't very smart, so it thinks you meant to have a 4th item in the array that was null.

woz
  • 10,888
  • 3
  • 34
  • 64