Raghavendra Reddy
Raghavendra Reddy

Reputation: 67

Knockout fallback view

Durandal, RequireJS, KnockoutJS

I have a generic and a client specific view in my project.

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

I want to show the client specific view if it exists or else show the default view.

Something like this.

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

Upvotes: -1

Views: 174

Answers (1)

Jason Spake
Jason Spake

Reputation: 4304

I think you're better off handling this in your view-model than in your view. Durandal lets you use an observable property for its compose binding so you can do something like this:

<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');
        }
    });

}

Trying to load the view path twice seems a bit redundant of course but if you don't have any other way to know if the view exists or not then it might be your best bet.

Upvotes: 0

Related Questions