简体   繁体   English

在Ember中控制器之间共享数据

[英]Sharing data between controllers in Ember

I'm new to Ember and I'm pulling my hair out over this. 我是Ember的新手,我正在把头发拉出来。

The app I'm working on has a set of "alerts" that will be dynamically updated. 我正在处理的应用程序有一组动态更新的“警报”。 I have the alerts template/controller/module all working fine. 我有警报模板/控制器/模块都工作正常。 The alerts controller has a method for displaying the number of alerts that have not been dismissed. 警报控制器具有用于显示尚未被解除的警报的数量的方法。 This works fine within the alerts template. 这在警报模板中工作正常。

However, when I access that same method from another controller, it always returns 0. 但是,当我从另一个控制器访问相同的方法时,它总是返回0。

Here's a pastebin with the controllers, templates and model 这是一个带有控制器,模板和模型的pastebin

Thanks for any help! 谢谢你的帮助!

UPDATE : 更新

I discovered that if I actually load the alerts view, and then go back to the home view, the alerts controller starts returning the correct value. 我发现如果我实际加载警报视图,然后返回到主视图,警报控制器将开始返回正确的值。 So apparently there is an issue with the fixture data not being ready when the menu controller accesses that method. 显然,当菜单控制器访问该方法时,夹具数据没有准备就绪存在问题。 I haven't yet been able to figure out why. 我还没弄清楚原因。

Try something like this: 尝试这样的事情:

App.HomeRoute = Ember.Route.extend({
    renderTemplate: function(controller, model) {
        this._super(controller, model);
        this.render('menu', {
            into: 'home',
            outlet: 'alertsMenuItem',
            controller: 'alerts'
        });
    }    
});

<script type="text/x-handlebars" data-template-name="home">
    <img {{bind-attr src=appInfo.headerImage}} id="header_image" alt="Header Image" />
    {{render "alerts" model}}
    <ul id="home_nav">
        <li>Home</li>
        <li>About</li>
        <li>
            {{outlet "alertsMenuItem"}}
        </li>
    </ul>
</script>   

<script type="text/x-handlebars" data-template-name="menu"> 
    {{#link-to 'alerts'}}
        Alerts ({{remaining}})
    {{/link-to}}
</script>

with Ember.Route.render you can specify the controller to use for a template. 使用Ember.Route.render,您可以指定用于模板的controller

Note that if you want to access your AlertsModel from routes other than alerts , you should set up your model in the AlertsController instead. 请注意,如果要从alerts以外的路径访问AlertsModel,则应在AlertsController中设置模型。 Something like this works for me with Ember 1.10, but might be slightly different for earlier version: 这样的东西适用于Ember 1.10,但对于早期版本可能略有不同:

App.AlertsController = Ember.ArrayController.extend({
    init: function() {
        var self = this;
        this.store.find('alert').then(function(model) {
            self.set('model', model);
        });
    },
    sortProperties: ['createdAt:desc'],
    sortedModel: Ember.computed.sort("model", "sortProperties"),
    remaining: function() {
            console.log(this.filterBy('isCompleted', false).get('length'));
            return this.filterBy('isCompleted', false).get('length');
    }.property('@each.isCompleted')
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM