【问题标题】:Emberjs: Dynamic template nesting and routingEmberjs:动态模板嵌套和路由
【发布时间】:2013-12-17 23:42:33
【问题描述】:

我今天大部分时间都在努力解决这个问题,这让我发疯了,因为我想我快到了,但就是无法弄清楚最后一部分......

我有一个名为 Map 的路线,它呈现一个侧边栏,并在其中有一个用于侧边栏内容的命名出口:

map.hbs:

<div id="map-container">
    {{render sidebar}}
    <div id="map-canvas">
    </div>
</div>

... sidebar.hbs:

<div id="content-menu">
    {{outlet sidebar-content}}
</div>

我的侧边栏中的每个菜单项都有一个名为 loadModule 的自定义操作,它执行将命名视图渲染到侧边栏内容出口(使用 {{action 'loadModule' 'sidebar.module'}}):

var MapRoute = App.AuthenticatedRoute.extend({
actions: {
    loadModule: function(module) {
        //load a valid view template into the view
        this.render(module, 
            {
                into: 'sidebar',
                outlet: 'sidebar-content'
            });
    }
}
});

module.exports = MapRoute;

该视图的控制器内的任何操作都可以正常工作,我可以从按钮等触发它们,或者通过在 SidebarModuleViews 的 didInsertElement 中调用它们。

我的问题是我无法为这些视图定义模型,因此如果我尝试从他们的任何控制器中的 API 获取数据,它不会将这些数据呈现给模板。

我尝试使用链接到,但我无法将模板附加到当前视口,而不是刷新整个页面,这违背了拥有侧边栏的意义(我不希望更改路线)

var SidebarUserController = App.ApplicationController.extend({
actions: {
    doSomething: function() {
        alert('SOMETHING');
    },
    fetchUserProfile: function() {
        //do something
        var mod = this.store.find('profile', App.Session.get('uid'));
    }
}
});

一旦渲染模板,我可以从渲染模板触发其中任何一个操作,但是,尽管我的商店使用记录进行了更新,但 sidebar/user.hbs 中的把手助手不会填充模型数据。

这是我的模型:

var Profile = DS.Model.extend({
uid: DS.attr('string'),
firstName: DS.attr('string'),
lastName: DS.attr('string'),
gender: DS.attr('string'),
DOB: DS.attr('date'),
email: DS.attr('string')
});

module.exports = Profile;

这是我的侧边栏/user.hbs:

<div class="container">
    <button {{action 'doSomething'}}>Do A Thing</button>
    <h1>{{firstName}} {{lastName}}</h1>
    <h4>{{id}}</h4>
    {{#if isAuthenticated}}
        <a href="#" {{action 'logout'}}>Logout</a>
    {{/if}}
</div>

在该模板中,不会填充 firstName、lastName 和 id 字段,即使我从 API 中提取数据并成功存储它。

此外,如果有帮助,我的侧边栏/用户 router.map 如下所示:

this.resource('sidebar', function() {
        this.route('user');
    });

我相信这里的根本问题是我无法在不触发路由的情况下为控制器设置模型。我是不是搞错了?

【问题讨论】:

    标签: javascript ember.js handlebars.js


    【解决方案1】:

    好的,我已经为我的特定实例解决了这个问题。这可能不是最好的方法,但这是我需要的:

    在我的 MapRoute 中,我在 setupController 函数中为我的附加侧边栏菜单设置了模型和控制器。这样做允许我在页面加载时加载关键数据(例如用户配置文件等),并且我仍然可以保留 Route 中每个侧边栏模块的渲染功能,这将允许加载初始数据,并且仍然允许我在后续功能上更新每个侧边栏模块控制器的模型数据:

    map_route.js:

    actions: {
            loadModule: function(module) {
                this.render(module, {into: 'sidebar', outlet: 'sidebar-content'});
            }
    },
    setupController: function(controller, profile) {
        var model = this.store.find('profile', App.Session.get('uid'));
        var controller = this.controllerFor('sidebar.user');
        controller.set('content', model);
    },
    ...
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多