Event Jquery UI in dart

Issue

I’m trying to get Event object in listener function of a Jquery Ui Element.
I’m actually programming in dart language and use the js package (dart:js)
Here is an example :

Example.html :

<script async type="application/dart" src="Example.dart"></script>
<script async src="packages/browser/dart.js"></script>
<link rel="stylesheet" href="//code.jquery.com/ui/1.11.1/themes/smoothness/jquery-ui.css">
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script src="//code.jquery.com/ui/1.11.1/jquery-ui.js"></script>

<div id="container">
  <canvas id="stage" width="500px" height="500px"></canvas>
</div>
<div id="menu">
  <button id="btnImport">Button Import</button>
  <div id="slider-1"></div>
</div>

Example.dart :

import 'dart:html';
import 'dart:js' as js;

void main() {
  js.JsObject slider_1 = js.context.callMethod("\$", ['#slider-1']);
  slider_1.callMethod('slider');
  slider_1.callMethod("on",["slidechange",_onSliderChange]);
}

void _onSliderChange(js.JsObject obj1,js.JsObject obj2) {

}

How i retrieve the Event Object of slidechange event in _onSliderChange function?
JsOjbect don’t really help me…
Ty

Solution

If I understand correctly, you cannot retrieve the event itself but just a js version of a jquery event (not native dom event) so you need to know how it looks like to read the property you want according to the doc in http://api.jqueryui.com/slider/#event-change

void _onSliderChange(js.JsObject obj1, js.JsObject obj2) {
  print(obj1['target']); // <= get the div
  print(obj2['value']);  // <= get the value
}

which should display, as you move the slider, something like

div (:1)
5 (:1)

As you said, it is sometimes hard to deal/debug JsObject so I wrote some helpers to convert such object to dart collection (list or map) for debugging purpose here https://github.com/alextekartik/tekartik_utils.dart/blob/master/lib/src/js_utils/jsobject_converter.dart

void _onSliderChange(js.JsObject obj1,js.JsObject obj2) {
  print(jsObjectKeys(obj1));
  print(jsObjectAsCollection(obj1));
}

which should display stuff like

[originalEvent, type, isDefaultPrevented, timeStamp, jQuery110207535289276856929, target, toElement, screenY, screenX, pageY, pageX, offsetY, offsetX, fromElement, clientY, clientX, buttons, button, which, view, shiftKey, relatedTarget, metaKey, eventPhase, currentTarget, ctrlKey, cancelable, bubbles, altKey, delegateTarget, handleObj, data, isTrigger, namespace, namespace_re, result] (:1)
{originalEvent: {originalEvent: Instance of 'MouseEvent', type: mouseup, isDefaultPrevented: {}, timeStamp: 1409328507659, jQuery110207535289276856929: true, toElement: span, screenY: 624, screenX: 115, pageY: 545, pageX: 110, offsetY: 16, offsetX: 15, fromElement: null, clientY: 545, clientX: 110, buttons: null, button: 0, which: 1, view: <window>, target: span, shiftKey: false, relatedTarget: null, metaKey: false, eventPhase: 3, currentTarget: Instance of 'HtmlDocument', ctrlKey: false, cancelable: true, bubbles: true, altKey: false, delegateTarget: Instance of 'HtmlDocument', handleObj: {type: mouseup, origType: mouseup, data: null, handler: {guid: 23}, guid: 23, selector: null, needsContext: null, namespace: slider}, data: null}, type: slidechange, isDefaultPrevented: {}, timeStamp: 1409328507659, jQuery110207535289276856929: true, target: div, toElement: span, screenY: 624, screenX: 115, pageY: 545, pageX: 110, offsetY: 16, offsetX: 15, fromElement: null, clientY: 545, clientX: 110, buttons: null, button: 0, which: 1, view: <window>, shiftKey: false, relatedTarget: null, metaKey: false, eventPhase: 3, currentTarget: div, ctrlKey: false, cancelable: true, bubbles: true, altKey: false, delegateTarget: div, handleObj: {type: slidechange, origType: slidechange, data: null, handler: {guid: 17}, guid: 17, selector: null, needsContext: null, namespace: }, data: null, isTrigger: 3, namespace: , namespace_re: null, result: null} (:1)

where you can likely find the property you want from the event

Answered By – alextk

Answer Checked By – Cary Denson (FlutterFixes Admin)

Leave a Reply

Your email address will not be published.