Archive

Archive for August, 2009

IList’s itemUpdated method

August 28, 2009 Leave a comment

For those of you using an MVC framework like PureMVC, here is an example of updating a data item from a model. Once the view has the collection, there aren’t any new notifications or events unless an item is added or removed. Updates to existing items can occur by reference in the model. The example simulates data updates every 5 seconds and changes the email address of the two alternate items.

Application.mxml:

<?xml version=”1.0″ encoding=”utf-8″?>

<s:Application xmlns:fx=”http://ns.adobe.com/mxml/2009&#8243;

xmlns:s=”library://ns.adobe.com/flex/spark”

xmlns:mx=”library://ns.adobe.com/flex/halo” initialize=”windowedapplication1_initializeHandler(event)”>

<fx:Script>

<![CDATA[

import model.DataModel;

import mx.events.FlexEvent;

private var _dataModel:DataModel;

protected function windowedapplication1_initializeHandler(event:FlexEvent):void

{

_dataModel = new DataModel();

lst.dataProvider = _dataModel.dataCollection;

}

]]>

</fx:Script>

<s:List x=”120″ y=”66″font-size: 11pt; font-family: Monaco; color: rgb(153, 0, 0);”>lst” labelField=”email” width=”402″ height=”276″></s:List>

</s:Application>

DataModel:

package model

{

import flash.events.TimerEvent;

import flash.utils.Timer;

import mx.collections.ArrayCollection;

[Bindable]

public class DataModel

{

private var _dataCollection:ArrayCollection;

private var _updateTimer:Timer;

public function DataModel()

{

_dataCollection = new ArrayCollection();

var dd:DataDto = new DataDto();

dd.email = “russ.watson@abc.com”;

dd.name = “Russ Watson”;

_dataCollection.addItem(dd);

dd = new DataDto();

dd.email = “khayes@abc.com”;

dd.name = “Keith Hayes”;

_dataCollection.addItem(dd);

_updateTimer = new Timer(5000);

_updateTimer.addEventListener(TimerEvent.TIMER, onUpdateTimer);

_updateTimer.start();

}

public function get dataCollection():ArrayCollection

{

return _dataCollection;

}

public function set dataCollection(value:ArrayCollection):void

{

_dataCollection = value;

}

private function onUpdateTimer(e:TimerEvent):void

{

var mod:int = _updateTimer.currentCount % 5;

var dd:DataDto;

var oldVal:String;

var newVal:String;

switch ( mod )

{

case 0:

dd = dataCollection[0] as DataDto;

newVal = “russWatson@abc.com”;

break;

case 1:

dd = dataCollection[1] as DataDto;

newVal = “asdbadsfg@abc.com”;

break;

case 2:

dd = dataCollection[0] as DataDto;

newVal = “1234123@abc.com”;

break;

case 3:

dd = dataCollection[1] as DataDto;

newVal = “dfbsdfsdfh@abc.com”;

break;

case 4:

dd = dataCollection[0] as DataDto;

newVal = “34t6werh@abc.com”;

break;

case 5:

dd = dataCollection[1] as DataDto;

newVal = “mnbvcx@abc.com”;

break;

}

oldVal = dd.email;

dd.email = newVal;

dataCollection.itemUpdated(dd, “email”, oldVal, newVal);

}

}

}

DataDto:

package model
{
public class DataDto
{
private var _email:String;
private var _name:String;

public function DataDto()
{
}

public function get email():String
{
return _email;
}
public function set email(value:String):void
{
_email = value;
}

public function get name():String
{
return _name;
}
public function set name(value:String):void
{
_name = value;
}
}
}

Advertisements
Categories: Flex 4 Examples