-
Notifications
You must be signed in to change notification settings - Fork 14
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support for component method as callback to diagram events #2
Comments
Yes, that's because of the scope those functions are being called in -- they are being called from DiagramComponent, not AppComponent, so this.buttonCallback is being evaluated as DiagramComponent.buttonCallback, which of course does not exist. The easiest thing to do here is define your buttonCallback function within your initDiagram function. |
But then this is limiting the Angular features. I tried to access data member of the component inside the working button callback method defined in the initDiagram, but again it gives Use caseThere may be cases where I need to use the data members for further actions like storing the received data in a variable to use somewhere else or open a popup modal, etc. Even the component's members are not accessible from inside the initDiagram. Also about the 2nd issue, what if I have to calculate the font-size or the margin, etc dynamically and then assign to the |
I see what you're saying. In modern Angular / React, data flow always moves down, from parent to child, so it is unusual to want to reference properties of a parent component (in your case, wanting to reference properties of AppComponent from DiagramComponent, which is where initDiagram is firing). Generally, all functions you depend on within initDiagram must be defined within initDiagram, as I said before. However, if there is some other data somewhere in your app that those functions must depend on, I recommend you store that in modelData, one of the Input properties DiagramComponent supports. This way, if you change modelData in your app, it will change in DiagramComponent's diagram, so then any function that references that property of modelData will also be effected. For example, say you want to console.log some property in your app when your buttonCallback fires. Let's call that property 'color.' Change / add that property in your modelData object that you are already binding to your DiagramComponent. In your stackblitz example, you could define it like: public diagramModelData = { prop: 'value', color: 'red' }; (Also, I see you bound 'familyData' to the [modelData] of your DiagramComponent, you should bind diagramModelData instead) Then, within initDiagram, define your buttonCallback as something like function buttonCallback(e, obj) { If anywhere in your app, you change the value of diagramModelData.color, that change will be reflected in your DiagramComponent's diagram.model.modelData.color, so your callback function will always reflect what your app data is |
@rjohnson465 Thanks for the explanation. The Updated the In my case, on click event, I want to open a modal to change the respected value.
In the above example, I have to access the May be this issue is related to |
Again, this is an issue of scope, and how generally, data flow goes from parent to child, not the other way around. One way to get around this is to set your click handler in your AppComponent's ngAfterViewInit() function. Make sure you have a reference to your DiagramComponent inside your AppComponent (by use of ViewChild) and then do something like
|
Thanks @rjohnson465 It's working this way and Here is what I have done
|
That's right, since you're calling appComp.buttonCallback, "this" will refer to AppComponent. Glad it's working for you! |
I was able to make it possible to access any property of my enclosing component inside initDiagram by using initDiagram.bind(this). |
Adding angular's component member function as callback to context menu button click is giving error
The stackblitz example: https://stackblitz.com/edit/angular-gojs-family-tree
I have added two menu for explanation where first menu is working fine but the second one where callback method is used is not working.
I also tried to bind the callback to
this
context usingBut error is still there.
Same issue is being faced where external methods are being used to return some value to the diagram properties.
Ex.,
The text was updated successfully, but these errors were encountered: