-
Notifications
You must be signed in to change notification settings - Fork 11
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Calling a user defined javascript function? #12
Comments
In fact I just found that it's possible to do this:
Is that the correct way? |
Ok so the above works for a native javascript function, but not for user-defined functions. Here is what I tried and which does not work:
https://jsbin.com/cagogafada/1/edit?html,output The button works, but not the slider. The error I get in the console is the following:
|
This may be pertinent to this issue: http://perfectionkills.com/global-eval-what-are-the-options/ |
Found a way by combining with the answer provided in #10 , it seems there is no way to bind to a callable (and pass the value), so the only way is add an <script src="https://unpkg.com/ink-components"></script>
<script type="text/javascript">
function show_msg() {
x = document.getElementsByName("x")[0];
// Alternative to get ink-var values, but it's more prone to error since it's not based on the variable's name
//x = document.getElementsByTagName("ink-var")[0];
alert("Working! x="+x.value);
}
</script>
<ink-var name="x" value="2"></ink-var>
X: <ink-range :value="x" bind="{x: value}" onclick=show_msg()></ink-range>, which is <ink-display name="x">2.0</ink-display>
<button type="button" onclick="show_msg()">Alert test</button> https://jsbin.com/hoxuxibinu/edit?html,output If there is a better way of doing this, I would be very interested in hearing it @rowanc1 :-) I think the ability to callback user-defined function would add a lot of flexibility to ink-components. |
(Also adding such an example in the documentation would be very helpful! :D) |
Another comment on the current version, the |
@rowanc1 That is AWESOME!!! I wholeheartedly agree with the change of Anyway the example in #10 and that I used here for inspiration is I think still useful in case when we need to use multiple variables to update a function, eg for a graph. Let's say I have 2 variables x and y, can the onchange of one widget provide the value of both? Eg: <script src="https://unpkg.com/ink-components"></script>
<script type="text/javascript">
function show_msg(x,y) {
alert("Working! x="+x.value+" y="+y);
}
</script>
<ink-var name="x" value="2"></ink-var>
<ink-var name="y" value="10"></ink-var>
X: <ink-range :value="x" :change="show_msg(x,y)"></ink-range>, which is <ink-display name="x">2.0</ink-display>
Y: <ink-range :value="y" :change="show_msg(x,y)"></ink-range>, which is <ink-display name="y">10.0</ink-display> Would this work, or then we need to fallback to the solution in #10 by fetching manually the variables from the DOM? /EDIT: or as you write in #13, through the exposed Please note I don't mind if it's not possible or it's too complex to implement, I'm fine with the exposed |
Huh, yeah, that seems to work too, nice when that happens! The I will also add some helper functions on For the var x = 2; // your variables (or other ink-vars)
function change(x, y){ return logToConsole(x, y); } // the change handler has it's own x |
That is just PERFECT! :D I can imagine so many things we can do with this simple scheme, awesome, very exciting!!! |
@stevejpurves just ran into this as well. The running PR in #13 is fixing things up and I will add to the docs as well. :) |
Now possible and documented! |
Is it possible to make an ink-component such as ink-range callback a user-defined javascript function?
I tried to do something like this but this doesn't work:
The text was updated successfully, but these errors were encountered: