JS How to check if a class variable changed?












0















I have a variable in a class that can change at any time? Is there a way that js can reactively check if it changed and if it did perform a function? I don't have control of the object, I can view it and modify it, but my code is not the only code that can do that.










share|improve this question

























  • You can manage the way this variable changes, like for example make any changes to this variable only through a certain function, in which you change the value, and do any other checks or changes you like in it.

    – Ahmed Hammad
    Nov 16 '18 at 0:13











  • I don't manage the variable, I can just view it.

    – user10011538
    Nov 16 '18 at 0:24











  • Well, one costly solution, is to run an interval every short perion of time, which checks on the value of this variable and act upon change. But don’t do this unless the change to the variable is frequent.

    – Ahmed Hammad
    Nov 16 '18 at 0:31













  • @user10011538 I provided you a Meteor specific answer below. Did it help you to solve your issue? If not please provide more detail so we can work this out.

    – Jankapunkt
    Dec 19 '18 at 9:32
















0















I have a variable in a class that can change at any time? Is there a way that js can reactively check if it changed and if it did perform a function? I don't have control of the object, I can view it and modify it, but my code is not the only code that can do that.










share|improve this question

























  • You can manage the way this variable changes, like for example make any changes to this variable only through a certain function, in which you change the value, and do any other checks or changes you like in it.

    – Ahmed Hammad
    Nov 16 '18 at 0:13











  • I don't manage the variable, I can just view it.

    – user10011538
    Nov 16 '18 at 0:24











  • Well, one costly solution, is to run an interval every short perion of time, which checks on the value of this variable and act upon change. But don’t do this unless the change to the variable is frequent.

    – Ahmed Hammad
    Nov 16 '18 at 0:31













  • @user10011538 I provided you a Meteor specific answer below. Did it help you to solve your issue? If not please provide more detail so we can work this out.

    – Jankapunkt
    Dec 19 '18 at 9:32














0












0








0








I have a variable in a class that can change at any time? Is there a way that js can reactively check if it changed and if it did perform a function? I don't have control of the object, I can view it and modify it, but my code is not the only code that can do that.










share|improve this question
















I have a variable in a class that can change at any time? Is there a way that js can reactively check if it changed and if it did perform a function? I don't have control of the object, I can view it and modify it, but my code is not the only code that can do that.







javascript meteor javascript-events meteor-blaze






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 16 '18 at 0:24







user10011538

















asked Nov 16 '18 at 0:08









user10011538user10011538

216




216













  • You can manage the way this variable changes, like for example make any changes to this variable only through a certain function, in which you change the value, and do any other checks or changes you like in it.

    – Ahmed Hammad
    Nov 16 '18 at 0:13











  • I don't manage the variable, I can just view it.

    – user10011538
    Nov 16 '18 at 0:24











  • Well, one costly solution, is to run an interval every short perion of time, which checks on the value of this variable and act upon change. But don’t do this unless the change to the variable is frequent.

    – Ahmed Hammad
    Nov 16 '18 at 0:31













  • @user10011538 I provided you a Meteor specific answer below. Did it help you to solve your issue? If not please provide more detail so we can work this out.

    – Jankapunkt
    Dec 19 '18 at 9:32



















  • You can manage the way this variable changes, like for example make any changes to this variable only through a certain function, in which you change the value, and do any other checks or changes you like in it.

    – Ahmed Hammad
    Nov 16 '18 at 0:13











  • I don't manage the variable, I can just view it.

    – user10011538
    Nov 16 '18 at 0:24











  • Well, one costly solution, is to run an interval every short perion of time, which checks on the value of this variable and act upon change. But don’t do this unless the change to the variable is frequent.

    – Ahmed Hammad
    Nov 16 '18 at 0:31













  • @user10011538 I provided you a Meteor specific answer below. Did it help you to solve your issue? If not please provide more detail so we can work this out.

    – Jankapunkt
    Dec 19 '18 at 9:32

















You can manage the way this variable changes, like for example make any changes to this variable only through a certain function, in which you change the value, and do any other checks or changes you like in it.

– Ahmed Hammad
Nov 16 '18 at 0:13





You can manage the way this variable changes, like for example make any changes to this variable only through a certain function, in which you change the value, and do any other checks or changes you like in it.

– Ahmed Hammad
Nov 16 '18 at 0:13













I don't manage the variable, I can just view it.

– user10011538
Nov 16 '18 at 0:24





I don't manage the variable, I can just view it.

– user10011538
Nov 16 '18 at 0:24













Well, one costly solution, is to run an interval every short perion of time, which checks on the value of this variable and act upon change. But don’t do this unless the change to the variable is frequent.

– Ahmed Hammad
Nov 16 '18 at 0:31







Well, one costly solution, is to run an interval every short perion of time, which checks on the value of this variable and act upon change. But don’t do this unless the change to the variable is frequent.

– Ahmed Hammad
Nov 16 '18 at 0:31















@user10011538 I provided you a Meteor specific answer below. Did it help you to solve your issue? If not please provide more detail so we can work this out.

– Jankapunkt
Dec 19 '18 at 9:32





@user10011538 I provided you a Meteor specific answer below. Did it help you to solve your issue? If not please provide more detail so we can work this out.

– Jankapunkt
Dec 19 '18 at 9:32












2 Answers
2






active

oldest

votes


















2














If you are the owner of this object you can use Proxy object on it to be used instead of original object. And this case you will have the total control on every single operation is produced with your object






share|improve this answer
























  • Sorry I did not mention this, I don't have absolute control of the object, I can view it and modify it, but my code is not the only code that can do that.

    – user10011538
    Nov 16 '18 at 0:25



















0














In Meteor (you have tagged it) you have the Tracker which autoruns a function if a reactive data source has been changed, no matter by whom or what.



Read: https://docs.meteor.com/api/tracker.html



Now in Blaze (which you have also tagged) you have also a Template internal Tracker:



Template.myTemplate.onCreated(function (){
const instance = this

instance.autorun(() => {
// this behaves like Tracker.autorun
})
})


How to to make a variable reactive?



If you access a reactive variable inside an autorun, you trigger the given function. A normal Object is not reactive. In Meteor you can store data using



ReactiveVar: https://docs.meteor.com/api/reactive-var.html



ReactiveDict: https://atmospherejs.com/meteor/reactive-dict



(Mini)mongo Cursor: https://docs.meteor.com/api/collections.html#mongo_cursor



Accessing data from these data stores triggers computations of your trackers.



Reading these docs should make it clear to understand the reactive concept of Meteor and you should be able to write a reactive Template.






share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53329611%2fjs-how-to-check-if-a-class-variable-changed%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    2














    If you are the owner of this object you can use Proxy object on it to be used instead of original object. And this case you will have the total control on every single operation is produced with your object






    share|improve this answer
























    • Sorry I did not mention this, I don't have absolute control of the object, I can view it and modify it, but my code is not the only code that can do that.

      – user10011538
      Nov 16 '18 at 0:25
















    2














    If you are the owner of this object you can use Proxy object on it to be used instead of original object. And this case you will have the total control on every single operation is produced with your object






    share|improve this answer
























    • Sorry I did not mention this, I don't have absolute control of the object, I can view it and modify it, but my code is not the only code that can do that.

      – user10011538
      Nov 16 '18 at 0:25














    2












    2








    2







    If you are the owner of this object you can use Proxy object on it to be used instead of original object. And this case you will have the total control on every single operation is produced with your object






    share|improve this answer













    If you are the owner of this object you can use Proxy object on it to be used instead of original object. And this case you will have the total control on every single operation is produced with your object







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 16 '18 at 0:19









    bva inbbva inb

    212




    212













    • Sorry I did not mention this, I don't have absolute control of the object, I can view it and modify it, but my code is not the only code that can do that.

      – user10011538
      Nov 16 '18 at 0:25



















    • Sorry I did not mention this, I don't have absolute control of the object, I can view it and modify it, but my code is not the only code that can do that.

      – user10011538
      Nov 16 '18 at 0:25

















    Sorry I did not mention this, I don't have absolute control of the object, I can view it and modify it, but my code is not the only code that can do that.

    – user10011538
    Nov 16 '18 at 0:25





    Sorry I did not mention this, I don't have absolute control of the object, I can view it and modify it, but my code is not the only code that can do that.

    – user10011538
    Nov 16 '18 at 0:25













    0














    In Meteor (you have tagged it) you have the Tracker which autoruns a function if a reactive data source has been changed, no matter by whom or what.



    Read: https://docs.meteor.com/api/tracker.html



    Now in Blaze (which you have also tagged) you have also a Template internal Tracker:



    Template.myTemplate.onCreated(function (){
    const instance = this

    instance.autorun(() => {
    // this behaves like Tracker.autorun
    })
    })


    How to to make a variable reactive?



    If you access a reactive variable inside an autorun, you trigger the given function. A normal Object is not reactive. In Meteor you can store data using



    ReactiveVar: https://docs.meteor.com/api/reactive-var.html



    ReactiveDict: https://atmospherejs.com/meteor/reactive-dict



    (Mini)mongo Cursor: https://docs.meteor.com/api/collections.html#mongo_cursor



    Accessing data from these data stores triggers computations of your trackers.



    Reading these docs should make it clear to understand the reactive concept of Meteor and you should be able to write a reactive Template.






    share|improve this answer




























      0














      In Meteor (you have tagged it) you have the Tracker which autoruns a function if a reactive data source has been changed, no matter by whom or what.



      Read: https://docs.meteor.com/api/tracker.html



      Now in Blaze (which you have also tagged) you have also a Template internal Tracker:



      Template.myTemplate.onCreated(function (){
      const instance = this

      instance.autorun(() => {
      // this behaves like Tracker.autorun
      })
      })


      How to to make a variable reactive?



      If you access a reactive variable inside an autorun, you trigger the given function. A normal Object is not reactive. In Meteor you can store data using



      ReactiveVar: https://docs.meteor.com/api/reactive-var.html



      ReactiveDict: https://atmospherejs.com/meteor/reactive-dict



      (Mini)mongo Cursor: https://docs.meteor.com/api/collections.html#mongo_cursor



      Accessing data from these data stores triggers computations of your trackers.



      Reading these docs should make it clear to understand the reactive concept of Meteor and you should be able to write a reactive Template.






      share|improve this answer


























        0












        0








        0







        In Meteor (you have tagged it) you have the Tracker which autoruns a function if a reactive data source has been changed, no matter by whom or what.



        Read: https://docs.meteor.com/api/tracker.html



        Now in Blaze (which you have also tagged) you have also a Template internal Tracker:



        Template.myTemplate.onCreated(function (){
        const instance = this

        instance.autorun(() => {
        // this behaves like Tracker.autorun
        })
        })


        How to to make a variable reactive?



        If you access a reactive variable inside an autorun, you trigger the given function. A normal Object is not reactive. In Meteor you can store data using



        ReactiveVar: https://docs.meteor.com/api/reactive-var.html



        ReactiveDict: https://atmospherejs.com/meteor/reactive-dict



        (Mini)mongo Cursor: https://docs.meteor.com/api/collections.html#mongo_cursor



        Accessing data from these data stores triggers computations of your trackers.



        Reading these docs should make it clear to understand the reactive concept of Meteor and you should be able to write a reactive Template.






        share|improve this answer













        In Meteor (you have tagged it) you have the Tracker which autoruns a function if a reactive data source has been changed, no matter by whom or what.



        Read: https://docs.meteor.com/api/tracker.html



        Now in Blaze (which you have also tagged) you have also a Template internal Tracker:



        Template.myTemplate.onCreated(function (){
        const instance = this

        instance.autorun(() => {
        // this behaves like Tracker.autorun
        })
        })


        How to to make a variable reactive?



        If you access a reactive variable inside an autorun, you trigger the given function. A normal Object is not reactive. In Meteor you can store data using



        ReactiveVar: https://docs.meteor.com/api/reactive-var.html



        ReactiveDict: https://atmospherejs.com/meteor/reactive-dict



        (Mini)mongo Cursor: https://docs.meteor.com/api/collections.html#mongo_cursor



        Accessing data from these data stores triggers computations of your trackers.



        Reading these docs should make it clear to understand the reactive concept of Meteor and you should be able to write a reactive Template.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 16 '18 at 6:56









        JankapunktJankapunkt

        4,09531835




        4,09531835






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53329611%2fjs-how-to-check-if-a-class-variable-changed%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            The Sandy Post

            Danny Elfman

            Pages that link to "Head v. Amoskeag Manufacturing Co."