I'm trying to make a mock-up application for prototyping apps, using backbone & jQuery. It's pretty minimal at this point. http://jsfiddle.net/IgnorantUser/u7JkX/
I just have 2 models Canvas
and ButtonWidget
.
There are also 3 Views: the CanvasView
for the Canvas, ButtonInCanvas
view for the ButtonWidget
and a dummy view called ButtonInToolbar
which is not backed by the ButtonWidget model, instead it gets it's default values from a .button css class
and is used for dragging & dropping into the canvas, populating it with buttons.
Since there are gonna be other widgets (labels, textboxes, imageholders etc...) I've taken the approach of passing the drop event handling triggered by "CanvasView" in the context of "ButtonInToolbar" view, by using the following code:
passDrop: function (e, ui){
if ($(ui.draggable).parent().is('#toolbar')){
ui.draggable.trigger('drop'); //sets the drop in the context of the dragged view (ButtonInToolbar)
}
The issue I'm facing is that I wanna be able to pass the ui.position.left
and ui.position.top
values into the ButtonInToolbar
view, so that when I call the dropHandler method to append a new ButtonInCanvas
view, I can pass the position of the drop and render the Button where it actually dropped. How do I do that?
dropHandler: function () {
$('.canvas').append(new ButtonInCanvas({model: new ButtonWidget}).render().el);
}
I've tried passing (event, ui)
within the above function so that i can have the ui.position.left
value but I get "Uncaught TypeError: Cannot read property 'position' of undefined". How could I work around this issue?
Also, in the above code, I'm using a selector for the canvas instead of the CanvasView
instance. Having in mind that in the future there would be more than one CanvasView
, how could I associate the specific CanvasView
instance so that the Widgets append only into it and not in all DOM elements that have a class of canvas?
More importantly, since this is my first attempt with Backbone, and it doesn't have that much in common with "ToDo" apps, can you point out any fatal errors in my implementation? Things I'm doing wrong, or I could have done in a better/more efficient way