2012-03-31 25 views
0

把一個非常簡單的Knockout.js例子放在一起來學習它是如何工作的。爲什麼簡單的knockout.js無限加載默認視圖?

我立即意識到它無限加載並導致瀏覽器做一個計算器。基本上,它運行默認的runRoute並且不會停止!

一個的jsfiddle你可以充分見證和使用調試器檢查:http://jsfiddle.net/hn5JS/

的基本代碼:

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()); 

這是sammy.js或者我自己的代碼的問題嗎?我需要使用不同版本的jQuery嗎?謝謝您的幫助。

回答

3

能夠找出解決方案。通過將ko.applyBindings(new AlloyViewModel());包裝在jQuery document.ready函數中,它成功執行。看起來它在DOM未完全加載時與數據綁定有關。

相關問題