How can I make use of Event Delegation in a parent Svelte Component - event-handling

In this example, if I click the Tutorial NavItem I get to the Tutorial
section. Here I forward an custom click event on every Nav Item, where I set the segment which has been clicked. In the app.svelte I pull in the NavItem component and set the segment to current, which tells the Nav Component (through Props), which segment is current and needs to be active.
My question is how Can i Make use of event Delegation, so the Nav Component listen for the click Event, so it doesn't feel so repetitiv
The minimal Example goes like that, I want the parent to handle the event?:
https://svelte.dev/repl/691e82ee168549c782b61c355e78cf9b?version=3.12.1
Here is a complex Ex. the Repl:
https://svelte.dev/repl/691e82ee168549c782b61c355e78cf9b?version=3.12.1

I see you already use the getContext and setContext functionality. Instead of firing the event you can simply update that context:
in NavItem.svelte
function setSegment(){
current.set(segment);
}

Related

How to re-direct events from one control to another control in PowerBuilder?

A button control can trigger event of a datawindow control with TriggerEvent() function.
The button control in my code was set as child object of DataWindow control with SetParent win32 API function. SetParent external function moves button from window to datawindow control but after SetParent the code that was already written for Clicked event is not working anymore. That is why i need to redirect the clicked event of button to buttonclicked event of datawindow.
There is good example of redirecting event by using win32 API calls. here is the link http://bitmatic.com/c/redirecting-mousewheel-events-to-another-control i need to do the same thing in PowerBuilder.
Can someone see that code or help me to redirect events the way i want?
You're doing things the hard way. Find the name of the datawindow control (e.g. dw_1), and from the command button just issue dw_1.event buttonclicked ( args ).
Better yet, move the code to a function in the parent object. Controls are navigation objects, they really shouldn't have too much code in them (IMHO), but fire off methods on the parent object.

How to trigger an event as soon as we launch the view in SWT

I have an RCP application where I am creating a view which invokes an instance of the composite.Now my problem is I want to trigger one event as soon as I show the view/composite in the screen.
I tried with addFocusListner(),addMouseTrackListener(),addMouseListner()but unfortunately none of the them gets the control as I move my mouse pointer around the view.Is there any way we can solve it?
Just grab the control you have used to create view and add selection listener on it.
for example if treeViewer is used to create that view then:
treeViewer.setSelection(new StructuredSelection(element),true);
Ensure selection by this treeViewer.getControl.setFocus();
If you want to trigger an event after your view is in focus/launch then you should add a listener to that view e.g IPartListener2for that you need to create a class and implement IPartListener2 interface. you will get more information here
Also if you want only using mouse event then you need to add MouseMoveListener to your view so when mouse pointer on your when mouseMove will call.
e.g. control.addMouseMoveListener(this);

Get notified when user jumps to a marker (annotation) in Eclipse

I'm currently writing a plugin for the Eclipse IDE. In this plugin, I defined my own type of resource marker (IMarker). Using the standard Eclipse means like the "Next annotation"/"Previous annotation" buttons, the user has the possibility to navigate between these markers. I also wrote a view which shows some detail information for a single marker. This view shall be updated when the user navigates to a marker. Can I register some kind of listener/observer that will be notified when the user selects/jumps to a marker? If so, how? If not, what are my alternatives?
The Next Annotation action ends up calling the ITextEditorExtension4.gotAnnotation method. The usually implementation for this is in AbstractTextEditor. This just calls finds the annotation and calls the selectAndReveal method.
So there does not seem to be any special listener you can use for this. Normal selection events should be generated so you could use the ISelectionService selection listener but you will have to work out if the selection is for your marker.

Best way to update view from command or filedialog in an eclipse rcp application

In my application I have a menu which open a SelectionDialog, this dialog is used to choose an object.
When this object is selected I have to display it in the view.
What is the best way to update my view?
Currently, I call myview.update(object) after the dialog is closed (in the handler of the menu). But I think this solution is not well design.
I have read about update my model and notify my view but my model does not change (no data are changed, I only display different Data ).
Does anyone has some ideas for a well design solution ?
Define model listener ( dataPopulated(Event e))
Make your view implement model listener and register it with the Model.
Define a Model class that can contain the objects that you want to populate in the view
When Model.setInput(Object input) is invoked fire dataPopulated() event on all registered model listeners.
Above steps works properly when you have view activated. You need to consider cases like when if view is deactivated or not visible ( make sure you refresh view is visible else you will have unnecessary overhead of refreshing view though it is notvisible)
Try adding a selection listener in the view and register this selection in the dialog.
In the listener action, add the code to show the selected object.

VM role in MVVM - should it handle everything and why?

Where exactly is the limit to adopt VM so it can suite better a particular View? Example:
There should be a command in UI (ex button) that should allow adding new item. Additional requirement can be that new item should be selected, ensured that its visible on control (lets say TreeView control), and to begin edit on the newly added item (in order to change predefined value that was set in VM). Lets assume that control doesn't have automatic mechanism to achieve this, so we need to do it manually. So the execution flow looks like this:
invoke add command on VM - done is View's xaml.
set SelectedItem to new item (usually we bind control's SelectedItem property to VM's CurrentItem property, and then just assign new item to CurrentItem.
ensure that new item is visible on control - this must be done in View's code behind.
Start editing - this must be done in View's code behind.
Now, since everywhere on net there are articles on using messages for almost everything, a question:
What do I break if I do it in the simple old fashion way? I use Click event instead of Command binding on adding new item, and in the method I do this:
// in View's Click event handler
ViewModel.AddCommand.Execute(null);
EnsureVisibleSelectedItem();
BeginEdit();
.. clean and clear! And what do I gain if I do it using messages:
// in ViewModel's AddCommand
AddNewItem();
SetCurrentItem();
SendMessageToEnsureVisibleSelectedItem();
SendMessageToBeginEditSelectedItem();
... where View has registered to receive these two messages.
Any light on this is greatly appreciated. To my opinion, UI can change, and VM should be able to adopt new UI without making changes to itself, so I dont quite understand current MVVM policy that is preached on internet.
I would say "make it simple".
What's really important in MVVM is:
what doesn't depend on the view should go in the ViewModel (your ViewModel must not be aware of the view in any way - not just by object reference)
everything else in the View and its code-behind.
Yes, in its code-behind. There's nothing wrong in writing code-behind if it is code that is related to the view, not logic. For instance, drag & drop management should be written in the code-behind.
To answer your question, you do not break anything in writing:
// in View's Click event handler
ViewModel.AddCommand.Execute(null);
EnsureVisibleSelectedItem();
BeginEdit();
Everything that is not related to the view is in the ViewModel, everything else in the View/code-behind. That's just fine.
No if I look at your second example:
// in ViewModel's AddCommand
AddNewItem();
SetCurrentItem();
SendMessageToEnsureVisibleSelectedItem();
SendMessageToBeginEditSelectedItem();
AddNewItem is OK (not related to the view), SetCurrentItem is OK (not related to the view), but what about SendMessageToEnsureVisibleSelectedItem and SendMessageToBeginEditSelectedItem?
EnsureVisible is typically useful for a treeview, but what if your view wasn't built with a treeview? What if the control would automatically make the new selected item visible? Of course you could ignore the message, but you would have written some useless code in ViewModel because you thought your view would need it for UI display.
You have typically written here some code in the ViewModel that is aware of how the View should be working. Yes, you have reduced the number of lines in the code-behind, but you definitely have broken the pattern.
Your "old fashion way" is actually a good way for your needs. Your ViewModel is not aware of the view, that's what's important.