Browse Source

allow named handlers to bind/rebing on global objects

DXT 4 years ago
parent
commit
2460ba2d41
1 changed files with 8 additions and 0 deletions
  1. 8 0
      src/actions/action_bind.erl

+ 8 - 0
src/actions/action_bind.erl

@@ -7,5 +7,13 @@
 -define(B(E), nitro:to_binary(E)).
 -define(T(T), wf_event:target(T)).
 
+render_action(#bind{postback=Code,target={g,_}=Control,type=Type,source=Src}) ->
+    % rebind same control to same handler on same target
+    G = ?T(Control), V = ?B(Src), E = ?B(Type),
+
+    ["{ if('",V,"' in ",G,"){",G,".removeEventListener('",E,"',",V,");delete ",G,"[",V,"]};",
+        V,"={handleEvent:(event) => { if(event.type === '",E,"'){",?B(Code),"}}};",
+        G,".addEventListener('",E,"',", V,");", G,".",V,"=",V,";}"];
 render_action(#bind{postback=Code,target=Control,type=Type}) ->
+    % check global action. unbind first
     ["{var x=",?T(Control),"; x && x.addEventListener('",?B(Type),"',function(event){",?B(Code),"});}"].