再生コンポーネントを備えたスタンドアロンのアドオンがあるとしましょう:
{{my-record play="recordPlay" stop="recordStop"}}
/app/components/my-record.js
import Ember from 'ember';
export default Ember.Component.extend({
actions: {
play: function() {
this.sendAction('play');
},
stop: function() {
this.sendAction('stop');
}
}
});
アドオンがバックエンドと独立して動作し、すべてのアクションを内部で処理する必要があります。これら 2 つのアクションrecordPlay
をrecordStop
アドオン自体から処理して、消費するアプリケーションのコントローラー/ルートに触れる必要がないようにするにはどうすればよいですか?
私が試してみました:
アドオン内でアプリケーションコントローラーを作成します。`/app/controllers/application.js - これは呼び出されません
アドオン内でアプリケーションルートを作成します。`/app/routes/application.js - これは、消費するアプリケーションがアドオンのルートをオーバーライドする独自の ApplicationRoute を持っていない限り呼び出されます
アドオン内から初期化子を使用して、これら 2 つのアクションを ApplicationController に注入できますか?
編集: ApplicationRoute._actions を使用した汚い回避策
/app/initializers/record.js
export default {
name: 'record',
initialize: function(container, app) {
var applicationRoute = container.lookup('route:application');
applicationRoute._actions.recordPlay = function(id) {
console.log('CALLED recordPlay', id);
};
applicationRoute._actions.recordStop = function(id) {
console.log('CALLED recordStop', id);
};
}
};