Session 余烬2.5观察会话属性更改
我已经对路由器进行了monkey补丁,将当前路由组件存储在会话变量中:Session 余烬2.5观察会话属性更改,session,ember.js,observers,Session,Ember.js,Observers,我已经对路由器进行了monkey补丁,将当前路由组件存储在会话变量中: var Router = Ember.Router.extend({ customSession: Ember.inject.service('session-custom'), location: config.locationType, didTransition: function() { this._super(...arguments); this.get
var Router = Ember.Router.extend({
customSession: Ember.inject.service('session-custom'),
location: config.locationType,
didTransition: function() {
this._super(...arguments);
this.get('customSession').set('currentEntity', this.get('currentRouteName').split('.')[0]);
this.get('customSession').set('currentDetailView', this.get('currentRouteName').split('.')[1]);
}
});
我知道这不是最干净的解决方案,但将会话写入控制台证明至少设置了这些参数
在我的控制器中,我想监听这些参数的变化,但不知何故,这不起作用:
import Ember from 'ember';
import ApplicationController from './application';
export default ApplicationController.extend({
customSession: Ember.inject.service('session-custom'),
currentRouteNameChanged: Ember.observer('customSession.currentEntity', function () {
console.log("route changed");
})
});
i、 e.“路线更改”从未打印到控制台
这似乎是一个很容易解决的问题,但我一直未能找到解决方案
谢谢 > P>请考虑使用AN将您的<代码>会话自定义< /代码>服务注入到应用程序的控制器中。为了达到这个目标,一些建议
首先,在路由器和其他地方,使用传统的、骆驼化的速记来注入您的服务,如下所示:
sessionCustom: Ember.inject.service(),
…并确保在代码中引用sessionCustom
,而不是customSession
接下来,创建一个会话自定义
初始值设定项,并将服务注入应用程序的控制器中:
export function initialize(application) {
application.inject('controller', 'sessionCustom', 'service:session-custom');
}
export default {
name: 'session-custom',
initialize,
};
现在,从控制器观察路线变化应该是成功的:
export default Ember.Controller.extend({
currentRouteNameChanged: Ember.observer(
'sessionCustom.currentEntity', function() {
console.log("CONTROLLER: ", this.get('sessionCustom.currentEntity'));
}
),
});
当然,这些变化也可以从服务本身观察到:
export default Ember.Service.extend({
currentEntity: '', // <-- it's helpful to explicitly declare
currentRouteNameChanged: Ember.observer(
'currentEntity', function() {
console.log("SERVICE: ", this.get('currentEntity'));
}
),
});
导出默认Ember.Service.extend({
CurrnEngult:“,//P.>也许考虑使用AN将您的<代码>会话自定义< /代码>服务应用到您的应用程序控制器中。
首先,在路由器和其他地方,使用传统的、骆驼化的速记来注入您的服务,如下所示:
sessionCustom: Ember.inject.service(),
…并确保在代码中引用sessionCustom
,而不是customSession
接下来,创建一个会话自定义
初始值设定项,并将服务注入应用程序的控制器中:
export function initialize(application) {
application.inject('controller', 'sessionCustom', 'service:session-custom');
}
export default {
name: 'session-custom',
initialize,
};
现在,从控制器观察路线变化应该是成功的:
export default Ember.Controller.extend({
currentRouteNameChanged: Ember.observer(
'sessionCustom.currentEntity', function() {
console.log("CONTROLLER: ", this.get('sessionCustom.currentEntity'));
}
),
});
当然,这些变化也可以从服务本身观察到:
export default Ember.Service.extend({
currentEntity: '', // <-- it's helpful to explicitly declare
currentRouteNameChanged: Ember.observer(
'currentEntity', function() {
console.log("SERVICE: ", this.get('currentEntity'));
}
),
});
导出默认Ember.Service.extend({
当前实体:“”//