Real quick test this week due to lack of time on my part, but still something that puzzled me somewhat coming from a CF background.
Quite simple. How do I evaluate something in Flex?
e.g The equivalent in CF:
Real quick test this week due to lack of time on my part, but still something that puzzled me somewhat coming from a CF background.
Quite simple. How do I evaluate something in Flex?
e.g The equivalent in CF:
Another puzzle down, and again, a whole load of good answers and feedback. Hopefully some of you are learning something from this.
The answers I am using today are from Kevin Hoyt as they provide the clearest examples of the way to do things. They are also the closest to what I had. This code can be downloaded in a zip file from here.
Ok, lets break this down into two parts. The “Please Select” prompt, and the formatting of the select items.
1) Please select
Some of you spotted the very easy way of doing this, i.e via the mx:ComboBox “prompt” attribute. This gives you the ability to provide a string as the selected element until another item is selected. At this point, the Prompt value is removed.
So what if you want to have the item showing all the time, i.e you want to add an element that has meaning, but is not returned from the webservice? Well, this is where we need to treat the combo as what it really is, an array of objects. By manipulating this array, we can perform pretty much anything we want on it. We can reformat values, we can add or remove values etc etc. If you take a look at the source of squirrelFixed.mxml you will notice a function doPrompt(). This function creates a new object, then sticks it into element 0 of the categories arrayCollection. Due to the bindings, this essentially has the same effect as adding it to the combo.
So why the arrayCollection? Well, these come in handy for quite a few things, mainly being able to manipulate some data without having to do anything fancy such as reinvoking a webservice. Try to think of an array collection as an array of structures in CF.
2) Reformatting the options
Easy one this, that a lot of people made extra complicated for themselves. The key? The labelFunction attribute of mx:ComboBox. This little gem lets Flex redirect an object to a given function that then returns a string containing the label. The benefit of this is that you do not need to pre-process this label, or save it off anywhere in another object. You simply process the labels when you need to see them. Cool huh? For an example, take a look at squirrelPrompt.mxml and categoryMerge(). Notice that this is only talking in the context of the single object passed in (which happens automagically).
So in summary, I hope that this has pointed out that Flex usually provides a neat simple way of doing most things, you just need to look through the API's. And secondly, that if something doesn't quite fit, you can take it on yourself and do it your own way.
Stay tuned next week for the next test.
Here's another little puzzle to pass the time.
Using the numbers 8, 8, 3 and 3, and only using addition, subtraction, multiplication, division and parantheses get the answer of 24 (exactly)
OK, here's the next Flex test. I've had some feedback on the first one and decided to do these tests surrounding a common theme, i.e Feed-Squirrel and the webservices that it provides.
This weeks test is quite simple. Last week you created a combo containing the information from the FS Webservice. Now, using the same webservice, and using the same Combo, bind the data in a similar way, but with some tweaks:
Firstly, add a “Please Select” element to the start of the list, and secondly, prefix each item with it's categoryID. So, you should end up with the following in your combo:
Please Select
1. Coldfusion
2. Flex
3. Flash
4. Technology.
When I receive a valid answer (comments or email will do), I'll post it and try to write a coherent explanation.
Go!
OK, from the email I have received, a few of you have tried this, some with some success, and some without. The main problem people were having is related to the way of thinking that a traditional CF/HTML developer has. In HTML a select consists of values and labels:
bar
However, in Flex, this isn't the case, each item of a select (or combo to give it it's correct name) is an object. This essentially means you can chuck pretty much anything you want at the combo, you just need to tell it what part of the object is the label value. (Remember, I said this would be enlightening).
So if we take some mxml (which answers the question):
You will see a webservice being called when the application has finishing loading, this then returns a load of objects, which are in turn bound to the combo's dataprovider. To get a label simply point Flex at the item in the object that represents your string label (in this case CATEGORYNAME) using the labelField attribute. This will give you your Combo with your labels.
So how do we get at the data? Well, remembering these are objects in the combo, the data is right there in each items relevant object (so in theory you can have loads of data fields in one combo!). To demonstrate this we have a label bound to the selected items CATEGORYID.
So, now you know. If you are a HTML/CF developer this would have probably been quite hard for you to figure out, due to the fundamental way this is different to HTML (which is partly the point of this test being first, as it is such as big difference and had me stumped for ages). If you knew Flex already, it should have been a walk in the park.
Incidentally, does anyone have any feedback on these tests? Do you want me to publish any more?
OK, I'm going to be setting a few little tests that I have found difficult while picking up Flex in the hope that the knowledge will spread outwards. Every now and then, one of these puzzles will appear. Just don't expect to win anything.
So, here's puzzle No 1:
- Using the feed-squirrel webservice (details at http://www.feed-squirrel.com/webservices/fswsdl.htm) create a combo box that contains the category's available, with the data being the category ID and the label being the name. You can use mx:WebService or mx:RemoteObject, I don't mind.
Now, admittedly if you know Flex this will be easy, but if you don't it can be quite tricky (but enlightening).
Game on.
UPDATE: Yup, I forgot the crossdomain.xml. Schoolboy error on my part. Thanks to Kevin Hoyt for pointing it out.
OK, maybe not for everyone, but a few days ago I got playing with Zazzle, an on-line tee store where you can make your own and sell them online.
So, after a few minutes I came up with the following. As far as I know you can buy these in any style, colour and size, and if response is any good, I may just make more.
![]() I love Web 1.0 |
![]() Eat My Nuts |
![]() Don't Panic, I speak fluent geek |
![]() Don't Panic, I'm a programmer |
![]() “i” |
|||