Why does @mixin only work for named models?
PGimenez opened this issue · comments
I'm trying to use the @mixing macro to extend the reactive model with fields from a struct. However, this only works when using a named model. For instance, this code will throw ERROR: UndefVarError:
field not defined
using GenieFramework
@genietools
@kwdef struct mixstruct
field::R{Bool} = false
end
@app begin
@out var1 = 0
@mixin mixstruct
@onbutton field begin
println("button clicked")
end
end
function ui()
btn("TEST", @click("field = true"))
end
@page("/", ui)
If I remove the @OnButton handler then the code runs fine, and if I inspect the model with @init
it does indeed have the field
from mixstruct
appended.
On the other hand, this code works with no issues
using GenieFramework
@genietools
@kwdef struct mixstruct
field::R{Bool} = false
end
@app Model begin
@out var1 = 0
@mixin mixstruct
@onbutton field begin
println("button clicked")
end
end
function ui()
btn("TEST", @click("field = true"))
end
route("/") do
model = Model |> init |> handlers
page(model, ui())
end
I've been looking at the code for @page
, @handler
and @mixin
but I don't see it. Only thing I've found is this suspicious line
Stipple.jl/src/ReactiveTools.jl
Line 611 in a72d761
It has to do with when the macro is executed. I don't have the time to look into it directly. But there is an easy workaround; just split the handlers off with the @handlers
macro:
@app Model begin
@out var1 = 0
@mixin mixstruct
end
@handlers begin
@onbutton field begin
println("button clicked")
end
end
That doesn't solve the issue when the model is unnamed 🥹, still getting ERROR: UndefVarError: field not defined
using GenieFramework
@genietools
@kwdef struct mixstruct
field::R{Bool} = false
end
@app begin
@out var1 = 0
@mixin mixstruct
end
@handlers begin
@onbutton field begin
println("button clicked")
end
end
function ui()
btn("TEST", @click("field = true"))
end
@page("/", ui)
Edit: If I paste the code in the REPL and do up()
to start a server it works. However when running the code from a file with include
it fails with the field
not defined error.
We have programmed the @in
, @out
and @mixin
macros with many side effects. The reason for this was that we wanted to keep the syntax for the app macro identical to that of the script style for which the macros where initially designed for.
The only place where I didn't use Core.eval()
is the one that now made the difficulties.
I think that this is successfully fixed in branch hh-mixin2. Please test before merging.