![](/img/trans.png)
[英]Parsley dependency injection problem with Flex 4 using Presentation Model pattern
[英]Injection model in Visual Component
我正在使用Parsley框架。 我試圖將模型注入自定義可視樹組件中;
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}"
/>
我嘗試覆蓋父函數,但出現錯誤。 (模型為空); override protected function dragDropHandler(event:DragEvent):void { model.action = "drop" }
我在模型設置器中設置了斷點,該斷點已執行,但模型仍然為空;
問題出在哪兒?
我發現了如何解決此問題。 如果嘗試在可視化組件中注入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有人有其他解決方案嗎?
不確定您是否想讓歐芹實例化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,您可以按類型注入,只需從模型的注入標記和配置中刪除ID。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.