Event Handlers - Legacy and Dynamic Update

Hello everyone,

Recently there was an update for the event handlers, now they are legacy and dynamic. See here

After this update there is an issue:

Upon experimenting, I can confirm that the repeater item selector $item does not select the correct item from the database.

For example, $item(‘#examsessionsDB’).getCurrentItem().fieldName always returns the value of the first item of repeater.

Code example:

$w('#sessionsrepeater').onItemReady(($item, itemData) => {
    console.log($item('#examsessionsDB').getCurrentItem().fieldName) //needs to log the corresponding repeater item, but always return the first item
})

Moreover, the legacy way does the same:

export function register_click(event) {
    let $item = $w.at(event.context)
  console.log($item('#examsessionsDB').getCurrentItem()) //needs to log the corresponding repeater item, but always return the first item
}

Please, help me to get this sorted.

Thank you in advance!

You’re using getCurrentItem wrong, accessing it through the repeater’s $item would not matter, it’s outside the repeater anyway, that’s the dataset isn’t it?

Just use console.log(itemData.fieldName)

Dear @DeanAyalon ,

Thank you for your response.

I am sure it has to work like that, as I have already built more than 3 websites and used that on multiple places and it works how it should.

Check the 2nd example, this one:

Isn’t it the correct approach? I am sure it is and it just sopped working, Right now I have a website on which it works, both for the first and 2nd examples.

Again, let me confirm - Is #examsessionsDB a Dataset?

This dataset only exists once, it does not repeat with the repeater, so no matter what you do, $item('#examsessionsDB') returns the same element - The dataset

When running getCurrentItem() on a dataset showing multiple items, it will simply return the first one

That is definitely not what you wanted is it?

It does not matter how many websites you built using some tool incorrectly, even if it has worked previously

Now, if you want to get the data of an item in the repeater, it is passed as the second argument to the function you give the event handler

$w('#someRepeater').onItemReady(($item, itemData) => { ... })

That’s the itemData you’re defining, just use it

If you don’t just want to log the item’s data, then let’s go back to the question - What is it you want to achieve?

Would you show me the websites in which this works and their code?

Dear @DeanAyalon,

Here is a code snippet from the same website which works as expected:

export function examsrepeater_itemReady($item) {
    if ($item('#examsessionsDB').getCurrentItem().format === "Digital") {
        if (wixWindow.multilingual.currentLanguage === "hy") {
            $item('#tooltip').tooltip = "Ուշ գրանցման վճար՝ Չի սահմանվում"
        }
        if (wixWindow.multilingual.currentLanguage === "en") {
            $item('#tooltip').tooltip = "Late entry fee: Not available"
        }
        if (wixWindow.multilingual.currentLanguage === "ru") {
            $item('#tooltip').tooltip = "Плата за позднюю регистрацию: Не доступен"
        }
    }
    if ($item('#examsessionsDB').getCurrentItem().format === "Paper-based") {
        dateconvert(new Date($item('#examsessionsDB').getCurrentItem().lateEntryDate))
            .then((date) => {
                if (wixWindow.multilingual.currentLanguage === "hy") {
                    $item('#tooltip').tooltip = "Ուշ գրանցման վճար՝ " + date
                }
                if (wixWindow.multilingual.currentLanguage === "en") {
                    $item('#tooltip').tooltip = "Late entry fee: " + date
                }
                if (wixWindow.multilingual.currentLanguage === "ru") {
                    $item('#tooltip').tooltip = "Плата за позднюю регистрацию: " + date
                }
            })
    }
}

You can check the screen recording of the live page here

Any thoughts on this?

Thank you in advance.

It’s intriguing that it works, I’ll give you that…
As for why, I don’t know, because it honestly shouldn’t :sweat_smile:

Anyway, have you tried using itemData instead? That is precisely what it was made for after all

Also,

Imagine you have a repeater, with a button in the repeater item, and you want to get that repeaters data on button click, this was the way to achieve it:

export function selectexambutton_click(event) {
    let $item = $w.at(event.context)
    console.log($item('#examregistrationsDB').getCurrentItem())
}

Now this is not working as well.

As well as, the new way:

$w('#selectexambutton').onClick((event) => {
    let $item = $w.at(event.context)
    console.log($item('#examsessionsDB').getCurrentItem())
})

not working either.

It should, it is in the documentation, as well as in older post questions, answered by Wix.

@DeanAyalon check this one, what is the right approach if not like this?

$w('#repeater').onItemReady(($item, itemData) => {
    $item('#button').onClick(someFunction)
})

Check this out:

Could you source where in the docs Wix shows $item('#dataset').getCurrentItem() as opposed to itemData please?

Interesting… Never seem that syntax… Maybe it can deduce the context using $item…

Still, have you tried using itemData?

Yes, okay, but see this:

Yes I did, it works, but now i just need to update all the codes in all the websites, beacuse they are stopping to work.

At least you got it working, that syntax makes more sense and uses a lot less computing anyway, so it’s a one-time change and you’re done

I really want the old way to work, and it should work, because these are in the documentation, and there is no announcement that it will stop working and that we need to update all the codes.

Agreed, still, if we look at what. actually happens, the item data is passed to the onItemReady function.
So it’s already there, therefore, using the getCurrentItem function for every item is redundant, and simply takes a lot of computation, running a function that retrieves data we already have

As for the syntax, I am not sure what may be causing it to not work on one page while functioning properly in another, but I’d stay away from it either way, especially as the whole static syntax is about to be deprecated

Yes, agreed.

Anyway, contacted Velo support, once I get answer, I will update here!

Thank you for your prompt replies and support, @DeanAyalon!