Coder Social home page Coder Social logo

Comments (7)

rniemeyer avatar rniemeyer commented on July 28, 2024

I think that I would have to see more code to help pin this one down. If you are using an observable to track the "current" module, then you will want to make sure that it is not getting set twice by the routing. You could check by creating a manual subscription against it and log out the value. Something like:

this.currentModule = ko.observable();
this.currentModule.subscribe(function(newValue) {
    console.log("module changed", newValue);
}, this);
```'
I am happy to help you out. Let me know if you have any additional code.  A simple throttle may help, but hard to say without seeing more.  Thanks!

from knockout-amd-helpers.

vishnurk avatar vishnurk commented on July 28, 2024

That was quick! Thanks for your response. Here is some more information..
So, I have a global view(which is my main HTML page) with its corresponding view model setup like below -

Main view - HTML page

      <div class="container">
          <div data-bind="module: {
                                name:MainModule.name,
                                template:MainModule.template,
                                data:MainModule.data,
                                initializer: 'Initialize',
                                disposeMethod: 'Dispose' 
                             }">            
         </div>
      </div>

main view model - code snippet

    //set to default values initially until a route gets activated
      var mainModule = {
                            type:'main',
                            name: ko.observable(" "),
                            template: ko.observable(" "),
                            data: ko.observable(null)
                          },
    //on route activation, the router calls the 'activatemodule' function below
    // passes in a module definition for a module that needs to be activated.
      activateModule = function (moduleDefinition) {
         if (typeof moduleDefinition !== 'undefined' && moduleDefinition!==null) {
            if (moduleDefinition.type == config.moduleTypes.main) {
               //activate module
               mainModule.name(moduleDefinition.name);
               mainModule.template(moduleDefinition.template);
               mainModule.data(moduleDefinition.data);
            } 
         }
      };

router - code snippet

    //sample get route. 
    //more routes setup w/ corresponding activation calls like below.
    sammy.get('#login', function () {
           vm.ActivateModule({
                                         type:'main',
                                         name: "vm.login",
                                         template: "login",
                                         data: {name:"Hello World"} 
                                         })
    });

Not sure if this is clear enough. Let me know what you think of the setup.

from knockout-amd-helpers.

vishnurk avatar vishnurk commented on July 28, 2024

Btw, I tried your suggestion and implemented manual subscriptions on mainModule.template & mainModule.name. Ran a quick test and verified that the template & the name are getting set only once, but, the initializer was getting called twice.

from knockout-amd-helpers.

rniemeyer avatar rniemeyer commented on July 28, 2024

The binding will depend on both the template and name in your case. Setting them sequentially will cause the binding to fire twice.

You could either set it all at once:

mainModule.module({
     template: yourTemplate, 
     name: yourName
});

or create a computed that you throttle that returns it as an object

module = ko.computed(function() {
    return {
          template: this.template(),
          name: this.name()
    };
}, this).extend({ throttle: 1 });

This does give me a thought to actually throttle it within the library itself, but would take some investigation/testing to understand how/what it could break.

As a final option, you could choose to make template a non-observable, and set it prior to setting name, if you don't need to ever just change the template.

Hope that helps!

from knockout-amd-helpers.

vishnurk avatar vishnurk commented on July 28, 2024

Thanks, Ryan.
As for the 1st option, I initially suspected that sequentially setting the 'template' and 'name' was causing the 'initializer' to fire twice - so, I matched the view & view model names and made only the module 'name' observable. The 'initializer' still fired twice ONLY when using the browser back button.

Also, another piece of information - I do have a navigation module (other than the main module) defined on the main HTML page which is dynamically bound as well depending on the route. Not sure at this point if this could be causing this issue.

Will give your recommendations a try and get back to you.

Thanks again.

from knockout-amd-helpers.

vishnurk avatar vishnurk commented on July 28, 2024

Quick update on this issue - It was the sequential setting of the module init data (after setting template & name) that caused the 'initializer' to fire twice. Got past it by setting template,name & data all at once as recommended by you. It's definitely much cleaner than setting them separately.

Thanks again for your help.

from knockout-amd-helpers.

rniemeyer avatar rniemeyer commented on July 28, 2024

@vishnurk - great. glad that you got it settled. Let me know if you run into any additional problems/issues.

from knockout-amd-helpers.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.