question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

callbacks losing this reference

See original GitHub issue

I’ve used the GridsterItem.initCallback as well as the GridsterConfig.itemChangeCallback and both lose the this reference, even using a fat arrow.

` gridsterConfig.itemChangeCallback = (item: GridsterItem, itemComponent: GridsterItemComponentInterface) => {

console.log(this.changes); <-- this.changes is simply an array that is tracking individual items changes

this.changes.push(item); <-- for ease of testing, we simply push the gridster item

console.log(this.changes); <-- never has the other values.

}; `

Of course my code is a little more involved since i’m programming to an abstraction. I’m curious as to why you don’t make the callback an angular Output instead? Perhaps put a callback and/or Output for changes on the gridster-item component as well?

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

4reactions
ShiftySituationcommented, Nov 1, 2018

I wrote a directive to provide an output…

`import { Directive, Input, OnInit, EventEmitter, Output } from ‘@angular/core’; import { GridsterConfig, GridsterItem, GridsterItemComponentInterface } from ‘angular-gridster2’;

@Directive({ selector: ‘gridster’, }) export class GridsterChangeDirective implements OnInit { // tslint:disable-next-line:no-input-rename @Input(‘options’) options: GridsterConfig; @Output() itemChanged = new EventEmitter<GridsterItem>();

ngOnInit() {
    this.options.itemChangeCallback = (item: GridsterItem, itemComponent: GridsterItemComponentInterface) => {
        this.itemChanged.emit(item);
    };
}

} `

0reactions
tiberiuzuldcommented, Jul 6, 2021

Feature released in 12.1.0 Work done by @legrottagliegionata

Read more comments on GitHub >

github_iconTop Results From Across the Web

Javascript callbacks losing 'this' - Stack Overflow
Your problem is with this line: $('#some-element').click(this.doSomething);. Why this is a problem. JavaScript methods don't know anything ...
Read more >
What to do when “this” loses context - freeCodeCamp
In React components, this loses context when methods are used as callbacks for UI events. Consider the following component: class TodoAddForm ...
Read more >
JavaScript Callbacks Variable Scope Problem - Pluralsight
Since each function declares it's own scope, and i has a base atomic type ( int ) it is not passed as a...
Read more >
How to access the correct this inside a callback
3 methods for accessing the correct this inside a callback · 1. Use an arrow function · 2. Create another variable to store...
Read more >
Why is "this" lost when a method is used as a callback ... - Reddit
You are passing along a little snippet of JavaScript code, a reference to a new anonymous function. You can do anything you want...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found