Flex Data Binding traps (i)

Source: Internet
Author: User

Flex Data-binding traps: common misuse and errors

When building a flex or Adobe AIR program, automatically passing the value of one object to another is one of the most common and useful features of data binding.

However, data binding slows down the initialization of the program, and when developers do not fully understand how the data binding mechanism works, it can cause some unexpected problems for developers.

The best solution is to make sure you use it correctly and use it only when you need it. In this article, I've compiled 10 of pitfalls and errors common to developers using data binding when building apps.



First article:

Hard to find silent error



Some of these binding operations seem to have worked, but you finally find yourself frustrated by the failure to change the example.



Exceptions and errors that are thrown when binding an expression or invoking a binding method in a binding framework are only silently captured. Therefore, you will not see any run-time errors in the Flash Player debug version. Not only does the binding not work,

And there are no errors displayed. Why are these errors captured silently?



Code implements a binding mechanism that requires several prerequisites before binding occurs. The binding mechanism eats up any errors to prevent runtime exceptions from being thrown at run time.

This is a good thing if you don't want to see these unexpected errors in your program.



Consider the following simple binding example:




      1. <?xml version= "1.0" encoding= "Utf-8"?>

      1. <s:application xmlns:fx= "http://ns.adobe.com/mxml/2009"

      1. Xmlns:s= "Library://ns.adobe.com/flex/spark"

      1. xmlns:mx= "Library://ns.adobe.com/flex/mx"

      1. Minwidth= "1024x768" minheight= "768"

      1. Preinitialize= "Handlepreinitialize ()" >


      1. <fx:Script>

      1. <! [cdata[

      1. [Bindable]

      1. private Var xml:xml=

      1. <users>

      1. <user>

      1. <name>EladElrom</name>

      1. <address>1 Wall street</address>

      1. </user>

      1. </users>;


      1. protected function handlepreinitialize (): void

      1. {

      1. XML = NULL;

      1. Bindingmanager.debugbinding ("Label.text");

      1. }

      1. ]]>

      1. </fx:Script>


      1. <s:label id= "Label" text= "{xml.user.name}"/>


      1. </s:Application>



Copy Code

I have added an XML variable bound to the label component. The code will work fine, but I'll set the XML variable to NULL when the component is pre-initialized.

This event will be dispatched before the initialization sequence of the component is started, and the label component is not yet set. This XML variable is set to NULL, so there is no Name attribute on the XML object.

If you run this program, you will find that the bindings did not occur and no errors were prompted.



Debugging bindings



Although the error is silently captured, you still have a way to find out what is going on.

Debugging with Bindingmanager.as and binding.as is not easy, because the binding class is not available if you do not download the full Flex SDK.



Instead, you can set a breakpoint and track the associated binding object to find out where the problem is. In this case, you will find that the value of the XML object is set to NULL, which is why the binding failed (see Figure 1).



Figure 1. The value of the XML object is set to null





The other approach is more intuitive and is a debugbinding method that uses the Bindingmanager class.

You set the components and properties you want to observe, and then you can see the errors that were caught.



In the example code above, I added a comment to the following line of code:




      1. Bindingmanager.debugbinding ("Label.text");



Copy Code

Remove the comment from the line and run it in debug mode; you will see a binding error in the console (see Figure 2)



Figure 2: Binding errors for console output





Take a look at the code for the binding.as and bindingmanager.as classes. The code contains many Try...catch expressions to ensure that the conditions for effective binding are formed.

The following are some of the types of errors that may be thrown during binding:



* Error #1006: Call attempted in an object, is not a function.

* Error #1009: Null has no properties.

* Error #1010: Undefined has no properties.

* Error #1055: Has no properties.

* Error #1069: Property-not found On-and there is no default value



* ERROR #1006: The object attempting to invoke is not a function.

* ERROR #1009: null no attribute.

* ERROR #1010: No attributes are defined.

* ERROR #1055: no attributes.

* ERROR #1069: attribute-not found-and no default value



If any of these errors occur, the bound objects will silently catch them and no longer implement the bindings.

You can see these errors by using the debug options for binding management.

Binding management captures other errors in addition to capturing these errors.

This article from: http://blog.csdn.net/gongyongxing/archive/2010/08/10/5801472.aspx

Flex Data Binding traps (i)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.