在route.render()调用(或替代)中自定义Ember视图

时间:2014-03-24 20:38:55

标签: javascript twitter-bootstrap view ember.js single-page-application

我试图创建一个通用的模态对话框视图,我可以从应用程序中的任何控制器调用。我已经有类似的工作了:

App.ModalManaging = Ember.Mixin.create({
actions: {
    openModal: function (modalName, model, callback) {
        this.controllerFor(modalName)
            .set("model", model);
        this.set("modal", {
            callback: callback,
            name: modalName
        });
        this.render(modalName, {
            into: "application",
            outlet: "modal",
            controller: "application",
            view: "modal"
        });
    }
    //... other stuff
});

因此,对于每个模态,我创建一个单独的控制器类并将提供的对象设置为其模型。然后我使用render()方法将模态视图/控制器/模板渲染到'模态'套接字在主布局中。 modal收集用户操作并向调用控制器提供响应的方式超出了本问题的范围。可以说,这一部分有效。

这部分也有效,但我不喜欢它。每个模态的控制器看起来像是一种矫枉过正。我只需要一个具有一些属性的视图,它们可以直接与Application路由通信。

所以在过去的几个小时里,我一直试图以某种方式自定义render()方法使用的视图对象。

这不起作用:

this.render(modalName, {
    into: "application",
    outlet: "modal",
    controller: "application",
    view: new App.ModalView({
        message: "Are you sure?"
    })
});

我正在浏览余烬源,看起来似乎也不是这样:

this.render(modalName, {
    into: "application",
    outlet: "modal",
    controller: "application",
    view: "modal",
    viewHash: {
        message: "Are you sure?"
    }
});

基本上,我需要{{view}}助手的程序化等价物,包括提供我自己的哈希的能力。

我想我可以使用固定的ContainerView而不是空的插座并操纵它,但这似乎是模态控制器的笨重。

任何有关如何实现此类或类似内容的想法都将受到赞赏。

1 个答案:

答案 0 :(得分:0)

更新:使用私有router._lookupActiveView()方法执行此操作的方法。

var modalView = App.ModalView.create({
    templateName: modalName,
    message: "Are you sure?"
});
this.router._lookupActiveView("application")
    .connectOutlet("modal", modalView);

这似乎有效,但我不确定使用私有方法比使用controller-> model和ContainerView的笨重解决方案更好。

仍然在寻找一个坚实,规范的解决方案。