Not long ago I introduced a new framework called Joist. As pointed out in that introduction, one of the more unique features of Joist is the concept of “handlers” and because of this I thought it would be worth digging into what handlers are and what you can do with them.
不久前,我引入了一个称为Joist的新框架。 正如该引言中所指出的,Joist的更独特之处之一是“处理程序”的概念,因此,我认为有必要深入研究什么是处理程序以及您可以对它们进行处理。
NOTE: If you haven’t read the introduction I suggest you do so.
注意:如果您还没有阅读介绍,我建议您阅读。
A handler in Joist allows a user to map one or more class methods to an “action” and one or more actions to a single class method. In more plain terms, a single action can trigger one or many methods to be called. Think of it somewhat like an event system.
Joist中的处理程序允许用户将一个或多个类方法映射到“操作”,并将一个或多个操作映射到单个类方法。 简而言之,一个动作可以触发一个或多个方法被调用。 认为它有点像事件系统。
In the simple example above it can be hard to see how this could be useful as it would be much more straight forward to just call the methods directly. To help show how you can benefit from handlers let’s talk about what an action should itself should be. An action name should describe what is happening. For example instead of “increment” we could say something more descriptive like “inc_btn_clicked”. This explains exactly what interaction has happened. If we take this naming approach coupled with the ability for a single method to handle multiple actions our above example might look something like this.
在上面的简单示例中,很难看到它如何有用,因为直接调用方法会更加直接。 为了帮助说明如何从处理程序中受益,我们来讨论一下动作本身应该是什么。 动作名称应描述正在发生的事情。 例如,我们可以说一些更具描述性的内容,例如“ inc_btn_clicked”,而不是“增量”。 这准确地解释了发生了什么交互。 如果我们采用这种命名方法,并结合使用一种方法来处理多个动作的能力,则上面的示例可能看起来像这样。
Now our code more explicitly tells us that when the decrement button is clicked OR the increment button is clicked we are going to update the existing count. This helps us see what is going on in our component, we can see what actions occur and in what order. This can be a helpful tool to help with debugging.
现在,我们的代码更明确地告诉我们,当单击减量按钮或单击增量按钮时,我们将更新现有计数。 这可以帮助我们了解组件中正在发生的事情,可以了解发生了什么动作以及发生了什么顺序。 这可能是有助于调试的有用工具。
Joist handlers can also match based on a regular expression. For example you might decide that you want to match any action that ends with “btn_clicked”, or in our code below we can create a debug function and watch any and all actions and log them out.
托梁处理程序还可以基于正则表达式进行匹配。 例如,您可能决定要匹配以“ btn_clicked”结尾的任何动作,或者在下面的代码中,我们可以创建调试功能并观察所有动作并将其注销。
Some of you out there might be starting to think of this as similar to how you would debug an application built with flux or redux, the piece we are missing is seeing how our state is changing, we can see actions flowing in but don’t see our state flowing out.
你们当中的有些人可能已经开始想到这一点,类似于调试使用flux或redux构建的应用程序的方式,我们所缺少的部分是看到状态如何变化,我们可以看到动作在进行,但没有看到我们的状态在流淌。
There are a couple of ways we could handle viewing updates to our state. We could just listen directly to our injected state object for changes but then we would have to correlate which action caused which state update ourselves. The more straight forward way is to make use of Joist’s onComplete callback. Joist handlers have the ability to return a Promise so Joist knows once all callbacks have completed.
我们可以通过两种方法来处理查看状态的更新。 我们可以直接听注入状态对象的更改,但是随后我们必须关联导致哪个状态更新的动作。 更直接的方法是利用Joist的onComplete回调。 Joist处理程序具有返回Promise的能力,因此Joist知道所有回调都完成后。
Using handlers allows us to keep our template code completely isolated from our component code. This lets you define very clear component code without needing to care about what is in the template, your component just needs to listen for actions and set state, it could care less about the presentation layer.
使用处理程序可以使模板代码与组件代码完全隔离。 这使您可以定义非常清晰的组件代码,而无需关心模板中的内容,而您的组件只需要侦听操作和设置状态,就可以减少对表示层的关心。
This has been a brief look into the concept of handlers in Joist. Handlers give us some interesting capabilities, are there any you can think of that I haven’t mentioned here? Let me know and give Joist a spin!
这是对Joist中处理程序的概念的简要介绍。 处理程序为我们提供了一些有趣的功能,您能想到我这里没有提到的任何功能吗? 让我知道,让Joist旋转!
https://github.com/joist-framework/joist
https://github.com/joist-framework/joist
翻译自: https://itnext.io/how-to-handle-yourself-in-joist-fa4b52b2b085
相关资源:微信小程序源码-合集6.rar