Ember.js 如何在应用程序启动时加载余烬数据?

Ember.js 如何在应用程序启动时加载余烬数据?,ember.js,ember-data,Ember.js,Ember Data,作为我(我认为这一点更具体)的后续行动,我想问你,当应用程序启动时,我如何加载数据。我需要一些缓存策略 我们内部应用程序中显示的数据每周刷新一次,这样我就不想每次用户进入路由时都发出新的请求(现在我看到很多ajax请求在应用程序中移动) .all()方法可能会解决我的问题,但首先我必须加载数据 我还可以在哪里加载控制器和模板可以访问的数据?是否可以在应用程序启动时加载数据并将其传递给控制器,就像您使用模型和控制器进行挂钩一样 在我看来,用户每周必须刷新一次应用程序是没有问题的——也许我们可以在以

作为我(我认为这一点更具体)的后续行动,我想问你,当应用程序启动时,我如何加载数据。我需要一些缓存策略

我们内部应用程序中显示的数据每周刷新一次,这样我就不想每次用户进入路由时都发出新的请求(现在我看到很多ajax请求在应用程序中移动)

.all()方法可能会解决我的问题,但首先我必须加载数据

我还可以在哪里加载控制器和模板可以访问的数据?是否可以在应用程序启动时加载数据并将其传递给控制器,就像您使用
模型
控制器进行挂钩一样

在我看来,用户每周必须刷新一次应用程序是没有问题的——也许我们可以在以后更改

我对直觉像素答案的理解

首先从服务器加载数据:

App = Ember.Application.create({
    ready: function() {
        this.set('userCache', App.User.find());
        this.set('currentUserCache', App.User.find(1));
        this.set('topCache', App.Top.find());
    }
});
然后从缓存(存储)加载数据

例如,尽管我可以通过以下方式访问化身源:

App.CurrentUser.all().objectAt(0).get('gravatar')
它在模板中不可见

{{#linkTo 'users' classNames="avatar pull-left" title="back"}}
  {{avatar gravatar}}
{{/linkTo}}
我还尝试了
content.gravatar
controller.gravatar
,但没有成功。
其余部分在视图中可见

仅在概念上,您可以执行以下操作:

App = Ember.Application.create({
  // Load your data once and set it somewhere accessible
  ready: function() {
    this.set('superModelCache', App.SuperModel.find());
  }
});


// Define your special model
App.SuperModel = DS.Model.extend({
  prop1: DS.attr('string'),
  prop2: DS.attr('string'),
  ...
});

// And then in all the routes that need data from your cache
// you could do something like this
App.MyFirstRoute = Ember.Route.extend({
  model: function () {
    return App.get('superModelCache.prop1');
  }
});

// and so on...
App.MySecondRoute = Ember.Route.extend({
  model: function () {
    return App.get('superModelCache.prop2');
  }
});
// And then in all the routes that need data from your cache
// you could do something like this
App.MyFirstRoute = Ember.Route.extend({
  model: function () {
    return App.SuperModel.all().get('prop1');
  }
});

// and so on...
App.MySecondRoute = Ember.Route.extend({
  model: function () {
    return App.SuperModel.all().get('prop2');
  }
});
或者你也可以在你的路线上做这样的事情:

App = Ember.Application.create({
  // Load your data once and set it somewhere accessible
  ready: function() {
    this.set('superModelCache', App.SuperModel.find());
  }
});


// Define your special model
App.SuperModel = DS.Model.extend({
  prop1: DS.attr('string'),
  prop2: DS.attr('string'),
  ...
});

// And then in all the routes that need data from your cache
// you could do something like this
App.MyFirstRoute = Ember.Route.extend({
  model: function () {
    return App.get('superModelCache.prop1');
  }
});

// and so on...
App.MySecondRoute = Ember.Route.extend({
  model: function () {
    return App.get('superModelCache.prop2');
  }
});
// And then in all the routes that need data from your cache
// you could do something like this
App.MyFirstRoute = Ember.Route.extend({
  model: function () {
    return App.SuperModel.all().get('prop1');
  }
});

// and so on...
App.MySecondRoute = Ember.Route.extend({
  model: function () {
    return App.SuperModel.all().get('prop2');
  }
});
这只会在应用程序启动时发出GET请求,然后您可以在应用程序生命周期中通过一些间隔/轮询来重新加载数据

如果您的应用程序依赖于数据来启动,您还可以调用
App.deferReadiness()App.advanceReadiness()完成后,请参见此处的API文档以供参考:


希望能有所帮助

很抱歉耽搁了,但我一回到我的旧车票就尝试一下,好吗?好的,我需要解决两个问题#1如果我通过App.User.find(1)(当前用户)加载模型,如何通过all加载?所有(1)项似乎都不起作用#2您能告诉我当您加载更多模型时如何使用延迟准备和提前准备吗?ready hook有可能吗?#1
.all(1)
不起作用,这就是我提出返回App.get('superModelCache.prop1')的简单方法的原因
在您的模型钩子中搜索
prop1
使用
可以获得什么。查找(1)
但不使用商店#2
deferReadiness
advanceReadiness
,除了当您调用
deferReadiness
时,余烬将等待启动进程,直到您调用
advanceReadiness
Hm,我不太明白。你的超模是什么?你是什么?第一和第二?请看一看我更新的示例,看看我的理解。超级模特只是概念性的,你可以随意称呼它,还有
prop1
等等。另外,因为我不知道你的数据,所以我把一些我发明的东西放在了一起,这是有意义的:)但是看看你更新的问题,我认为这一行
this.set('currentUserCache',App.User.find(1))
应该类似于
this.set('currentUserCache',App.User.all().objectAt(0))
否则,
currentUserCache
不是来自已加载的数据
App.User.find()