javascript - Ember.js draggable and droppable jqueryUI/native 拖放 mixin

标签 javascript jquery jquery-ui ember.js

我需要创建具有拖放和排序功能的项目。因此可以将一个项目拖到另一个项目中。

我见过一些解决方案,通过混入进行拖动并使用该混入创建可拖动 View ,然后通过可放置混入从可放置 View 创建另一个 View 。

但我需要每个项目/ View 都具有可拖动、可放置和可排序的功能。

谁能告诉我通过混合或子类化或...做到这一点的最佳方法?

我还可以创建一个 jqueryUi mixin 作为基础 mixin,然后在创建可拖动、可放置和可排序的 mixin 时使用该 mixin 吗?这可能吗?

最好使用 jqueryUI 还是 html5 拖放 api 或其他东西?

感谢帮助 瑞克

最佳答案

不确定您是否看到了 Katz 的代码 here ,但我正在使用它来制作 View :Droppable、Draggable、...或 jQuery UI 支持的任何其他交互。因此,您定义了一个基础 Mixin,您将在所有 jQuery UI 交互 Mixins 中使用它:

// Create a new mixin for jQuery UI widgets using the new SproutCore 2.0
// mixin syntax.
JQ.Base = Ember.Mixin.create({
  // When SproutCore creates the view's DOM element, it will call this
  // method.
  didInsertElement: function() {
    this._super();

    // Make jQuery UI options available as SproutCore properties
    var options = this._gatherOptions();

    // Make sure that jQuery UI events trigger methods on this view.
    this._gatherEvents(options);

    // Create a new instance of the jQuery UI widget based on its `uiType`
    // and the current element.
    var ui = jQuery.ui[this.get('uiType')](options, this.get('element'));

    // Save off the instance of the jQuery UI widget as the `ui` property
    // on this SproutCore view.
    this.set('ui', ui);


  },

  // When SproutCore tears down the view's DOM element, it will call
  // this method.
  willDestroyElement: function() {
    var ui = this.get('ui');

    if (ui) {
      // Tear down any observers that were created to make jQuery UI
      // options available as SproutCore properties.
      var observers = this._observers;
      for (var prop in observers) {
        if (observers.hasOwnProperty(prop)) {
          this.removeObserver(prop, observers[prop]);
        }
      }
      ui._destroy();
    }
  },

  // Each jQuery UI widget has a series of options that can be configured.
  // For instance, to disable a button, you call
  // `button.options('disabled', true)` in jQuery UI. To make this compatible
  // with SproutCore bindings, any time the SproutCore property for a
  // given jQuery UI option changes, we update the jQuery UI widget.
  _gatherOptions: function() {
    var uiOptions = this.get('uiOptions'), options = {};

    // The view can specify a list of jQuery UI options that should be treated
    // as SproutCore properties.
    uiOptions.forEach(function(key) {
      options[key] = this.get(key);

      // Set up an observer on the SproutCore property. When it changes,
      // call jQuery UI's `setOption` method to reflect the property onto
      // the jQuery UI widget.
      var observer = function() {
        var value = this.get(key);
        this.get('ui')._setOption(key, value);
      };

      this.addObserver(key, observer);

      // Insert the observer in a Hash so we can remove it later.
      this._observers = this._observers || {};
      this._observers[key] = observer;
    }, this);

    return options;
  },

  // Each jQuery UI widget has a number of custom events that they can
  // trigger. For instance, the progressbar widget triggers a `complete`
  // event when the progress bar finishes. Make these events behave like
  // normal SproutCore events. For instance, a subclass of JQ.ProgressBar
  // could implement the `complete` method to be notified when the jQuery
  // UI widget triggered the event.
  _gatherEvents: function(options) {
    var uiEvents = this.get('uiEvents') || [], self = this;

    uiEvents.forEach(function(event) {
      var callback = self[event];

      if (callback) {
        // You can register a handler for a jQuery UI event by passing
        // it in along with the creation options. Update the options hash
        // to include any event callbacks.
        options[event] = function(event, ui) { callback.call(self, event, ui); };
      }
    });
  }
});

然后定义 Draggable Mixin:

JQ.Draggable = Ember.Mixin.create( JQ.Base, {
    uiType: 'draggable',
    uiOptions: ['disabled', 'addClasses', 'appendTo', 'axis', 'cancel', 'connectToSortable', 'containment', 'cursor', 
              'delay', 'distance', 'grid', 'handle', 'snap', 'snapMode', 'stack'],
    uiEvents: ['create', 'start', 'drag', 'stop']

});

Resizable Mixin:

  JQ.Resizable = Ember.Mixin.create( JQ.Base, {
    uiType: 'resizable',
    uiOptions: ['disabled', 'alsoResize', 'animate', 'animateDuration', 'animateEasing', 'aspectRatio', 'autoHide', 'cancel', 
              'containment', 'delay', 'distance', 'ghost', 'grid', 'handles', 'helper', 'maxHeight', 'maxWidth', 'minHeight',
              'minWidth'],
    uiEvents: ['create', 'start', 'resize', 'stop']
});

基本上,对于您要定义的每个 UI 交互,uiType(可拖动、可放置、可排序等)、交互的 uiOptions(由 mixin 观察) ,以及您要在 View 中实现的交互的 uiEvents

通过在您的 View 中包含 JQ.Draggable 和 JQ.Droppable,它会自动变为可拖放 + 您可以在 View 中更改其选项并将这些更改反射(reflect)在 UI 插件上,例如this.set('delay', 900),并在您的 View 中实现插件事件,例如drag: function(){/* 在拖动时做一些事情*\} ).

关于javascript - Ember.js draggable and droppable jqueryUI/native 拖放 mixin,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11146470/

相关文章:

javascript - jQuery UI 自动完成在恰好 2041 条记录后停止工作

asp.net-mvc - 编码 UI 测试项目未触发 Jquery AJAX 成功

javascript - 如何编写 HTML 页面以在单击按钮时更新从文本文件导入的文本?

javascript - AngularJS 使用 ajax 调用将 ng-repeat 加载到另一个 ng-repeat

javascript - 使用 velocity.js 将 div 动画化到中心?

javascript - JS触发checkbox的checked attr不会动态改变

javascript - 在Javascript中,为什么不能任意设置原型(prototype)继承呢?

JavaScript 正则表达式 : how to match on the whole text?

javascript - 覆盖标签?

jquery - ui-draggable 在 Chrome 上的模态对话框中无法正常工作