crockpotveggies
crockpotveggies

Reputation: 13320

Why does simple knockout.js load default view infinitely?

Put together an extremely simple Knockout.js exmaple to learn how it works.

I immediately realized it loads infinitely and causes the browser to do a stackoverflow. Basically, it runs default runRoute and will not stop!

A jsFiddle that you can fully witness and inspect using the debugger: http://jsfiddle.net/hn5JS/

The basic code:

function AlloyViewModel() {
    // Data
    var self = this;
    self.appViews = ['Dashboard', 'Engine', 'Map', 'Jobs', 'Clients', 'Users'];
    self.currentAppView = ko.observable();

    // Behaviours
    self.goToAppView = function(appView) { 
        location.hash = '/' + appView;
    };

    // Client-side routes    
    Sammy(function() {
        this.get('#/:folder', function() {
            self.currentAppView(this.params.appView);
        });
        this.get('', function() { this.app.runRoute('get', '#Dashboard') });
    }).run();

};

ko.applyBindings(new AlloyViewModel());

Is this an issue with sammy.js or my own code? Do I need to use a different version of jQuery? Thanks for the help.

Upvotes: 0

Views: 279

Answers (1)

crockpotveggies
crockpotveggies

Reputation: 13320

Was able to figure out the solution. By wrapping ko.applyBindings(new AlloyViewModel()); in a jQuery document.ready function, it successfully executed. Looks like it has issues with data bindings when the DOM is not fully loaded.

Upvotes: 3

Related Questions