3

What I need to do is subscribe to another file / object via knockout js and require js.

For example I have in File1.js:

define(
[
    '/path/to/some/component',
    'ko'
],
function (Component, ko) {
    "use strict";
    return Component.extend({

        // this is the var that I need to subscribe to in File2
        somevar: ko.observable(null);

        // I have an event that triggers this method in another 
        // external file that sets somevar
        myEvent: function(value){
           somevar(value);
        }
   });
});

and in file2.js I need to do something like

define(
[
    '/path/to/some/component',
    '/path/to/file#1',
    'ko'
],
function (Component, File1, ko) {
    "use strict";
    return Component.extend({

        // set a new observable var in file 2
        subscribeVar: ko.observable(null);

        initialize : function(){

            // and subscribe to the change in value of the var in 
            // file 1
            File1.somevar() .subscribe(function(anotherValue){
                // here I want to execute my logic but the    
                // illustrated method of doing this is not working.
            }, this);
        },
   });
});
  • I had the same issue, it helped me: this – OleksaNdr Aug 17 '17 at 13:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.