使用表示模型在 Flex 中添加元素
我正在重构其他开发人员编写的一些 Flex 代码,并且正在实现演示模型方法,因为我喜欢将 ActionScript 从 MXML 中分离出来。我发现的问题之一是原始 ActionScript 代码在 MXML 中添加/删除元素。
发生的情况是处理函数检查模型,如果值正确,则会在视图中创建或删除元素。让演示模型将元素广告到视图中并仍然保持我所追求的松散耦合的最佳方法是什么。
我正在考虑使用演示模型调度的简单事件,并且视图可以列出来传递要添加的元素的详细信息。还有其他解决方案吗?
谢谢
斯蒂芬
I'm refactoring some Flex code written by another developer and I'm implementing a PresentationModel approach, as I like to separate out the ActionScript from the MXML. One of the problems I've found is that the original ActionScript code adds/removes elements from the MXML.
What happens is the handler function checks the model and if the values are correct will either create or remove a element from the view. What is the best way to get the presentation model to ad elements to the view and still keep this loose coupling that I'm aiming for.
I was thinking of using simple events which the presentation model dispatches and a view can list for passing the details of the element to add. Is there another solution?
Thanks
Stephen
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您使用演示模型,我假设您拥有某种需要发生的数据。当动态添加/删除任何类型的项目时,我确保使其由数据驱动,以便于操作。如果您想添加另一个项目,请将另一个数据模型添加到您选择的数据提供程序(列表、组合框、数据组等)中。
通过采用这种方法,您可以将逻辑从呈现器抽象到视图。事件只能用作让您的视图了解演示者何时完成某些操作的一种方式。可以通过这种方式接收数据(这样做是一个很好的做法),或者您可以将演示者中的数据绑定到您的 dataProvider。两者都很好,我只是发现绑定更干净、更简单。
If you're using the presentation model, I'd assume that you have some kind of data of what needs to happen. When items of any sort are being dynamically added/removed, I make sure to make it data-driven for easier manipulation. If you want another item added, add another data model to your dataProvider of choice (List, ComboBox, DataGroup, etc).
By doing this approach, you're abstracting the logic from the presenter to the view. Events should only be used as a way for your view to know when your presenter has accomplished something. Data can be received this way (and it's good practice to do so) OR you can just bind the data within the presenter to your dataProvider. Both are good, I just find binding to be cleaner and simpler.
执行图形操作的代码的每个部分(绘制边框、设置样式、拖放管理、动画等)都应包含在视图中,而不是表示模型中。
对于这种应该在 PM 中更改属性后执行的图形,我们使用 Cairngorm 3 Observer 库。基本上,它监听表示模型中的一些变化,并允许您在视图中执行函数。
请参阅文档
Every part of code that do some graphical stuff (drawing border, setting style, drag & drop management, animations, ...), should be included in the view, not the presentation model.
For this kind of graphical that stuff should be executed after a property has been changed in the PM, we use the Cairngorm 3 Observer lib. Basically, it listens to some changes in the presentation model and allows you to execute a function in the View.
See the documentation