1

If i have an object that is draggable and has Start, Drag, Stop functions, how would i trigger them?

the issue that i am thinking would cause issues is the arguments passed into the function.

I wasn't sure if there was an easy way to do this, if there was some sort of simulation that can be used, or if i should create a custom object in which would be passed into the e and ui function attributes.

I would write code, but it is as simple as:

Code:

var $a = $("<div />").append("Test Text").appendTo("body").draggable({
    start: function(e,ui){/*...*/},
    drag: function (e,ui){/*...*/},
    stop: function (e,ui){/*...*/}
});

I wanted to call the functions, and originally i was thinking:

$a.draggable("option", "start")();
$a.draggable("option", "drag")();
$a.draggable("option", "stop")();

I just did a quick code walk, to see which arguments were being used where.

start:   ui.position.top|left
drag:    ui.position.top|left
stop:    ui.position.top|left, eventArgs.ClientX, eventArgs.ClientY, eventArgs.target

All of these have things like scope, and this, but since it is properly attached to something calling a click would be different then just calling the function without a reference to the object at hand.

After looking at the functions, I SHOULD be able to just create objects to pass into the trigger OR function as i have fired it in the above statement.

Fallenreaper
  • 10,222
  • 12
  • 66
  • 129
  • How is it different than your previous question: http://stackoverflow.com/questions/22152604/simulating-jquery-drag-programmatically ??? – A. Wolff Mar 03 '14 at 17:48
  • Whats your goal? What are you trying to accomplish with the trigger? Maybe a fiddle demonstrating what your after would help. – Trevor Mar 03 '14 at 17:55
  • Well, the thing that i was thinking of is that trigger applies arguments to the function called. This is fine, but i am unsure how to create the E, and UI objects correctly to pass them into the arguments. Granted, to me, since im not using all of them, i could sort of pick and choose the values in the object by reading and figure out which the function itself make use of. – Fallenreaper Mar 03 '14 at 18:26
  • No, this is not working for me, partially because I dont define the correct attributes, other reason MIGHT be because of the scope. I dont think there are scope issues though with the functions (as that would be bad programming) – Fallenreaper Mar 03 '14 at 18:28
  • Put your code in one of these http://www.sitepoint.com/7-code-playgrounds/ for better inspection. – Remigijus Pankevičius Mar 03 '14 at 20:49

1 Answers1

3

So at first, the idea was Trigger. It will call the events associated with the Listener called.

The second option i was told was apply. When doing an inline js apply though, there needs to be a scope.... because the scope is wrong when calling it in an arb. line of code. This wouldnt refer to the textbox being moved but the function container executing it.

I first build a list of args used, as you saw above and just applied that given the scope of the draggable object.

var s = [null,{position:{top:0,left:0}}],
    d = [null,{}position:{top:0,left:0}],
    sto = [{ClientX:0, ClientY:0, target:""},{]]
$("#my_item").draggable("option", "start").apply($("#my_item"), s);
$("#my_item").draggable("option", "drag").apply($("#my_item"), d);
$("#my_item").draggable("option", "stop").apply($("#my_item"), sto);

This answer does work, as it passes in the correct scope and all the data needed. I wasnt sure if there were any needed hidden functions, etc that i was unaware of.

Fallenreaper
  • 10,222
  • 12
  • 66
  • 129