Array vs. ArrayCollection in Flex 2 (with a ColdFusion Slant)

本文探讨了Flex开发中Array与ArrayCollection的区别及其应用场景,特别是在与ColdFusion集成时如何利用ArrayCollection来提高数据处理的灵活性。
I've been spending a bit of personal time digging into Flex 2 development. One thing I've noticed up front is that there's an important distinction between using an Array and an ArrayCollection when coding your applications. This is an important point for ColdFusion developers because, in CF, we only have an Array datatype while most other languages (Java, C#, etc.) have an explicit ArrayCollection class. Further, since Flex is simply a front-end development language, you're likely to be doing lots of talking to a back-end technology to return data sets and the like.

So what's the difference? Well, the LiveDocs entry for the ArrayCollection class sums it up pretty well, so here it is:

 

The ArrayCollection class is a wrapper class that exposes an Array as a collection that can be accessed and manipulated using the methods and properties of the ICollectionView or IList interfaces. Operations on a ArrayCollection instance modify the data source; for example, if you use the removeItemAt() method on an ArrayCollection, you remove the item from the underlying Array.

The first sentence in that explanation is the key: The ArrayCollection class is simply a wrapper around the Array class. So if they're both Arrays at some level, why is this important? Well, with a standard Array, what you have available to you are the basic methods and properties that you'd expect in any language: push() (for adding an element to the end), pop() (for removing the last element), length (the number of indices), etc. However, the ArrayCollection class provides a suite of immensely convenient "extra" methods that can act on the Array.

For example, say I have an array with the values in the color spectrum:

 

var spectrumColors:Array = ["red","orange","yellow","green","blue","indigo","violet"];

(Quick side note to ColdFusion developers: Arrays in ActionScript are zero-based. So, to directly access the element "red", you'd use spectrumColors[0] rather than spectrumColors[1] like you would in ColdFusion.)

However, Stephen Hawking releases a new theory that there's no such thing as "green," and mandates that it should be removed from the color spectrum. How would you do it using the Array class? Honestly, it's not worth getting into the code because it's a waste of keystrokes. However, there's a really easy way to do this: Use the ArrayCollection class. The ArrayCollection class extends the ListCollectionView class. Without getting into the boring details, the ListCollectionView class has a bunch of extremely handy methods for manipulating its elements, namely the addItemAt(), removeItemAt(), and getItemAt() methods. Going back to our example, if the spectrumColors variable is datatyped as being an ArrayCollection, accomodating Mr. Hawking becomes immensely easier:

 

var spectrumColors:ArrayCollection = ["red","orange","yellow","green","blue","indigo","violet"];

spectrumColors.removeItemAt(spectrumColors.getItemIndex("green"));

All the code above does is create the spectrumColors variable of type ArrayCollection, and then removes the item from the array based on the returned index value that matches the value "green." See? Easy. No need to manually loop over the array to find an index or verbose constructs like that.

OK, so I promised a ColdFusion slant in the post title. Now that you have an understanding of Array vs. ArrayCollection, let's see how this fits in to ColdFusion and Flex.

Flex is all about providing a rich user interface for our users. However, the key phrase in that sentence is "user interface." Flex does not provide any native database connectivity, so you need a back-end technology to do the "heavy lifting" and return the results to Flex for display. The ColdFusion team did a phenomenal job of making Flex/ColdFusion integration as seamless as possible in the 7.0.2 release. However, there are certain nuances that we need to keep in mind when working between these two technologies. One such nuance is the fact that it's one thing to query for data and return it as-is to the calling environment (ColdFusion), but it's another to be able to use advanced concepts such as run-time sorting and filtering for display (Flex). To that end, certain Flex 2 framework components use what are known as data providers to not only display the result set, but to further allow the user to manipulate the results to their liking. To that end, the Flex engineers cleverly implemented these framework components such that you could either use the raw datatype returned or you could use a collection instead, which allows for more advanced access to the data set. Hopefully my above explanation regarding the difference between an Array and and ArrayCollection sufficiently shows the difference between a datatype and its Collection wrapper class. You could use a basic Array (the raw datatype) to display a result set, if that's all you want to do. But to provide more advanced interaction, these raw datatype classes don't provide the necessary advanced access or event broadcasts necessary. So you're going to need to convert the datatype that comes back from your back-end (i.e., ColdFusion) into the appropriate collection type so that it can then be used for display, thereby offering some of the more advanced features that the Flex framework provides.

While there are many controls that use data providers, I'm going to use the DataGrid in my example because that seems to be the most common way of displaying tabular data in Flex. If I want to display a datagrid consisting of some basic company information: company name, address, etc., below is the ActionScript code needed to take the returned results into the appropriate collection datatype. Assume that a ColdFusion query is being returned.

 

<? xml version="1.0" encoding="utf-8" ?>
< mx:Application  xmlns:mx ="http://www.adobe.com/2006/mxml"  layout ="vertical"  initialize ="myService.send()" >

< mx:Script >
<![CDATA[
   import mx.collections.ArrayCollection;
   import mx.rpc.events.ResultEvent;
   
   [Bindable]
   private var companyInfo:ArrayCollection;
   
   private function resultHandler(event:ResultEvent):void {
    companyInfo = event.result as ArrayCollection;
   }
]]>
</ mx:Script >

< mx:HTTPService  id ="myService"  url ="url/to/cfc?WSDL"  result ="resultHandler(event);"   />

< mx:DataGrid  dataProvider ="{companyInfo}"   />

</ mx:Application >

As you can see the above code simply takes the query returned from ColdFusion ("event.result") and "coerces" it (or casts it) to an ArrayCollection using the as keyword. The framework takes care of everything else for you. Then I simply assign the companyInfo variable as the data provider for the DataGrid. If I now wanted to add more advanced functionality, such as filtering or editing, to the result set, I already have the results in the appropriate format. I just need to write a function that acts as the filter or "listens" for the updated data. The Adobe LiveDocs resource provides a great example of providing this sort of functionality.

Hopefully this provides a basic understanding of the difference between datatypes and their associated Collection classes, and when each one should be used.

 

标题基于Python的自主学习系统后端设计与实现AI更换标题第1章引言介绍自主学习系统的研究背景、意义、现状以及本文的研究方法和创新点。1.1研究背景与意义阐述自主学习系统在教育技术领域的重要性和应用价值。1.2国内外研究现状分析国内外在自主学习系统后端技术方面的研究进展。1.3研究方法与创新点概述本文采用Python技术栈的设计方法和系统创新点。第2章相关理论与技术总结自主学习系统后端开发的相关理论和技术基础。2.1自主学习系统理论阐述自主学习系统的定义、特征和理论基础。2.2Python后端技术栈介绍DjangoFlask等Python后端框架及其适用场景。2.3数据库技术讨论关系型和非关系型数据库在系统中的应用方案。第3章系统设计与实现详细介绍自主学习系统后端的设计方案和实现过程。3.1系统架构设计提出基于微服务的系统架构设计方案。3.2核心模块设计详细说明用户管理、学习资源管理、进度跟踪等核心模块设计。3.3关键技术实现阐述个性化推荐算法、学习行为分析等关键技术的实现。第4章系统测试与评估对系统进行功能测试和性能评估。4.1测试环境与方法介绍测试环境配置和采用的测试方法。4.2功能测试结果展示各功能模块的测试结果和问题修复情况。4.3性能评估分析分析系统在高并发等场景下的性能表现。第5章结论与展望总结研究成果并提出未来改进方向。5.1研究结论概括系统设计的主要成果和技术创新。5.2未来展望指出系统局限性并提出后续优化方向。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值