Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>i was looking for a drag n drop exemple and find yours, i updated the code slightly to 1.0.0-rc5 and add a double click on item ability for fun ... </p> <p><a href="http://jsfiddle.net/kadactivity/hhBrM/1/" rel="nofollow">http://jsfiddle.net/kadactivity/hhBrM/1/</a></p> <p><strong>Handlebars</strong></p> <pre><code>&lt;script type="text/x-handlebars" &gt; &lt;b&gt;Available Products&lt;/b&gt; &lt;br /&gt;&lt;br /&gt; {{#each product in model}} {{#view App.ProductView contentBinding="product"}} {{view.content.name}} {{/view}}&lt;br /&gt; {{/each}} &lt;hr /&gt; {{#view App.ProductDropTarget dragContextBinding="currentDragItem"}} Shopping Cart &lt;div style="height: 20px"&gt;{{helpText}}&lt;/div&gt; {{/view}} &lt;br /&gt; {{#each cart in productsInCart}} {{#view App.ProductView contentBinding="cart"}} {{view.content.name}} {{/view}}&lt;br /&gt; {{/each}} &lt;/script&gt; </code></pre> <p><strong>Javascript</strong></p> <pre><code>App = Ember.Application.create(); App.Router.map(function() { // put your routes here }); App.ApplicationRoute = Ember.Route.extend({ model: function() { return [ App.Product.create({ name: "MacBook Pro", isAdded: false }), App.Product.create({ name: "iPhone", isAdded: false }), App.Product.create({ name: "iPad", isAdded: true }), App.Product.create({ name: "iTV", isAdded: false }) ]; } }); DragNDrop = Ember.Namespace.create(); DragNDrop.cancel = function(event) { event.preventDefault(); return false; }; DragNDrop.Draggable = Ember.Mixin.create({ attributeBindings: "draggable", draggable: "true", dragStart: function(event) { var dataTransfer = event.originalEvent.dataTransfer; dataTransfer.setData("Text", this.get("elementId")); } }); DragNDrop.Droppable = Ember.Mixin.create({ dragEnter: DragNDrop.cancel, dragOver: DragNDrop.cancel, drop: function(event) { event.preventDefault(); return false; } }); App.Product = Ember.Object.extend({ name: null, isAdded: null }); App.ProductView = Ember.View.extend(DragNDrop.Draggable, { tagName: "span", // .setDragImage (in #dragStart) requires an HTML element as the first argument // so you must tell Ember to create the view and it"s element and then get the // HTML representation of that element. dragIconElement: Ember.View.create({ attributeBindings: ["src"], tagName: "img", src: "http://twitter.com/api/users/profile_image/twitter" }).createElement().get("element"), dragStart: function(event) { this._super(event); // Let the controller know this view is dragging this.set("content.isDragging", true); // Set the drag image and location relative to the mouse/touch event var dataTransfer = event.originalEvent.dataTransfer; dataTransfer.setDragImage(this.get("dragIconElement"), 24, 24); }, dragEnd: function(event) { // Let the controller know this view is done dragging this.set("content.isDragging", false); }, doubleClick: function(event) { this.set("content.isAdded", !this.get("content.isAdded")); } }); App.ProductDropTarget = Ember.View.extend(DragNDrop.Droppable, { tagName: "div", classNames: ["dropTarget"], classNameBindings: ["cartAction"], helpText: null, // This will determine which class (if any) you should add to // the view when you are in the process of dragging an item. cartAction: function() { if(Ember.isEmpty(this.get("dragContext"))) { this.set("helpText","(Drop Zone)"); return null; } if(!this.get("dragContext.isAdded")) { this.set("helpText", "(Drop to Add)"); return "cart-add"; } else if(this.get("dragContext.isAdded")) { this.set("helpText", "(Drop to Remove)"); return "cart-remove"; } else { this.set("helpText", "(Drop Zone)"); return null; } }.property("dragContext"), drop: function(event) { var viewId = event.originalEvent.dataTransfer.getData("Text"), view = Ember.View.views[viewId]; // Set view properties // Must be within `Ember.run.next` to always work Ember.run.next(this, function() { view.set("content.isAdded", !view.get("content.isAdded")); }); return this._super(event); } }); App.ApplicationController = Ember.ArrayController.extend({ currentDragItem: function() { return this.findProperty("isDragging", true); }.property("@each.isDragging"), productsInCart: function() { var cartItems = this.filterProperty("isAdded", true); console.log(cartItems); if(!Ember.isEmpty(cartItems)) { // Sort desc by name return cartItems.sort(function(a,b){ if((a.get("name").toLowerCase()) &lt; (b.get("name").toLowerCase())) return -1; else return 1; }); } }.property("@each.isAdded") }); </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload