watched attributes (and text)

This commit is contained in:
Torsten 2023-02-07 18:34:19 +02:00
parent 6aa874fe64
commit e93e4b0e38
3 changed files with 26 additions and 17 deletions

View File

@ -30,12 +30,13 @@ module VueR
end end
def track( key ) def track( key )
puts "Tracking:#{key}-#{@effect.hash}"
return unless @effect return unless @effect
subs = get_subscribers_for(key) get_subscribers_for(key) << @effect
subs << @effect
end end
def trigger(key) def trigger(key)
puts "Trigger:#{key}"
effects = get_subscribers_for(key) effects = get_subscribers_for(key)
effects.each {|effect| effect() } effects.each {|effect| effect() }
end end

View File

@ -2,7 +2,7 @@ require 'opal-parser'
module VueR module VueR
class Mounter class Mounter
HANDLEBARS = /{{\s?([^}]*)\s?}}/
def initialize( id , app ) def initialize( id , app )
@root = $document[id] @root = $document[id]
@application = app @application = app
@ -14,34 +14,43 @@ module VueR
def mount_Text elem def mount_Text elem
text = elem.text text = elem.text
puts text scan = text.scan(HANDLEBARS)
scan = text.scan(/{{\s?([^}]*)\s?}}/)
return unless scan.length > 0 return unless scan.length > 0
puts scan
raise "only one curly per text implemented not:#{scan.length}" if scan.length > 1 raise "only one curly per text implemented not:#{scan.length}" if scan.length > 1
match = text.match(/{{\s?([^}]*)\s?}}/) match = text.match(HANDLEBARS)
str_before = text[0 ... match.begin(0)] str_before = text[0 ... match.begin(0)]
str_after = text[ match.end(0) .. -1] str_after = text[ match.end(0) .. -1]
ruby = match[0][2 ... -2] ruby = match[0][2 ... -2]
puts "Text: #{ruby}"
proc = Proc.new do proc = Proc.new do
elem.text = str_before + @application.eval(ruby).to_s + str_after elem.text = str_before + @application.eval(ruby).to_s + str_after
end end
proc.call @application.watch_effect(proc)
end end
def mount_Element(elem) def mount_Element(elem)
elem.attributes.each do |name , value| elem.attributes.each do |name , value|
puts "Name: #{name}" mount_attribute(elem , name) if name.start_with?("r_")
puts "Value: #{value}" mount_event(elem , name) if name.start_with?("e_")
puts "DYN" if name.start_with?("r_")
puts "EVENT" if name.start_with?("e_")
end end
elem.children.each do |elem| elem.children.each do |elem|
base_name = elem.class.to_s.split("::").last base_name = elem.class.to_s.split("::").last
send "mount_#{base_name}" , elem send "mount_#{base_name}" , elem
end end
end end
def mount_attribute(element , name)
native_name = name[2 .. -1]
ruby = element[name]
old_value = element[native_name]
puts "Attribute: #{ruby}"
proc = Proc.new do
element[native_name] = old_value + " " + @application.eval(ruby).to_s
end
@application.watch_effect(proc)
end
def mount_event(element , name)
puts "Event: #{name}"
end
end end
end end

View File

@ -1,11 +1,10 @@
#app #app
.flex.justify-center .flex.justify-center
= @image.attributes = #@image.attributes
.flex.justify-center .flex.justify-center
%a.underline{ e_click: "hi"} %a.underline{ e_click: "hi" , r_class: 'bg'}
{{ bg }} {{ bg }}
%div{ r_class: 21 }
:opal :opal
class Clicker < VueR::Application class Clicker < VueR::Application