简体   繁体   English

Glimmer.js 如何在不使用构造函数的情况下将跟踪属性重置为初始值

[英]Glimmer.js how to reset tracked property to initial value without using the constructor

In Glimmer.js, what is the best way to reset a tracked property to an initial value without using the constructor?在 Glimmer.js 中,在不使用构造函数的情况下将跟踪属性重置为初始值的最佳方法是什么?

Note: Cannot use the constructor because it is only called once on initial page render and never called again on subsequent page clicks.注意:不能使用构造函数,因为它只在初始页面呈现时调用一次,并且在后续页面点击时不再调用。

There are two parts to this answer, but the common theme between them is that they emphasize switching from an imperative style (explicitly setting values in a lifecycle hook) to a declarative style (using true one-way data flow and/or using decorators to clearly indicate where you're doing some kind of transformation of local state based on arguments).这个答案有两个部分,但它们之间的共同主题是它们强调从命令式样式(在生命周期挂钩中显式设置值)到声明式样式(使用真正的单向数据流和/或使用装饰器来实现)的切换清楚地表明您在哪里根据参数对本地状态进行某种转换)。

  1. Are you sure you need to do that?你确定你需要这样做吗? A lot of times people think they do and they should actually just restructure their data flow.很多时候人们认为他们这样做了,他们实际上应该重组他们的数据流。 For example, much of the time in Ember Classic, people reached for a pattern of "forking" data using hooks like didInsertElement or didReceiveAttrs .例如,许多的灰烬经典的时候,人们达到使用像钩子“分叉”的数据的模式didInsertElementdidReceiveAttrs In Glimmer components (whether in Ember Octane or in standalone Glimmer.js), it's idiomatic instead to simply manage your updates in the owner of the data: really doing data-down-actions-up.在 Glimmer 组件中(无论是在 Ember Octane 中还是在独立的 Glimmer.js 中),在数据所有者中简单地管理您的更新是惯用的:真正执行 data-down-actions-up。

  2. Occasionally, it does actually make sense to create local copies of tracked data in a component—for example, when you want to have a clean separation between data coming from your API and the way you handle data in a form (because user interfaces are API boundaries! ).有时,在组件中创建跟踪数据的本地副本实际上是有意义的——例如,当您希望将来自 API 的数据与您在表单中处理数据的方式(因为用户界面是 API界限! )。 In those scenarios, the @localCopy and @trackedReset decorators from tracked-toolbox are great solutions.在这些场景中,来自tracked-toolbox@localCopy@trackedReset装饰器是很好的解决方案。

    • @localCopy does roughly what its name suggests. @localCopy大致按照其名称进行操作。 It creates a local copy of data passed in via arguments, which you can change locally via actions, but which also switches back to the argument if the argument value changes.它创建了通过参数传入的数据的本地副本,您可以通过操作在本地更改它,但如果参数值更改,它也会切换回参数。

    • @trackedReset creates some local state which resets when an argument updates. @trackedReset创建一些本地状态,当参数更新时重置 Unlike @localCopy , the state is not a copy of the argument, it just needs to reset when the argument updates.@localCopy不同,状态不是参数的副本,它只需要在参数更新时重置。

With either of these approaches, you end up with a much more “declarative” data flow than in the old Ember Classic approach: “forking” the data is done via decorators (approach 2), and much of the time you don't end up forking it at all because you just push the changes back up to the owner of the original data (1).使用这两种方法中的任何一种,你最终都会得到比旧的 Ember Classic 方法更“声明性”的数据流:“分叉”数据是通过装饰器完成的(方法 2),而且大部分时间你不会结束根本不会分叉它,因为您只是将更改推送回原始数据的所有者 (1)。

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

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