0

I have a method to close bootstrap cards. These cards are loaded in the webpage based on an object array this.unratedjobs. Upon closing individual cards I remove each card's objects(Elements) form the object array this.unratedjobs. While removing these cards, I play a lil' animation using JQuery hide function.

Now, my problem is I can't update the global variable inside the callback function, even if I find a way to access it.

As per my findings, I can copy the global variable into the local scope and manipulate but I can't make it affect the global ones.

onDiscardJobCard(jobId?){

        if(!jobId){
            jobId = this.alertMessageModal.instance.result;
            this.addToRateAnotherTime(jobId);
        }

        //This is assigning a global variable to a local one
        var rr = this.unratedJobs;

        $('#rating-section-' + jobId).hide('drop', {direction: "left"}, 'slow', function() {

            $('#rating-section-' + jobId).remove();

            rr = _.filter(rr,  r=> r.jobId != jobId);

        });

        this.alertMessageModal.dismiss();

        if(this.unratedJobs && this.unratedJobs.length == 0){
            this.onDiscardModal.emit();
        }
    }

I have tried many ways but none worked successfully.

Kirk
  • 4,957
  • 2
  • 32
  • 59
  • global variable is accessible everywhere. – Negi Rox Apr 24 '19 at 11:53
  • 3
    Replace `function()` with `() =>` and your `this` remains the class instance. Your method works in general, but you need to store `this` in a variable (like `var that = this;`, then inside `that.unratedJobs = ...`) –  Apr 24 '19 at 11:53
  • Also, good practise is to use let and const instead of var. – Alator Apr 24 '19 at 11:55
  • @Quentin This question is a dupe of https://stackoverflow.com/questions/20279484/how-to-access-the-correct-this-inside-a-callback, not that other famous one, I believe. –  Apr 24 '19 at 11:56
  • Try just at the very beginning and outside of every function to declare `var variable = 0;` than inside of function don't write `var` anymore just do the changes u need! – Flamur Beqiraj Apr 24 '19 at 11:56
  • @NegiRox no, not in this case when you tried to access within callback func. If I restore rr variable with this.unratedjobs it will be an undefined variable. – Kirk Apr 24 '19 at 11:57
  • @ChrisG - Could be both, but while the code is having issues accessing `unratedJobs` inside the callback function, the fundemental problem is that the callback is called when the animation finishes which is after the `if` statement at the end runes. – Quentin Apr 24 '19 at 12:00
  • @ChrisG, Shorthand function definition works in this case. I can now able to access the variable. Could you explain why it didn't before? I mean both are same function except one being just a shoter version of it? – Kirk Apr 24 '19 at 12:03
  • Arrow functions keep the `this` context. It's why they were added to JS in the first place, basically. –  Apr 24 '19 at 12:03
  • That's new for me, I thought arrow function is just a shortcut. It seems like its lil more than that. Gotta work on my ES6 lil more. Good save :) – Kirk Apr 24 '19 at 12:05
  • this.unratedJobs is not in global scope. – Negi Rox Apr 24 '19 at 12:07
  • @NegiRox well, I didn't specify here, but in the whole class it is a member variable. – Kirk Apr 24 '19 at 12:08
  • 1
    you are playing with local variable. after modification its value is gone. – Negi Rox Apr 24 '19 at 12:10
  • 1
    replace this var rr = this.unratedJobs; to var rr=this. then rr.unratedJobs = _.filter(rr.unratedJobs, r=> r.jobId != jobId); – Negi Rox Apr 24 '19 at 12:11

0 Answers0