2011-08-22 19 views
1

我使用的是Parsley框架。我試圖在自定義可視化樹組件中注入模型;Visual Component中的注入模型

private var _model:Model 

[Bindable] 

public function get model():Model 
{ 
    return _model; 
} 

public function set model(value:Model):void 
{ 
    _model = value; 
} 

構建配置:

<Object id="customTree" type="{CustomTree}"> 
    <Property name="model" idRef="model"/> 
</Object> 

然後我在MXML使用此樹:

<components:CustomTree 
     id="categoriesTree" 
     width="100%" height="100%" 
     labelField="@title" 
     right="0" bottom="0" left="0" top="10"   
     doubleClickEnabled="true" 
     maxHorizontalScrollPosition="250" 
     horizontalScrollPolicy="auto" 
     dragEnabled="true" 
     dropEnabled="true" 
     dataProvider="{model.dataHolder}" 
     /> 

我曾嘗試重寫父函數,我已經得到了一個錯誤。 (模型爲null); override protected function dragDropHandler(event:DragEvent):void { model.action = "drop" }

我在模型setter中設置了斷點,它被執行但模型仍然爲空;

問題在哪裏?

+0

您發佈的代碼沒有意義。請全部顯示。此外,你並沒有注入任何東西。 –

+0

我在Build Config中省略了模型的配置。 – nkukhar

回答

1

我發現如何解決這個問題。如果我們嘗試在可視組件中注入smth,我們應該像配置可視組件一樣配置它。

public class CustomTree extends Tree 
{ 

public function CustomTree() 
{ 
    super(); 
    this.addEventListener(Event.ADDED_TO_STAGE, configure); 
} 

protected function configure(event:Event):void 
{ 
    this.dispatchEvent(new Event ('configureIOC', true)); 
} 

... }

MB有人有一些其他的解決辦法?

0

不確定您是否想讓歐芹實例化您的CustomTree。 將模型注入到視圖中,並讓mxml中的CustomTree實例綁定到模型。

配置:

<Object id="model" type="Model"/> 

MXML:

<mx:Script> 
     <![CDATA[ 
[Inject(id='model')] 
[Bindable] 
public var model:Model; 
]]> 
    </mx:Script> 

<components:CustomTree 
     id="categoriesTree" 
     width="100%" height="100%" 
     labelField="@title" 
     right="0" bottom="0" left="0" top="10"   
     doubleClickEnabled="true" 
     maxHorizontalScrollPosition="250" 
     horizontalScrollPolicy="auto" 
     dragEnabled="true" 
     dropEnabled="true" 
     dataProvider="{model.dataHolder}" 
     /> 

你不需要注射的id,你可以按類型注入,只是下降的Inject標籤和配置的ID爲模型。

+0

認爲我迷惑了你。我需要在CustomTree中注入模型,而不是在MXML組件中。我有類 - CustomTree.as在這個類內,我用模型執行一些操作。然後我使用mxml中的CustomTree。而且,我知道我可以不用身份證注射。但是,感謝信息。 – nkukhar