0

我想转换到模板,但通过 JSON 请求重新加载页面内容。

换句话说,我最初通过getJSON请求加载我的索引模板,然后我转到另一个模板来修改一些数据。稍后,我想通过具有相同getJSON请求的操作处理程序返回到我的初始模板(它将返回与第一个不同的另一个数据)。

App.IndexRoute = Ember.Route.extend({
    model: function() {
        return $.getJSON("myurl");
    },
    actions: {
        goto: function(){
            $.getJSON("myurl");
            this.transitionTo('index');
        }
    }
});

我能够转换到我的模板并执行新的 JSON 请求,但我看到的数据是最初的数据。这仅在我单击时才有效,因为 ember 使用模型函数的第一次调用来F5呈现模板。getJSON

编辑:

我已经找到了一个解决方案,但我想知道是否还有另一个更好的解决方案,而无需重新加载页面。

App.IndexRoute = Ember.Route.extend({
    model: function() {
        return $.getJSON("myurl");
    },
    actions: {
        goto: function(){
            this.transitionTo('index').then(function(){location.reload(true)});
        }
    }
});

先谢谢了!

4

2 回答 2

0

也许您可以使用控制器并手动设置模型,然后在函数 this.controllerFor('index').set('model',model)

这接近正确答案。

如果路由已经渲染到显示器,Ember 将取消转换,因此您的

this.transitionTo('index');

实际上并没有开火。这就是你的问题的根源。(可能值得为您的应用程序打开 ember 调试以将状态转换输出到控制台,这将表明它正在被终止)。

如果您确实想在路由中修改模型,我会直接在该路由控制器的操作处理程序中这样做:

App.IndexController = Ember.ObjectController.extend({
    actions: {
        goto: function(){
            this.set('model', $.getJSON('myurl'));
        }
    }
});
于 2014-02-13T04:34:24.087 回答
0

你能测试一下吗?

App.IndexRoute = Ember.Route.extend({
    model: function() {
        return $.getJSON("myurl");
    },
    actions: {
        goto: function(){
            var newModel = $.getJSON("myurl"),
            that = this;
            newModel.then(function(model){
                that.transitionTo('index',model);
            });
        }
    }
});
于 2013-11-05T11:44:20.530 回答