ExampleClass = Class.create();
ExampleClass.prototype = {
initialize: function(elementId,dragObject) {
this.element = $(elementId);
this.dragObject = dragObject;
this.dragElement = this.dragObject.element.id;
this.dragOptions = this.dragObject.options;
this.someEvent = this.eventHandler.bindAsEventListener(this);
Event.observe(this.element, 'mousedown', this.someEvent);
},
eventHandler: function(event) {
this.killDraggable();
this.someEndEvent = this.endEventHandler.bindAsEventListener(this);
Event.observe(this.element,'mouseup',this.someEndEvent);
},
endEventHandler: function(event) {
Event.stopObserving(this.element,'mouseup',this.someEndEvent);
this.makeDraggable();
},
makeDraggable: function() {
this.dragObject = new Draggable(this.dragElement,this.dragOptions);
},
killDraggable: function() {
this.dragObject.destroy();
}
}
So we pass as an option to the class creation, the drag object . . . for instance if we had the drag object:
testExampleDiv.draggableObject = new Draggable('elementid',{options});
We would pass testExampleDiv.draggableObject to our class. The initialization function then grabs the elementId and the options passed in the initial Draggable object creation from the draggable object and saves it. At the beginning of the event, the script kills the draggable object, and then at the end of the event the script reinvokes the draggable object, passing the elementId of the original object, and the original options object to the Draggables initialization function.
The above example uses prototype, which is a safe assumption if you're using scriptaculous . . .
Enjoy!
No comments:
Post a Comment