1

I have a controller that defines onDragEvent:

controller = Em.Object.create( {
    onDragEvent: function() {
        console.log("Drag Event");
    }
});

And I have a Mixin:

Event = Ember.Mixin.create( {
    attributeBindings: 'draggable',
    draggable: 'true',
    drag: function (event) {
        Em.run.throttle(this, controller.onDragEvent, 1000);
    }
});

My understanding is that during the drag the controller.onDragEvent should be called once every 1000 milliseconds.

The console is logging out:

Uncaught TypeError: Object function (a,b){var d,e;f.begin();try{if(a||b)d=c(a,b,arguments,2)}finally{f.end()}return d} has no method 'throttle' Events.js:42
Events.Ember.Mixin.create.drag Events.js:42
Ember.View.Ember.Object.extend.trigger ember-1.0.pre.min.js:16
d ember-1.0.pre.min.js:14
Ember.View.states.hasElement.handleEvent ember-1.0.pre.min.js:16
Ember.View.Ember.Object.extend.invokeForState ember-1.0.pre.min.js:16
Ember.View.Ember.Object.extend.handleEvent ember-1.0.pre.min.js:16
(anonymous function) ember-1.0.pre.min.js:16
c ember-1.0.pre.min.js:14
Ember.run ember-1.0.pre.min.js:14
Ember.EventDispatcher.Ember.Object.extend._bubbleEvent ember-1.0.pre.min.js:16
(anonymous function) ember-1.0.pre.min.js:16
f.event.dispatch jquery-1.7.2.min.js:3
h.handle.i

I'm pretty sure I don't understand how to use the throttle feature. I also notice that I am using Em.Object.create vs. Ember.Mixin.create, but calling the controller.onDragEvent without the throttle works fine. What am I doing incorrectly?

edit 1:

if I use:

Event = Ember.Mixin.create( {
    attributeBindings: 'draggable',
    draggable: 'true',
    drag: function (event) {
        Em.run.throttle(controller, onDragEvent, 1000);
    }
});

I get a different error:

Uncaught ReferenceError: onDragEvent is not defined

For reference; just calling controller.onDragEvent(); works fine.

4

2 回答 2

1

我刚刚意识到我从未见过堆栈的右侧。您使用的是旧版本的 ember,直到 1.0 beta 后期才引入油门和去抖动功能(准确地说是 1.0 RC7,这是 1.0 pre 之后的方式)

切换到这个版本:

//cdnjs.cloudflare.com/ajax/libs/ember.js/1.0.0/ember.js

它似乎对我有用:

http://emberjs.jsbin.com/oZIMiMA/1/edit

突出显示输出上的 a 或 b。

你能设置一个 jsbin 显示它不工作吗?

可拖动示例:

http://jsfiddle.net/tjZac/1/

DragNDrop.Draggable = Ember.Mixin.create({
  attributeBindings: 'draggable',
  draggable: 'true',
    drag: function(event) {
        console.log('real call')
         Ember.run.throttle(this, DragNDrop.controller.onDragEvent, 300);
    }
});
于 2013-10-18T19:32:52.807 回答
0

据我所知,您希望在拖动元素时每隔一段时间触发一个事件,并希望该事件在控制器中提示函数调用。

您可以使用throttle阻止函数被调用的频率超过指定的超时时间。这里有一些文档:http: //emberjs.com/api/classes/Ember.run.html#method_throttle。如果您在调用之前声明函数而不是匿名传递,您的问题是否仍然存在?

您的控制器可能应该从 扩展Ember.ObjectController,而不是扩展Ember.Object,因为它ObjectController附带了一堆方便的方法,这些方法将使作为控制器而不是通用对象访问控制器变得更加容易。

于 2013-10-18T19:29:01.513 回答