【问题标题】:In PureMVC, where is the correct place to put Keyboard listener that controls a view?在 PureMVC 中,放置控制视图的键盘侦听器的正确位置在哪里?
【发布时间】:2011-01-26 19:03:46
【问题描述】:

我获得了一些 PureMVC 经验,我想使用键盘命令来控制我的视图。应用程序的其余部分不需要知道这个视图在做什么。

我应该将它们直接放在视图中,还是应该将它们放在其他地方并在按下键时使用通知通知视图?

谢谢!

【问题讨论】:

    标签: flash actionscript-3 actionscript puremvc


    【解决方案1】:

    正如您所说,您有两种选择 - 将一些侦听器放在 view.mxml 类中,或者将侦听器放在某个通用类中。

    1-st - 这似乎是正常的方法,无需进一步解释,每个程序员都会这样做。

    第二种方法更有趣。如果你有很多视图,监听键盘事件,你会开始使用类似的东西

    public class EnterButtonPressed extends SimpleCommand 
    {
      function execute(...):void
      {
        //do something with the model, and then notify the view
      }
    }
    

    但是在添加了更多应该监听Enter 键的视图之后,你的类最终会变成这样

    public class EnterButtonPressed extends SimpleCommand {
      function execute(...):void
      {
        switch(viewType)
        {
          case view1:
            //do something with the model, and then notify view1
            break;
          case view2:
            //do something with the model, and then notify view2
            break;
          case view3:
            //do something with the model, and then notify view3
            break;
          case view4:
            //do something with the model, and then notify view4
            break;
          ...
      }
    }
    

    如果您听到许多键盘事件,这似乎很糟糕。但是如果你熟悉设计模式,你可以使用State Pattern

    当我遇到许多不同的视图状态监听许多事件时,它在我的最新项目中帮助了我很多。

    我也推荐你看看Mate 框架,它就像 PureMVC + 数据绑定 + Flex 事件。

    【讨论】:

    • 谢谢!很好的答案。我把它放在视图中并不再担心它,但如果需要灵活性,很高兴知道其他方式也是可以接受的。
    猜你喜欢
    • 1970-01-01
    • 2016-08-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多