Ember renderTemplate 中继模型 [英] Ember renderTemplate relay model

查看:13
本文介绍了Ember renderTemplate 中继模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这里努力开发我的 Ember 应用程序,一切顺利.但是,我遇到了一个意外行为的问题,我不确定解决这个问题的最佳方法.

Working hard on my Ember app here, and it's going along fine. However, I've run into an issue of unexpected behaviour and I'm not sure regarding the best approach to this problem.

问题是在特定的路线中,我想将另一条路线渲染到另一个出口.但是,我渲染到另一个出口的另一条路线没有保留它自己的模型.

The problem is that in a specific route, I want to render another route into another outlet. However, the other route that I render into the other outlet doesn't retain it's own model.

如果我这样做:

App.TestRoute = Ember.Route.extend({
    model: function() {
        return {
            heading: "Test",
            testContent: "This is test."
        }
    }
});

App.IndexRoute = Ember.Route.extend({
    renderTemplate: function() {
        this.render("test", {
            outlet: "left"
        });

        this.render({
            outlet: "right"
        });
    },

    model: function() {
        return {
            heading: "Index",
            indexContent: "This is index."
        }
    }
});

... 并访问 IndexRoute,我希望 TestRoute 的模型被渲染到 TestRoute 的模板中,但只有 IndexRoute 的模型被中继到两个模板.

... and access the IndexRoute, I would expect the TestRoute's model to be rendered into the TestRoute's template, but only the IndexRoute's model is relayed to both templates.

在这里摆弄:http://jsfiddle.net/3TtGD/1/

如何让 Ember 使用默认模型作为路由,而无需明确地合并它们?看起来很乏味.

How do I allow Ember to use the default model for a route without having to expressively merge them? It seems tedious.

此外,某些模型属性具有相同的名称,例如 {{heading}} 是可取的,但不是必需的.

Also, having the same name of some model properties, like {{heading}} is desirable, but not necessary.

解决此问题的最佳方法是什么?

What's the best approach for solving this issue?

感谢您的宝贵时间.

最好的问候,昏暗

推荐答案

renderTemplate 方法中,您告诉 Ember 在插座内渲染模板,但它只会将控制器默认为一个管理路线.鉴于它是处理路由的控制器,它管理该路由中的所有模板是有道理的.

In the renderTemplate method you're telling Ember to render a template inside an outlet but it will just default the controller to the one managing the route. Given it's the controller handling the route it makes sense that it manages all the templates within that route.

当然,您可以使用以下方法指定不同的控制器:

Of course you can specify a different controller using:

this.render("test", {
    outlet: "left",
    controller: 'test'
});

它又可以是您已经实例化的控制器(并且可能设置其content):

it can in turn be a controller you already instantiated (and maybe set its content):

var testController = this.controllerFor('test');
testController.set(....)
this.render("test", {
    outlet: "left",
    controller: testController
});

关于使用模型:你可以在路由内部调用this.modelFor('test'),它会返回test路由的模型(它甚至知道如果已经解决).当我需要访问父路由之一的模型时,我通常会这样做.

About using the model: You can call this.modelFor('test') inside the route and it will return the model of the test route (it even knows if it has already been resolved). I usually do this when I need to access the model of one of the parent routes.

我相信访问父路由的模型是有意义的,但如果您访问的是无关路由的模型,则意义不大.你为什么不想合并两个模型?

I believe it makes sense to access the model of a parent route, but not so much if you're accessing the model of an unrelated route. Why don't you want to merge both models?

这篇关于Ember renderTemplate 中继模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆