Flex: Passing data to a php file under some condition - apache-flex

I am using an accordian in which has three childs. Each child has some textInput elements. Now, i want to send data written in currently selected accordian's child's textInputs.
I have created a function "configure" which is called when someone clicks a button. That function checks as to which child of accordian is selected. Whichever is selected, the textInputs' text of that child are stored in locally defined variables.
Now, i have no idea how to pass these variable to the HTTPService i am sending at the end of function configure.
Can anyone tell me what should i do now or if there is any other efficient solution?
Thankyou
Codes:
private function configure():void
{
var selectedAlgos:Array = algosList.selectedItems;
var selectedMode:Array;
if (modeAccordian.selectedIndex == 0)
{
var N_interface:String = N_interface.text;
var N_duration:String = N_duration.text;
selectedMode.push(N_interface);
selectedMode.push(N_duration);
}
else if (modeAccordian.selectedIndex == 1)
{
var F_filePath:String = F_filePath.text;
var F_filePrefix:String = F_filePrefix.text;
}
else if (modeAccordian.selectedIndex == 2)
{
var T_filePath:String = T_filePath.text;
var T_filePrefix:String = T_filePrefix.text;
var T_metaFile:String = T_metaFile.text;
var T_toMergeFile:String = T_toMergeFile.text;
var T_NAT:String = T_NAT.text;
var T_NATIP:String = T_NATIP.text;
}
configureService.send();
}
HTTPService:
<mx:HTTPService id="configureService" url="configure.php" resultFormat="object" method="POST">
<mx:request xmlns="">
<selectedAlgos>{selectedAlgos}</selectedAlgos>
<selectedMode>{selectedMode}</selectedMode>
</mx:request>
</mx:HTTPService>

According to the HTTPService docs:
public function send(parameters:Object = null):mx.rpc:AsyncToken
parameters:Object (default = null)
An Object containing name-value pairs or an XML object,
depending on the content type for service requests.
So I believe you can drop the mx:request section of your mxml, and just add this to the send request:
configureService.send(
{
selectedAlgos:selectedAlgos.join(","),
selectedMode:selectedMode.join(",")
}
);
Otherwise, if you want to use binding, you should make the selectedAlgos/selectedMode bindable members of the same class that configure is defined in.

Related

Use Widget Value in Server Script

I may be overthinking this drastically, but what is the easiest way to access a widget's value in a server script?
In my particular case, I am trying to use a dropdown widget's value as the filter for a calculated model query.
function getMonthlyTotalsByResource_() {
var allRecordsQuery = app.models.Allocations.newQuery();
allRecordsQuery.filters.Approved._equals = true;
allRecordsQuery.filters.Resource.Manager.ManagerName._equals = /* How do I make the widget's value available here? */
var allRecords = allRecordsQuery.run();
...
...
In your calculated model datasource in the server script have the following:
return getMonthlyTotalsByResource_(query);
Still in your model datasource add a parameter ('String?') and call it ManagerName.
On your page with the dropdown bind the value of the widget to #datasource.properties.ManagerName
In your server script function change to the following:
function getMonthlyTotalsByResource_(query) {
var allRecordsQuery = app.models.Allocations.newQuery();
allRecordsQuery.filters.Approved._equals = true;
allRecordsQuery.filters.Resource.Manager.ManagerName._equals =
query.parameters.ManagerName;
var allRecords = allRecordsQuery.run();
The easiest way to do this is to add a parameter to the datasource of your calculated model. Then bind something to it from the client side (e.g. bind to datasource.mycalculatedds.parameters.myparam ).
Then pass the default 'query' object from your calculated datasource. For example in your calculated DS, where you call your function, call
getMonthlyTotalsByResource_(query). Then you can set something like
var thismanager = query.parameters.myparam
// get widget value
function getWidgetValue() {
var val = app.PAGES.YOUR_PAGE.descendants.WIDGET_NAME.value;
return val;
}
// execture query
function getMonthlyTotalsByResource_(widgetValue) {
var allRecordsQuery = app.models.Allocations.newQuery();
allRecordsQuery.filters.Approved._equals = true;
allRecordsQuery.filters.Resource.Manager.ManagerName._equals = widgetValue;
var allRecords = allRecordsQuery.run();
}
// run
getMonthlyTotalsByResource_(getWidgetValue);

Flex Advanced Data Grid w/ hierarchical data: How to access currentTarget fields on dragdrop event?

So this is driving me crazy. I've got an advanced data grid with a dataprovider that's an array collection w/ hierarchical data. Each object (including the children) has an id field. I'm trying to drag and drop data from within the ADG. When this happens, I need to grab the id off the drop target and change the dragged object's parentid field. Here's what I've got:
public function topAccountsGrid_dragDropHandler(event:DragEvent):void{
//In this function, you need to make the move, update the field in salesforce, and refresh the salesforce data...
if(checkActivateAccountManageMode.selected == true) {
var dragObj:Array = event.dragSource.dataForFormat("treeDataGridItems") as Array;
var newParentId:String = event.currentTarget['Id'];
dragObj[0].ParentId = newParentId;
} else {
return;
}
app.wrapper.save(dragObj[0],
new mx.rpc.Responder(
function():void {
refreshData();
},
function():void{_status = "apex error!";}
)
);
}
I can access the data I'm draggin (hence changing parentId) but not the currentTarget. I think the hierarchical data is part of the problem, but I can't find much in the documentation? Any thoughts?
event.currentTarget is not a node, it's the ADG itself. However, it's quite easy to get the information you want, since the ADG stores that data internally (as mx_internal).
I'm using the following code snippets (tested with Flex SDK 4.1) in a dragOver handler, but I guess it will work in a dragDrop handler too.
protected function myGrid_dragDropHandler(event:DragEvent):void
{
// Get the dragged items. This could either be an Array, a Vector or NULL.
var draggedItems:Object = getDraggedItems(event.dragSource);
if (!draggedItems)
return;
// That's our ADG where the event handler is registered.
var dropTarget:AdvancedDataGrid = AdvancedDataGrid(event.currentTarget);
// Get the internal information about the dropTarget from the ADG.
var dropData:Object = mx_internal::dropTarget._dropData;
// In case the dataProvider is hierarchical, get the internal hierarchicalData aka rootModel.
var hierarchicalData:IHierarchicalData = dropTarget.mx_internal::_rootModel;
var targetParent:Object = null;
// If it's a hierarchical data structure and the dropData could be retrieved
// then get the parent node to which the draggedItems are going to be added.
if (hierarchicalData && dropData)
targetParent = dropData.parent;
for each (var draggedItem:Object in draggedItems)
{
// do something with the draggedItem
}
}
protected function getDraggedItems(dragSource:DragSource):Object
{
if (dragSource.hasFormat("treeDataGridItems"))
return dragSource.dataForFormat("treeDataGridItems") as Array;
if (dragSource.hasFormat("items"))
return dragSource.dataForFormat("items") as Array;
if (dragSource.hasFormat("itemsByIndex"))
return dragSource.dataForFormat("itemsByIndex") as Vector.<Object>;
return null;
}
var dropData:Object = mx_internal::dropTarget._dropData;
should be
var dropData:Object = dropTarget.mx_internal::_dropData;
Try this.

Combine/merge Dynamic Objects in AS3

I have 2 dynamic objects and I want to build one to contain all the properties:
var o1:Object = {prop1:val1,prop2:val2,prop3:val3};
var o2:Object = {prop3:val3a,prop4:val4};
and I need to obtain a third object that looks like that:
{prop1:val1, prop2:val2, prop3:val3a, prop4:val4};
Basically I need a way to iterate through the object properties and to add new properties to the third object. I have to mention I'm quite new to AS3/Flash/Flex.
First question, do you really mean to have prop3 in both objects? you will need to decide what to do in case of a collision like that, which object has precedence.
Secondly, check out the introspection apis: http://livedocs.adobe.com/flex/3/html/help.html?content=usingas_8.html
something like this should work:
public function mergeDynamicObjects ( objectA:Object, objectB:Object ) : Object
{
var objectC:Object = new Object();
var p:String;
for (p in objectA) {
objectC[p] = objectA[p];
}
for (p in objectB) {
objectC[p] = objectB[p];
}
return objectC;
}
If the property exists in A and B, B's will overwrite A's. Also note that if the values of a property is an object, it will pass a reference, not a copy of the value. You might need to clone the object in those cases, depending on your needs.
Note: I haven't actually tested the above, but it should be close. Let me know if it doesn't work.
Updated to fix the errors. Glad it works for you though.
You can dynamically access/set properties on objects with the index operator. The for loop will itterate over the property names, so if you put it all together, the following test passes:
[Test]
public function merge_objects():void {
var o1:Object = {prop1:"one", prop2:"two", prop3:"three"};
var o2:Object = {prop3:"threeA", prop4:"four"};
var o3:Object = new Object();
for (var prop in o1) o3[prop] = o1[prop];
for (var prop in o2) o3[prop] = o2[prop];
assertThat(o3.prop1, equalTo("one"));
assertThat(o3.prop2, equalTo("two"));
assertThat(o3.prop3, equalTo("threeA"));
assertThat(o3.prop4, equalTo("four"));
}
you can iterate over the object properties like:
var obj1:Object = new Object();
for(var str:String in obj2){
obj1[str] = "any value"; // insert the property from obj2 to obj1
}

Assign Xml Values to dynamically created components

xml values are stored in 'arr' array collection. depending on the length of the array stored, the below described components are created and assign those values to relevant components dynamically.
For Example:
AdvanceSearch.mxml is one component, and another components as advanceSearchAtom.mxml. within 'advanceSearchAtom' has some sub components as textbox, combo box etc,. we can add many advanceSearchAtom.mxml inside 'AdvanceSearch.mxml'.
var container : AdvanceSearch;
var adv : advanceSearchAtom;
for(var i:int=0;i<arr.length;i++) {
adv = new advanceSearchAtom();
adv.field.text = arr[i].field;
adv.value.selectedIndex = arr[i].value;
container.addChild(adv);
}
Please let me know if anybody come across this type of problem. if any relevant link is appreciable. Thanks in advance
You didn't mention it, but I guess the problem is that you're getting null reference error (1009) on the following lines:
adv.field.text = arr[i].field;
adv.value.selectedIndex = arr[i].value;
Am I right?
This is because field and value are not yet created. As per the default component instantiation, Flex creates the children only when it is required - i.e., when it is to be displayed.
You can either listen to the creationComplete event of the AdvanceSearchAtom component and update the values from there; or have Binadble public variables in the AdvanceSearchAtom class, bind them to field.text and value.selectedIndex and assign xml values to those variables in the loop.
Using creation complete:
public var container:AdvanceSearch;
public var searchItems:Array = [];
public var arr:Array;
//assuming that arr has been initialized with xml values.
var adv:AdvanceSearchAtom;
for(var i:int=0;i<arr.length;i++) {
adv = new AdvanceSearchAtom();
adv.addEventListener(FlexEvent.CREATION_COMPLETE, onAtomCreated);
container.addChild(adv);
searchItems.push(adv);
}
public function onAtomCreated(e:Event):void
{
var adv:AdvanceSearchAtom = e.currentTarget as AdvanceSearchAtom;
if(!adv)
return;
var index:Number = searchItems.indexOf(adv);
adv.field.text = arr[index].field;
adv.value.selectedIndex = arr[index].value;
}
Using data binding:
Inside AdvanceSearchAtom.mxml
<mx:TextInput id="field" text="{textValue}"/>
<mx:ComboBox id="value" selectedIndex="{comboIndex}"/>
In the script block of AdvanceSearchAtom.mxml
[Bindable]
public var textValue:String;
[Bindable]
public var comboIndex:Number;
var adv:AdvanceSearchAtom;
In the AdvanceSearch class:
for(var i:int=0;i<arr.length;i++) {
adv = new AdvanceSearchAtom();
container.addChild(adv);
adv.field.text = arr[i].field;
adv.value.selectedIndex = arr[i].value;
}

how can i validate(phone number validation) each array item in script using flex

var str:String=cntslst.text;
var results:Array = str.split(",");
for(i=0;i<result.length;i++)
{<mx:NumberValidator source="{result[i]}" property="text" id="cell" />}
is it possible ?
You can use the PhoneNumberValidator class, instantiated in either AS3 or MXML is fine. Don't set the source or property properties unless you want validation error UI to appear.
You can then call the validate method on that instance for each string and check to see if the results property of the ValidationResultEvent that is returned is null or empty.
var validator:PhoneNumberValidator; //instantiate how you like
var result:ValidationResultEvent = validator.validate(str);
var isPhoneNumber:Boolean = result.results == null || result.results.length == 0;

Resources