CodeMoose
CodeMoose

Reputation: 3025

Unexpected token error on namespaced knockout binding

Building a simple app with Knockout, and I ran into a bug I don't fully understand.

Given these bindings:

var bindings = {
    employee: {
        name: ko.observable("Employee Name"),
        address: ko.observable("Employee Address")
    },

    ...
};

ko.applyBindings(bindings);

I can set up <input data-bind="value: employee.name"> without issue. The trouble starts when I try to wrap employee in another object:

var bindings = {
    case: {
        name: ko.observable("Case Name"),

        employee: {
            name: ko.observable("Employee Name"),
            address: ko.observable("Employee Address")
        }
    },

    ...
};

ko.applyBindings(bindings);

Trying to set up <input data-bind="value: case.employee.name"> returns an error Unexpected token: case. Is this some kind of limitation within Knockout itself, or am I overlooking something obvious? Googling "Unexpected token error knockout" (and its variants) doesn't return any useful results.

Upvotes: 0

Views: 553

Answers (1)

manji
manji

Reputation: 47978

case is a reserved word. When knockout tries to generate the binding function, the case word will not be understood by the js engine and will generate that error. Use another -not reserved- word.

Upvotes: 4

Related Questions