在Ember.js中渲染应用程序之前,正在等待模型加载

Waiting for models to load before rendering app in Ember.js

本文关键字:在等待 加载 模型 应用程序 js Ember      更新时间:2023-09-26

我有许多不同的应用程序级别模型,即当前用户、当前帐户等,我希望在呈现应用程序之前加载这些模型。应该如何以及在哪里进行?这个问题/答案帮助很大,但它没有涵盖异步方面。

下面的代码实现了我想要的,但在beforeModel中加载模型(以利用它等待承诺的解决)似乎并不正确。我应该在ApplicationRoute中加载这些模型吗?

App.ApplicationController = Ember.Controller.extend({
  currentAccount: null
});
App.ApplicationRoute = Ember.Route.extend({
  beforeModel: function () {
    var self = this;
    return App.Account.find(...).then(function (account) {
      self.controllerFor('application').set('currentAccount', account);
    });
  }
});

谢谢你的帮助!

诀窍是从路由的模型方法返回promise
这将导致路由器转换到App.LoadingRoute路由,直到承诺解决(可用于加载指示杆/轮子等)
当promise解析时,App.LoadingRoute将被停用,并且将调用原始路由的setupController方法
这适用于ember数据承诺、JQuery的$.ajax承诺和ember模型的fetch承诺
只需确保在解决承诺后返回实际模型即可
如果承诺被拒绝,这也是一个处理错误的好地方——但我将把这留给其他问题。

至于你应该在哪里加载你的模型,这取决于你的应用程序的使用情况
通常,您会加载一个模型,其中URL指示您需要该模型——经验法则是在URL中指示模型ID
如果您需要预取一些数据,这种情况当然会改变。

现在为一些代码:

App.SomeRoute = Ember.Route.extend({
   model: function(params){
       return App.SomeModel.fetch(params.model_id).then(function(modelData){
           // it is better to return the actual model here, and not the promise itself
           return App.SomeModel.find(params.model_id);
       });
   },
   setupController: function(controller, model){
       controller.set("model", model);
       // do some controller setup here - can be omitted if no setup is needed
       // this will run only after the promise has been resolved.
   }
});
App.LoadingRoute = Ember.Route.extend({
        activate: function(){
            this._super();
            // add some loading indication here
        },
        deactivate: function(){
            this._super();
            // remove loading indication
        }
}

希望这能有所帮助。

您想预加载数据/模型来初始化应用程序,但觉得beforeModel不正确?

听起来你需要一个应用程序初始化器!

在这种情况下,你的朋友:

应用程序.deferReadness()//停止应用程序的进程,直到该调用的所有实例(即:多个初始化程序)与以下调用的实例匹配:

App.advanceReadness()//认为这相当于promise解析调用。

1) 从你直接查找用户,修改提到的位置以适合你的应用程序设置:

Ember.Application.initializer({
    name: 'loadUser',
    after: 'store',
    initialize: function(container, app) {
        // modify this following to suit how you're determining the account
        var url = 'user/' + currentAccount;
        // tell the app to pause loading until advanceReadiness is declared
        app.deferReadiness();
        // load from JSON 
        Ember.$.getJSON('url').then(function(json) {
            var store = container.lookup('store:main');
            store.load(app.User, json);
            // tell app to start progressing again
            app.advanceReadiness();
        });
    }
});

2) 通过元标签:

Ember.Application.initializer({
    name: 'currentUser'
    after: 'store',
    initialize: function(container, app) {
        app.deferReadiness();
        $(function() {
            // Look up an attribute in a meta tag
            var store      = container.lookup('store:main'),
                attributes = $('meta[name="current-user"]').attr('content');
            if (attributes) {
                var obj        = store.load(app.User, JSON.parse(attributes)),
                    user       = App.User.find(obj.id),
                    controller = container.lookup('controller:currentUser').set('content', user);
                container.typeInjection('controller', 'currentUser', 'controller:currentUser');
            }
            app.advanceReadiness();
        });
    }
});

3) 直通会话数据:

Ember.Application.initializer({
    name  : 'currentUser',
    after : 'session',
    initialize: function(container, app) {
        var controller = container.lookup('controller:currentUser');
        container.typeInjection('controller', 'currentUser', 'controller:currentUser');
    }
});

我通过在ApplicationRoute中使用嵌套的Promises和afterModel方法成功地完成了这项工作。

App.ApplicationRoute = Ember.Route.extend({
    model: function() {
        // load the reservation (a globally needed model)
        return App.Reservation.fetch().then(function(reservations) {
            return reservations.get('firstObject');
        });
    },
    afterModel: function() {
        // Load all other globally needed models
        var self = this;
        return App.Gender.fetch().then(function(genders) {
            self.controllerFor('application').set('genders', genders);
            return App.FilterAttribute.fetch().then(function(filterAttributes) {
                self.controllerFor('application').set('filterAttributes', filterAttributes);
                //return App.SomeOtherModel...
            });
        });
    },
    setupController: function(controller, model) {
        controller.set('reservation', model);
    }
});

工作非常完美:-)应用程序将保留在LoadingRoute中,直到加载所有记录为止。注意,我使用的是Ember模型,但这应该没有什么区别,它只需要返回一个Promise。