我試圖讓我的代碼儘可能模塊化,並模仿WPF和Caliburn.Micro的模式。以下是我迄今爲止使用Knockout組件嘗試過的內容。模塊化Web組件與基於視圖的數據綁定上下文
組件視圖模型
function welcomeViewModel() {
this.greeting = 'Hello world!;
}
應用視圖模型
function appViewModel() {
this.firstGreetingVM = new welcomeViewModel();
this.secondGreetingVM = new welcomeViewModel();
}
應用查看
<WelcomeWidget data-bind-to="firstGreetingVM"/>
<WelcomeWidget data-bind-to="secondGreetingVM"/>
如何我可以在視圖本身中定義上下文(使用什麼視圖模型)?
http://knockoutjs.com/documentation/component-custom-elements.html –