data mangling n.
Skip this Video
Loading SlideShow in 5 Seconds..
Data Mangling PowerPoint Presentation
Download Presentation
Data Mangling

Loading in 2 Seconds...

play fullscreen
1 / 42

Data Mangling - PowerPoint PPT Presentation

Download Presentation
Data Mangling
An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

  1. Data Mangling The key to data-management within your Flex Applications

  2. Data In Flex • Flex, along with just about every other programming language stores local variables, or data in memory. • In Flex’s case, all the variables are stored on the client’s local machine in RAM. • A variable's memory space needs to be initialized before it can be used, so it is cleaned and reserved for that piece of data.

  3. Data in Flex • Because we are working on the client’s machine, we need to be weary of the amount of RAM we are using for our operations • We will need to choose the BEST solution for each need to store memory • Larger memory use = slower application • We don’t have control of the amount of RAM that is available to our application.

  4. Introduction to Variables • Variables have 4 properties that we need to worry about:

  5. Introduction to Variables • Scope: • The scope will define how much “reach” a variable has. This will also determine how long a variable will “live” and where it is initialized. • Private Variables : Private variables only live within the object that created them, and are only accessible from within that object. This variable get initialized within a Script block that is on the same level as the root of the object creating it.

  6. Private Variables • Private Variables should be your most-use variable for storing regular data within your application. • Private data will no longer be accessible when the object that created it is no longer accessible. In this example, when the Application closes.

  7. Public Variables • Public Variables only live within the object that created it, however, they are accessible from any object that has access to the variable’s parent object. • This variable get initialized within a Script block that is on the same level as the root of the object creating it. • Useful as getters and setters of your object

  8. Public Variables • Other objects are able to get and set Public variables at their own whim. There is no scrubbing of data when these are touched. • Public Variables are no longer accessible when the object that creates them is no longer accessible.

  9. Local Variables • Local Variables are used within Functions. • They are NOT accessible to anything outside the function, and become inaccessible as soon as the function is finished. • They are declared within the header of a function, and within the functions themselves.

  10. Local Variables • Variables in the header of a function (the data1 variable) and variables declared in the function expire at the same time • Avoid using public or private variables to work with data used exclusively in a function.

  11. Variable Types • Since Flex is a strongly “typed” language, it requires that variables be declared with a type • This allows the compiler to optimize the storage of that data, and expose some additional functionality of that variable type • Types = Classes

  12. Basic Variable Types • Boolean: Stores either True or False • Int: Integer. Stores whole numeric values • Number: Stores any numeric values* • Beware of minimum and maximum values! • String: Stores Text • uInt: Unsigned Integer. Whole, absolute numeric values (Any number 0 and above)

  13. More advanced Types • Array: Stores a collection of data. The basis of the ICollection and the ArrayCollection • Null: Stores nothing. Nada, Zip, Zilch. Simply a placeholder. • Date: Stores Date and time information • XML: Stores clean, parsed XML data • Object: More about this later

  14. The Object • The Object type is the bases of all types, and can hold ANY data • Often used to store “structures” of data, or simply creating your own data type

  15. The Object • Because the Object type is so generic, it is very inefficiently compiled • It also offers NO compile-time help! • However, it can be very useful if you need to package up data and send it to something else that is un-typed • If the compiler can’t sense a type of data, it classifies it as an Object

  16. The Object

  17. Setting Values • In order to declare a variable, you don’t need to set a value. • A variable will use its default value if you don’t. • NaN stands for “Not A Number” and is not eligible for any arithmetic

  18. Setting Values • Obviously, you can’t set a value to a variable that the variable can’t support • Setting the value of “Hello” to an integer just doesn't make sense. • If you do this during compile type, you will get a compile error. • If you do this during runtime, you will get an error box.

  19. Setting Values • So what if you want to convert one data type to another? • This is known as “Casting” • Casting can be accomplished by either setting a variable “constructor” with your variable, or by using the “as” keyword. • Use the “as” keyword if the types are related • A Constructor will work for some “sane” conversions.

  20. Setting Values • To find out what types are related (built upon), check the docs or the Flex API Posters.

  21. Enhancing Your Variables • So, now you’ve got your data stored in your application, you may want to enhance it. • You can introduce “Metadata” to the variables • Such as [Bindable] or [Managed] • The Bindable Metatag tells the class that hosts the data to let others know when the data has changed.

  22. Binding your data • Making your data bindable allows you to easily display and modify your data using the common Flex controls. • For example, if you wanted to show the string below in a <mx:Text> tag, you would: <mx:Text text=“{myBindableData}”/>

  23. Binding your data • At the same token, if you modify the data in the binded control, it will update your variable automatically with the new result. • You can also bind multiple bits of data in one string, or make expressions :

  24. Models • So, lets say you want to store a bunch of related data, but don’t want to keep track of a ton of different variables? • You need a variable that is accessible to the entire component / application you are working on (public scope) • You can use the <mx:Model> tag!

  25. Models • Models, allow you to, well, model your data structure. • Stores data in an untyped object • Very verbose creation, but also very readable XML.

  26. Models • Again, you can access your data via the dot notation. {} • You can read/write data into this model just like any other variable – you don’t need to setup default data. • Like a generic object, there is noway to scrub the data before it isstored.

  27. Custom Classes • So, now we got the basics of the structures of data, how do we create our own “types” of data? • We can easily create a custom class! • Custom classes, while requiring a bit more work than a Model, allow for typing of data, and the ability to scrub data before and after it is stored.

  28. Custom Classes • To create the custom class: • (Optional) Create a directory to store your custom class • Make sure this directory is not a reserved word! • Right click where you want to store your Class and click New -> ActionScript Class

  29. Custom Classes • Project: The project you want to create the class in. • Package: This is the directory, or the collection of classes you want this to be grouped with. • Name: The name of the Class.

  30. Custom Classes • Modifiers: This allows you to make this a purely internal class (only callable from within the package) • Superclass: If you want to “extend” another class, you can select it here.

  31. Custom Classes • When you click “Finish”, a new ActionScript file will be created for you. • This is the basis of your new class; now you can begin implementing the properties that you wish to store. • In the public class ClassName {} function, declare your variables as you did in the past.

  32. Custom Classes • All you need now is a “Constructor”, or a function that is called to initialized all the variables. • This constructor is only called once per time that this class is initialized.

  33. Custom Classes • Create the constructor by declaring a Public function, with a return type of Void. It can take as many or as few parameters as you wish.

  34. Custom Classes • You may also want to implement the “toString” function, to be able to output the results of your class to the debugger or the screen • The toString function takes no parameters and returns a string.

  35. Custom Classes • As you may have noticed in the toString() function, we had accessed the variables using the “this” keyword. • The “this” keyword always refers to the local variable scope of the object that you are working in.

  36. Get / Set Methods • So, now you have a basic class, you may want to control how data is sent to you, and be able to scrub data before it leaves. • This is in the form of a public function with the keywords of get or set. • This allows us to set variables in our classes just like they were any other variable: theUser.age = 24; trace(theUser.ofAge);

  37. Get / Set Methods • These functions will return the ofAge variable to the end user, and will also set it properly when the age is set. • This allows you to control what data goes in and what goes out.

  38. Custom Classes • But you don’t have to only expose just your variables, you can create other functions.

  39. Debugging • Debugging all these variables is simple. • Just run your program in “debug” mode, and set a break point. • In the upper-left corner of your application, you will see all the variables you have in your application • Custom Classes will show up as structures.

  40. Debugging (Demo)

  41. Any Questions?

  42. Giveaways! • ActionScript 3.0 Cookbook • Thanks to O’Reilly Publishing • Flex 2 API Posters • Thanks to Adobe Developer Relations • Don’t forget, we have our BIG drawings in June! • Up to $2,000 of Adobe Software • Other Vendors