【问题标题】:How to deal with dynamic content with Router?如何用Router处理动态内容?
【发布时间】:2012-10-25 03:33:15
【问题描述】:

我正在通过 Ember.js 开发一个应用程序,该应用程序维护类别,在类别的列表视图中,当用户单击创建按钮时,我们将动态显示新的类别表单。在使用 Ember 的 Router 之前,我们使用如下机制:

查看:

LCF.CategoriesView = Ember.View.extend({
    templateName:'admin/app/templates/categories/list',
    isNewVisible:false,

    showNew:function () {
        this.set('isNewVisible', true);
    },

    hideNew:function () {
        this.set('isNewVisible', false);
    }
});

模板:

<div class="well well-small">
   <a class="btn  btn-primary" href="#" {{action "showNewCategory"}}>Create</a>
</div>
{{#if view.isNewVisible}}
  {{view LCF.NewCategoryView}}
{{/if}}

使用路由器后,事件由路由处理,我修改代码如下:

路由器:

    categories:Em.Route.extend({
        route:'/categories',

        connectOutlets:function (router, context) {
            router.get('applicationController').connectOutlet('categories', router.get('store').findAll(LCF.Category));
        },

        showNewCategory:function (router) {
            router.transitionTo('categories.newCategory', {});
        },

        index:Ember.Route.extend({
            route:'/'
        }),

        newCategory:Em.Route.extend({
            route:'/new',

            cancelEdit:function (router) {
                router.transitionTo('categories.index');
            },

            connectOutlets:function (router, context) {
                router.get('categoriesController').connectOutlet('editCategory', {});
                router.get('editCategoryController').enterEditing();
            }
        })

模板:

<div class="well well-small">
    <a class="btn  btn-primary" href="#" {{action "showNewCategory"}}>Create</a>
</div>
{{outlet}}

有效。

我的问题是:

  • 这是处理动态视图的正确方法吗?我们是否需要为页面中的每个浮动层创建一个状态/控制器?
  • 即使应用仍然显示类别,URL 也会更改。
  • 模板中只有一个{{outlet}},如果有多个动态视图要显示,怎么办?

【问题讨论】:

    标签: dynamic view ember.js router


    【解决方案1】:
    • 对我来说,这看起来很不错。对于状态创建和控制器......这里没有是/否的答案。通常我会说您为每个上下文逻辑创建一个控制器,并在您想要处理新的应用程序状态时创建一个路由。在您的示例中,您做得很好,类别(状态以概述所有类别),显示(显示特定类别)...

    • 这里有什么问题?抱歉 :s,我不明白你想在这里了解什么。

    • 这里的答案很简单,如果你有多个动态视图显示在页面中,你可以使用多个 {{outlets}},使用命名的 outlet。见:Ember.js Router App Architecture -- How to have multiple nested view/controller pairs

    【讨论】:

    • 不客气。如果你能在第二点澄清你想要什么,我会尽力回答:)
    • 对于问题2:传统上,在rails应用程序中,我们将使用链接xxx/categories来显示所有类别,然后在同一页面中,我们应该新的类别形式,在这种情况下,网址不变。但是当我们使用 ember 的路由器时,进入 newCategory 状态后,url 将变为xxx/categories/new(这是用于 Rails 中的新页面)。这看起来很难看。
    猜你喜欢
    • 2010-12-18
    • 2017-02-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-30
    • 2019-01-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多