1

Given the following scenario for our Durandal application:

When a customer visits certain URL's of our SPA we would like to present him a dialog/pop-up. For the initial load this is not a problem, we call a function from the activate callback function in the shell.js.

The problem is when the user navigates through our site and reaches a certain URL where the dialog should pop up.

Let say we've two viewmodels product and prodcutOverview and the shell. All three would have an activate function like this:

vm.activate = function () {
    var keywordsExists = checkUrl();
    if(keywordsExists){
       globals.showDialog();
    }
};

The idea of globals comes from this question

How do I create one activate function that is used on every route change so I prevent the duplicate code on my viewmodels?

Community
  • 1
  • 1
Andrew
  • 5,395
  • 1
  • 27
  • 47

2 Answers2

2

You could create the common activate function as a require module and then reuse it in that manner.

define('common-activate', ['globals'], function(globals){
   function checkUrl(){
      //checks url
   }
   var activate = function(){
      var keywordsExists = checkUrl();
      if(keywordsExists){
      globals.showDialog();
   }
   return activate;
}

define('products', ['common-activate'], function(activate){
   return {
      activate: activate
   };
}

define('productOverview', ['common-activate'], function(activate){
   return {
      activate: activate
   };
}
Anish Patel
  • 4,332
  • 1
  • 30
  • 45
1

You could use the Pub/Sub system in Durandal. Have a method in your shell.js that has the sole purpose of showing the dialog that you require that is then subscribed to the message <your_message_name>, then in each of the places that you want the dialog to show up, publish the message <your_message_name>.

Another thought using the same technique would be to publish a message of what url the app is visiting, then on the subscriber determine when to show the dialog. that way all the logic for determining when to show the dialog is in one place instead of spread all over the application.

Nathan Fisher
  • 7,961
  • 3
  • 47
  • 68