首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Ember.js可拖放jqueryUI /本机拖放混合

Ember.js可拖放jqueryUI /本机拖放混合
EN

Stack Overflow用户
提问于 2012-06-22 04:46:16
回答 1查看 3.2K关注 0票数 4

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

我已经看到了一些解决方案,可以通过混入进行拖动,并使用此混入创建一个可拖动视图,然后通过droppable混入从droppable创建另一个视图。

但我需要每个项目/视图有可拖,可放和可排序的功能。

有没有人能告诉我最好的方法是通过混合或者子类化或者...?

另外,我是否可以创建一个jqueryUi混入作为基础混入,然后在创建可拖动、可丢弃和可排序的混入时使用该混入?这是可能的吗?

最好是使用jqueryUI或html5拖放应用程序接口还是其他什么?

谢谢你的帮助瑞克

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-07-09 01:29:48

不知道你是否看到了Katz here的代码,但我用它来创建一个视图:可放,可拖,...或jQuery UI支持的任何其他交互。因此,您定义了一个基础Mixin,它将在所有jQuery UI交互Mixin中使用:

代码语言:javascript
运行
复制
// 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); };
      }
    });
  }
});

然后定义可拖动的Mixin:

代码语言:javascript
运行
复制
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']

});

可调整大小的Mixin:

代码语言:javascript
运行
复制
  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观察)以及您希望在视图中实现的交互的uiEvents

通过在你的视图中包含JQ.Draggable和JQ.Droppable,它会自动变得可拖可放+你能够在你的视图中改变它的选项,并在UI插件上反映这些变化,例如this.set('delay', 900),并在你的视图中实现插件事件,例如drag: function(){ /* do something when dragging*\ } )。

票数 7
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11146470

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档