Skip to content
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

[3.x] Async callback refactoring #1313

Merged
merged 10 commits into from
Oct 27, 2024
Merged

Conversation

dev-lnk
Copy link
Contributor

@dev-lnk dev-lnk commented Oct 26, 2024

Refactoring AsyncCallback

FormBuilder::make(route('form.async'))->fields([
    Text::make('Name')
])
    ->async(
        callback: AsyncCallback::with(
            beforeRequest: 'myBeforeRequest',
            responseHandler: 'myResponseHandler',
            //Called after standard response processing if responseHandler is not specified
            afterResponse: 'myAfterResponse'
        )
),
document.addEventListener("moonshine:init", () => {
    MoonShine.onCallback('myBeforeRequest', function(form, component) {
        console.log('myBeforeRequest', form, component)
    })

    MoonShine.onCallback('myResponseHandler', function(data, form, events, alpineComponent) {
        console.log('myResponseHandler', data, form, events, alpineComponent)
    })

    // Called after standard response processing if responseHandleris not specified
    MoonShine.onCallback('myAfterResponse', function(data, messageType) {
        console.log('myAfterResponse', data, messageType)
    })
})

Now the fragment has access to events and all the capabilities of AsyncCallback

Fragment::make([
    FlexibleRender::make('<p> Test: ' . time() . '</p>')
])
    ->updateWith(
        events: [
            AlpineJs::event(JsEvent::FRAGMENT_UPDATED, 'fg-step-2'),
        ],
        callback: AsyncCallback::with(afterResponse: 'fgAfter')
    )
    ->name('fg-step-1')
,
//...
ActionButton::make('FG')
    ->dispatchEvent([AlpineJs::event(JsEvent::FRAGMENT_UPDATED, 'fg-step-1')])
,
MoonShine.onCallback('fgAfter', function(data, messageType) {
    console.log('fgAfter', data, messageType)
})

@dev-lnk dev-lnk changed the title Async callback [3.x] Async callback refactoring Oct 26, 2024
rename all methods
@lee-to lee-to merged commit b231756 into moonshine-software:3.x Oct 27, 2024
5 checks passed
DissNik added a commit to DissNik/layouts-field that referenced this pull request Oct 30, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants