在将控制器模型加载到Ember之前,使其可用于ApplicationController

时间:2014-10-12 02:24:41

标签: javascript ember.js

我正在尝试访问我的ApplicationController所需的控制器(应用程序需求实践),但是实践控制仅在通过它的相应route-url访问加载资源之后才可用。 我如何确保PracticeController及其内容/模型始终可用? 具体来说,我需要我的flashcardArray可以在我的应用程序中随时可用,即使练习路线没有被访问过,因此也可以加载。 谢谢你的帮助!

这是我的代码:

App.Router.map(function() {
    this.resource('signup');
    this.resource('login');
    this.resource('profile');
    this.resource('practice');
    this.resource('overview');
});

App.ApplicationRoute = Ember.Route.extend({
    model: function () {
    return this.store.find('user');
  }
});

App.LoginRoute = Ember.Route.extend({
    controllerName: 'application',
    model: function () {}
});

App.PracticeRoute = Ember.Route.extend({
    model: function () {
        return this.store.find('flashcards');
    }
});

//ApplicationController
App.ApplicationController = Ember.ObjectController.extend({
    needs: ['practice'],
    flashcardArray: Ember.computed.alias('controllers.practice.flashcardArray'),
    currentUser: Ember.computed.alias('model'),
    isLoggedIn: false
    }
});

//PracticeController
App.PracticeController = Ember.ObjectController.extend({
    needs: ['application'],
    flashcardArray: Ember.computed.alias('model'),
    currentUser: Ember.computed.alias('controllers.application.currentUser')
    }
});

// Practice template feeded into an outlet of the application template
<script type="text/x-handlebars" id="practice">
    <div class="content-padded">
        {{#each object in flashcardArray}}
            <div class="card_wrapper">
            <p><h1>{{{object.num_reference}}}</h1><p>
            <p>PinYin: {{{object.mandarin}}}</p>
            <p>def: {{object.definition}}</p>
        {{/each}}
    </div>
</script>

1 个答案:

答案 0 :(得分:0)

我认为如果您在整个应用程序中都需要数据,无论用户的活动路由如何,您可能需要将其作为ApplicationRoute返回:

App.ApplicationRoute = Ember.Route.extend({
    model: function () {
        return {
            user: this.store.find('user'),
            flashcards: this.store.find('flashcards')
        };
    }
});