代码之家  ›  专栏  ›  技术社区  ›  raphonic

从另一个组件修改组件的状态

  •  3
  • raphonic  · 技术社区  · 11 年前

    我试图在回调中设置子组件的父组件的实例变量。使用调试器,我可以看到实例变量在回调中设置正确,但在呈现子组件时,子组件不会反映所做的更改。

    那么,从海边的另一个组件修改组件的状态是违法的,还是我做错了什么?

    示例代码:

    MyParentComponent>> initialize
        super initialize.
        child := MyChildComponent new.
    
    MyParentComponent>> renderContentOn: html 
      html render: child.   
      html anchor
         callback: [ 
            child property: 'Something'.
        ] ; with 'Navigate'.
    
    MyParentComponent>> children 
      ^ Array with: child
    
    2 回复  |  直到 11 年前
        1
  •  4
  •   Damien Cassou    11 年前

    你错过了一些 super initialize 我想是在父组件中。

    我还建议你不要这样工作。

    做一个 MyParentComponent>>child 具有

      ^ child ifNil: [ child := MyChildComponent new ]
    

    另外,不要做 html render: child 但是 html render: self child . 这样,您就可以轻松地交换组件。

    这样,您就可以确保子项已正确初始化。

        2
  •  1
  •   raphonic    11 年前

    经过一番试验,我发现了问题。在其中一种渲染方法中,我在每次渲染页面时都创建一个新组件,而不是重复使用在 initialize 方法

    另一个组件用于导航,我根据选择的菜单设置要显示的主组件。

    所以很明显,修改州在海滨并不违法。