Sunday, January 21, 2007

New version of RemoteList (IList implementation that support paging)

This is another post about implementation of remote list for Adobe Flex 2.

As I mentioned in previous post Flex framework provides ItemPendingError exception mechanism to use when collection data is not available. For example DataGrid component provides support for this exception. I didn't look too deeply in DataGrid code but my understanding that if ItemPendingError is thrown DataGrid would retry load the data by providing hooks into the ItemPendingError. I believe that all List based components should behave similary.

Given all these built-in support I realized it's much better to rely on ItemPendingError mechanism then event notification as I did in my previous post.

New version of RemoteList is based on old one with one exception - instead of dispatching CollectionEventKind.REPLACE to notifiy view (DataGrid), I throw ItemPendingException.

There are 3 main pieces:
IRemoteDataSet - interface that defines how to get remote data (RemoteDataSet a sample implementation)
RemoteList - class that uses IRemoteDataSet to get data and ItemPendingError mechanism to notify DataGrid when data is available
ItemPendingErrorEx - extends ItemPendingError to add dataReady/error methods.


I extended ItemPendingException by adding new method:


public function dataReady(data:Object):void
{
if (responders)
{
responders.forEach(
function(item:IResponder, index:int, arr:Array):void
{
item.result(data);
});
}
}




(I am somewhat surprised that similar method is not included by default)

This method iterates through all IResponders and notifies them when data is ready. IResponders would be added by DataGrid component so I don't need to do anything else here.

I also updated miss() method in the RemoteList class:

/**
* Called when we need to load a new page of data and throws a new exception for first request or
* rethrows already created exception if data loading already in progress.
*/
public function miss(index:int):void
{
var item:Object = localData[index];
if (item == null)
{
var page : Number = Math.floor(index / pageSize);

if (pagesPending[page] == null)
{
remoteDataSet.loadData(page, pageSize);

var loadingEvent:Event = new Event("Loading");
dispatchEvent(loadingEvent);

var error:ItemPendingErrorEx = new ItemPendingErrorEx("Loading Data ...");
pagesPending[page] = error;
throw error;
} else
{
throw pagesPending[page];
}
}
}



The method would throw an ItemPendingErrorEx to let notify DataGrid that data is loading.

When data is ready onDataReady() method is called by RemoteDataSet class:


public function onDataReady(loadedData:Object):void
{
var result:Array = loadedData.result[0]["value"].toArray();
var page:int = loadedData.result[1]["value"];

var error:ItemPendingErrorEx = pagesPending[page];

pagesLoaded[page] = true;
var beginIdx : Number = page * pageSize;

localData.splice.apply(localData, [beginIdx, result.length].concat(result));

if (cleanData)
{
cleanInvisiblePages(page);
}

trace("localData is updated: " + beginIdx + "-" + (beginIdx + result.length) );

error.dataReady(result);

if (isDataInit == false)
{
isDataInit = true;
dispatchResetEvent(); // make columns are initialized
}

var dataReadyEvent:Event = new Event("DataReady");
dispatchEvent(dataReadyEvent);
}



Here ItemPendingErrorEx.dataReady method is called that in turn would call DataGrid's supplied listener to notify that data is ready and DataGrid can redraw the rows.


You can download all code from here.
(Still need to add support for sorting)

6 comments:

Cosma said...

Nice work, I started from your code to implement a similar behaviour, thanks.

However, it seems that it will not be easy to support sorting without switching to ICollectionView and IViewCursor interfaces..

RD said...

Still didnt' have chance to implement sorting, my current thinking just to do 'reset' when sort is clicked - meaning invalidate all current data and start over by loading first 100 rows based on new order (which of course means that you would lose you current row selection)

side note: I am in process moving my blog into new place: http://blog.widget-labs.com/ and will post any updates there.

Morgan said...

I really like the work you have done with this and I wonder if you have had time to implement sorting. Thereby saving me even more time :-)

//M

伊凡姐姐 said...

酒店打工

酒店兼職

台北酒店

打工兼差

酒店工作

酒店經紀

禮服店

酒店兼差

酒店上班

酒店PT

酒店

酒店上班請找艾葳 said...

艾葳酒店經紀公司提供專業的酒店經紀, 酒店上班小姐,八大行業,酒店兼職,傳播妹,或者想要打工兼差打工,兼差,八大行業,酒店兼職,想去酒店上班, 日式酒店,制服酒店,ktv酒店,禮服店,整天穿得水水漂漂的,還是想去制服店日領上班小姐,水水們如果想要擁有打工工作、晚上兼差工作兼差打工假日兼職兼職工作酒店兼差兼差打工兼差日領工作晚上兼差工作酒店工作酒店上班酒店打工兼職兼差兼差工作酒店上班等,想了解酒店相關工作特種行業內容,想兼職工作日領假日兼職兼差打工、或晚班兼職想擁有鋼琴酒吧又有保障的工作嗎???又可以現領請找專業又有保障的艾葳酒店經紀公司!

艾葳酒店經紀是合法的公司工作環境高雅時尚,無業績壓力,無脫秀無喝酒壓力,高層次會員制客源,工作輕鬆,可日領現領
一般的酒店經紀只會在水水們第一次上班和領薪水時出現而已,對水水們的上班安全一點保障都沒有!艾葳酒店經紀公司的水水們上班時全程媽咪作陪,不需擔心!只提供最優質的酒店上班,酒店上班,酒店打工環境、上班條件給水水們。心動嗎!? 趕快來填寫你的酒店上班履歷表

水水們妳有缺現領、有兼職缺錢便服店的煩腦嗎?想到日本留學缺錢嗎?妳是傳播妹??想要擁有高時薪又輕鬆的賺錢,酒店和,假日打工,假日兼職賺錢的機會嗎??想實現夢想卻又缺錢沒錢嗎!??
艾葳酒店台北酒店經紀招兵買馬!!徵專業的酒店打工,想要去酒店的水水,想要短期日領,酒店日領,禮服酒店,制服店,酒店經紀,ktv酒店,便服店,酒店工作,禮服店,酒店小姐,酒店經紀人,
等相關服務 幫您快速的實現您的夢想~!!

Kitty said...

不倫希望若妻出会いドM主婦完全無料出会い淫乱エロ妻出会い割切り人妻とやれる近所の人妻即エッチ出会い割り切った奥さん完全無料出会い淫乱人妻と出会いやらしい奥さん完全無料出会いやらしいギャル妻とやれる近所の30代即エッチ出会い割り切った若妻完全無料出会いエッチなエロ妻掲示板愛人募集新妻出会い不倫エロ妻出会い系エッチ好き人妻出合い巨乳40代と出会えるエッチな30代掲示板セフレギャル妻出会い系サイトやらしいギャル妻とやれるドM主婦と出会える割切り若妻出会いサイトヤリマン主婦出逢い愛人希望エロ妻出逢いヤリマンギャル妻と出会える愛人奥さん即エッチ出会い割切りギャル妻とやれる出会い系