灰烬添加customEvents

时间:2019-10-02 11:00:57

标签: ember.js

我正在使用Ember 2.9,并且想使用“粘贴”事件。

如何在启动应用程序时将粘贴添加为customEvent:

这是我当前的app.js:

import Ember from 'ember';
import Resolver from './resolver';
import loadInitializers from 'ember-load-initializers';
import config from './config/environment';

let App;

Ember.MODEL_FACTORY_INJECTIONS = true;

App = Ember.Application.extend({
  modulePrefix: config.modulePrefix,
  podModulePrefix: config.podModulePrefix,
  Resolver
});

loadInitializers(App, config.modulePrefix);

export default App;

1 个答案:

答案 0 :(得分:1)

我在https://github.com/bartocc/stackoverflow-2176861处安装了一个Ember.js 2.9演示应用程序,用于演示下面的代码。

下面是一个示例app.js,用于配置Ember.js来监听paste事件:

// /app/app.js
import Ember from 'ember';
import Resolver from './resolver';
import loadInitializers from 'ember-load-initializers';
import config from './config/environment';

let App;

Ember.MODEL_FACTORY_INJECTIONS = true;

App = Ember.Application.extend({
  modulePrefix: config.modulePrefix,
  podModulePrefix: config.podModulePrefix,
  Resolver,

  // This is how you make your ember app listen the paste event
  customEvents: {
    paste: 'paste'
  }
});

loadInitializers(App, config.modulePrefix);

export default App;

the Ember.js 2.9 API中查找更多信息。

然后,使用以下命令使任何组件监听paste事件:

import Ember from 'ember';

export default Ember.Component.extend({
  paste(event) {
    const text = event.originalEvent.clipboardData.getData('Text');
    alert(`you've just pasted the text '${text}'`);
  }
});