2017-04-01 75 views
-1

迪朗达尔,RequireJS,KnockoutJS淘汰赛后备视图

我有一个通用的,在我的项目客户特定视图。

│ faq.html 
│ welcome.html 
│ shell.html 
│ 
├───Client1 
│  faq.html 
│ 
├───Client2 
│  faq.html 
│  welcome.html 

我想告诉客户特定视图,如果它存在,否则显示的默认视图。

就是这样。

<div> 
    <div data-bind="compose: { view:'views/Client1/faq.html', fallbackView: 'view/faq.html' }"></div> 
</div> 

回答

0

我认为你最好在你的视图模型中比在你的视图中处理这个更好。迪朗达尔让您使用可观察的属性为其撰写绑定,所以你可以做这样的事情:

<div> 
    <div data-bind="compose: { view: currentView }"></div> 
</div> 

...

viewModel = function(){ 
    var self = this; 
    self.currentView = ko.observable(); 

    self.loadView = require(['text!views/Client1/faq.html', 'text!view/faq.html'], function(clientView, defaultView) { 
     if(clientView) { 
      self.currentView('views/Client1/faq.html'); 
     } else { 
      self.currentView('view/faq.html'); 
     } 
    }); 

} 

试图加载视图路径两次似乎有点多余,当然,但如果您没有任何其他方式知道该视图是否存在,那么这可能是您最好的选择。