);
-
- // Register the closure in the JS global scope
+
let handler_name = format!("handler_{}", input_id);
- let handler_name_global = handler_name.clone();
- let global = js_sys::global();
- Reflect::set(
- &global,
- &handler_name_global.into(),
- &closure.as_ref()
+ js_sys::Reflect::set(
+ &js_sys::global(),
+ &handler_name.clone().into(),
+ closure.as_ref(),
).unwrap();
+ closure.forget();
- // Typeahead initialization using jQuery
+ // Corrected initialization script using bracket notation for handler
let init_script = format!(
r#"
- (function() {{
- console.log('[TYPEAHEAD] Initializing for #{id}');
+ console.log('[JS] Starting Typeahead init for #{id}');
+ try {{
+ var bloodhound = window.bloodhoundInstance;
$('#{id}').typeahead(
{{
hint: true,
highlight: true,
minLength: 1
}},
- {dataset}
+ {{
+ name: 'suggestions',
+ source: bloodhound.ttAdapter(),
+ display: 'label',
+ templates: {{
+ suggestion: function(data) {{
+ console.log('[JS] Rendering suggestion', data);
+ return $('').text(data.label);
+ }}
+ }}
+ }}
).on('typeahead:select', function(ev, suggestion) {{
- console.log('[TYPEAHEAD] Select event triggered');
- {handler}(ev, suggestion);
+ console.log('[JS] Selection event received');
+ window['{handler}'](ev, suggestion);
}});
- console.log('[TYPEAHEAD] Initialization complete for #{id}');
- }})();
+ console.log('[JS] Typeahead initialized successfully');
+ }} catch (e) {{
+ console.error('[JS] Typeahead init error:', e);
+ }}
"#,
id = input_id,
- dataset = JSON::stringify(&dataset).unwrap(),
- handler = handler_name
+ handler = handler_name.replace('-', "_") // Replace hyphens to avoid JS issues
);
- log!("[TYPEAHEAD] Init script: {}", init_script);
- let _ = js_sys::eval(&init_script).unwrap();
- closure.forget();
+ log!("[RUST] Initialization script: {}", init_script);
+ if let Err(e) = js_sys::eval(&init_script) {
+ log!("[RUST] Eval error: {:?}", e);
+ }
}
\ No newline at end of file