【问题标题】:Flex - Issue with binding in script blocksFlex - 脚本块中的绑定问题
【发布时间】:2010-01-15 00:23:16
【问题描述】:

我正在开发多文件上传器,并希望根据我的数据网格中选定的 questionID(即目录名称)设置上传目录。

代码可以在这里找到http://pastie.org/784185

类似这样的:

我已经设置了 myQuestionID(要上传到的目录),所以它是可绑定的(第 136-137 行):

[Bindable] public var myQuestionID:int;

在我的数据网格中,我使用了一个更改处理程序(第 539 行):

change="setQuestionID();"

我们在 setQuestionID 函数中设置变量(第 400-407 行):

[Bindable (event="questionChange")]          
private function setQuestionID():void
{
     myQuestionID = questionsDG.selectedItem.QuestionID;
     dispatchEvent(new Event("questionChange"));
}

然后尝试在我的上传脚本中使用它(第 448-475 行):

// initUploader is called when account info loads
public function getSessionInfoResult(event:ResultEvent):void{           

        // Get jsessionid & questionid (final directory) for CF uploader
        myToken = roAccount.getSessionToken.lastResult;             
        // BUG: myQuestion is null in actionscript, but okay in form.

        var postVariables:URLVariables = new URLVariables();
        postVariables.jsessionid = myToken;
        postVariables.questionid = myQuestionID;            

        multiFileUpload = new MultiFileUpload(
                    filesDG,
                    browseBTN,
                    clearButton,
                    delButton,
                    upload_btn,
                    progressbar,
                    uploadDestination,
                    postVariables,
                    350000,
                    filesToFilter
                    );

         multiFileUpload.addEventListener(Event.COMPLETE,uploadsfinished);         
}

我可以在我的 MXML 中看到绑定的值(第 639 行):

<mx:Label text="{myDirectory}"/>

当我单击数据网格中的一行时它会更新。但是,如果我尝试在任何操作脚本中访问此 myQuestionID 值,它将显示为 null (0)。我知道我的上传器正在工作,因为我可以将 myDirectory 硬编码到一个已知目录,它会上传正常。

我真的很难过。

【问题讨论】:

  • 请提供更多代码
  • 这一行出现在哪个函数myDirectory = myDataGrid.selectedItem.someID;?您如何/何时调用该函数?
  • 你能把文件贴在馅饼或要点上吗?看起来它应该可以工作,但可能是范围问题或您认为不会影响它的事情。

标签: apache-flex data-binding binding


【解决方案1】:

questionid = null 的原因是 getSessionInfoResult() 在设置 myQuestionID 的绑定值之前由您的初始化代码调用。

所以您的文件上传器 (multiFileUpload) 已经用 myQuestionID = null 进行了实例化。

您需要在设置后将值实例化/传递给 multiFileUpload 组件。

【讨论】:

  • 是的,就是这样 - 谢谢约翰。我忘记了在应用程序的 init() 中,如果在函数内部,所有值都是一成不变的,并且如果您希望变量是动态的,则必须摆脱它。
【解决方案2】:

每次用户更改选择时,使用 dataGrid 更改事件设置 myDirectory。这将正确更新 myDirectory 的值。

如果您不想在 DG 上使用更改事件,通过将 someID 设置为 Bindable 将主要解决您的问题

【讨论】:

    猜你喜欢
    • 2010-12-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-15
    • 1970-01-01
    相关资源
    最近更新 更多