强制模板刷新ember.js

Force template refresh ember.js

本文关键字:ember js 刷新      更新时间:2023-09-26

我正在使用I18n本地化包来处理我的应用程序的切换语言部分。它使用一个全局变量来设置所需的语言,并使用一个json文件来存储翻译。

由于语言的切换只是全局变量中的一个变化,ember不会检测到它,也不会自动呈现模板。我通过应用程序控制器中的一个操作强制它:

Extranet.ApplicationController = Ember.ObjectController.extend(
{
    actions:
    {
        localToFr: function()
        {
            this.localisation('fr'); // this changes the global variable
            this.get('target.router').refresh(); // this is what refresh the template
        },
        localToEn: function()
        {
            this.localisation('en');
            this.get('target.router').refresh();
        }
    },
    localisation: function(lg)
    {
        I18n.locale = lg;
    }
})

我对这个解决方案有两个问题:1) 应用程序模板没有通过我的重新绘制

this.get('target.router').refresh();

2) 我的另一个问题是,它不适用于不请求服务器访问的模板(例如:路由"authSession"的嵌套)

Extranet.Router.map(function()
    {
        this.resource(
            'parkings', {path:'/'}, function ()
            {
                this.route('parking', {path:'/parking/:parking_id'});
                this.route('historique', {path:'/parking/:parking_id/historique'});
                this.route('sessAct', {path:'/parking/:parking_id/sessAct'});
                this.route('rapport', {path:'/parking/:parking_id/rapport'});
            }
        );
        this.resource(
            'authSession', function ()
            {
                this.route('login');
                this.route('logout');
            }
        );
    }
);

我也遇到了类似的问题。我只是在主视图上使用了View.rerender(),这是我的一种形式。