EmberJS 中 "persistent state" 的定义范围是什么?

What's the scope of definition of "persistent state" in EmberJS?

这听起来像是一个迂腐的问题。对不起:)

我有一个这样的案例...这是我的路由器定义:

Router.map(function() {
  this.resource('gobernadores', { path: '/gobernadores' }, function() {
    this.resource('gobernador', { path: '/:id_estado' }, function() {
      this.route('simulacion', { path: '/simulacion' }),
      this.route('info', { path: '/info' })
    })
  });
  this.route("login");
  this.route("bienvenido");
});

在"gobernadores" 路线中,我有省份列表。你可以看到它是一个嵌套布局。在同一个页面中,我们显示了当前选择的省份(这是 gobernador 路线)。在那个 gobernador 路线的模板中,我有一个选项卡,有两个元素...,一个显示路线 "simulacion",另一个显示路线 "info"(该省)的模板。

现在,问题是:当用户从一个省跳到另一个省(通过点击屏幕左侧的导航菜单)时,我想在内存中保存当前选择的选项卡,对于每个省.

因此,如果用户当前正在查看 X 省的模拟结果,然后他点击 link 前往 Y 省(在那里他将看到 "info" Y 省),然后他回到 X 省,我希望应用程序将用户带回他看到的屏幕(X 省的模拟)。

您不能将该信息存储在控制器 (GobernadorController) 中,因为我可以看到控制器不能保持状态,它是无状态的。

所以...,我必须将该信息移动到路线模型 (GobernadorRouteModel)...

我的疑问:可以吗?为什么我的怀疑?正因为如此:http://emberjs.com/guides/concepts/core-concepts/

它说:

MODELS

A model is an object that stores persistent state. Templates are responsible for displaying the model to the user by turning it into HTML. In many applications, models are loaded via an HTTP JSON API, although Ember is agnostic to the backend that you choose.

ROUTE

A route is an object that tells the template which model it should display.

这个 GobernadorRouteModel 不是我在后端坚持的东西。我无意这样做。那么,我是否违反了对好的 EmberJS 应用程序的一般建议?

或者换句话说:这里的"persistent"不一定是"something you save into DB",对吧?只是 "something you want to keep around..., eventhough only during the session of the app, in the memory".

提前致谢, 拉卡

You can't have that information stored in the controller (GobernadorController), because I can see that controllers can't keep state, it's stateless.

这可能就是您的问题所在。控制器 不是 无状态的。 Ember 中的控制器是单例,并在应用程序的整个生命周期中保持其状态。然而,这将在 Ember 2.0 中改变。引用该 RFC:

Persistent state should be stored in route objects and passed as initial properties to routable components.

因此,如果您想要向前兼容,那是我会采用的方法。在我看来,模型实际上应该只用于持久状态(持久意味着它在页面加载之间持续存在)。为了保持会话状态,我会按照 RFC 所说的那样做,并在路由中保持该状态,并在 resetController 挂钩期间将其注入控制器。

或者,如果您不想那么花哨并且不关心向前兼容性,只需拥有一个存储状态的全局 Session 对象。我目前就是这样做的,并且它工作得很好。 (虽然我们可能会远离它。)

TL;DR:不,我认为您没有将模型用于预期目的。