PDA

View Full Version : Problem with databinding via actionscript


tschai
10-24-2006, 08:27 AM
I tried the tutorials etc., all the visual databinding is going well, no problem there, but when I try to do this manual by
actionscript...:confused:
I allready searched the web and found that I'm not the only one with this problem.

Can someone help me with this or give me some advice? Many thanks in advance.

Setup: 1 combobox named cmbSel, 2 textareas txtTitle and txtDesc

What goes:
- Getting value from the title string in the xml to textarea via xmlconnector is no problem.


Problems
- Selecting specific value(index) from xml to textarea gives me only null as value. If I make a xmlconnector
and import the schema visualy, this goes without a problem.
- Filling the combobox with the array of titles (item is the array in the schema) from the xmlconnector:
* when I don't use the rearrangefield formatter, in the output I get an XML object as result,
which seems ok, but when I use the formatter, I get nothing. I know this works with the visual
schema in the databinding tab of the xmlconnector, but how to do this with code?
* Also there is a problem with the index, NaN.
- Getting the connection to the combobox selectedindex. Maybe this has to do with the previous problem.

In the output you get some more info about the bindings.

The XML I use is a very simple representation of a rss xml, <rss><channel><item><title></title></item></channel></rss>,
with 4 titles.

Here is my code:
--- start code -----
import mx.data.components.XMLConnector;
import mx.data.binding.*;

_global.__dataLogger = new mx.data.binding.Log();

// Create events for xmlConnector
var xmlListener:Object = new Object();
xmlListener.result = function(evt:Object){
trace("results in");
trace("__________________");
trace("XMLConnector data:");
trace("1e node:" + xmlConn.results.firstChild.nodeName);
trace("__binding:" + mx.data.binding.ObjectDumper.toString(xmlConn));
};

xmlListener.status = function(evt:Object){
trace("status::"+evt.code);
};

//Create xmlConnector
var xmlConn:XMLConnector = new XMLConnector();
//xmlConn.results.__schema = {name:"XML", elements:[{name:"rss", type:{name:"Object", elements:[{name:"channel", type:{name:"Object", elements:[{name:"item", type:{name:"Object", elements:[{name:"title", type: {name: "String", readonly: true, validation: {cls: mx.data.types.Str, className: "mx.data.types.Str",settings: {}}}}]}}]}}]}}]}
xmlConn.addEventListener("result", xmlListener);
xmlConn.addEventListener("status", xmlListener);
xmlConn.direction = "receive";
xmlConn.URL = "test.xml";
xmlConn.multipleSimultaneousAllowed = false;
xmlConn.suppressInvalidCalls = true;


// Create bindings

// Bind xmlconnector to txtTitle
var src = new mx.data.binding.EndPoint();
src.component = xmlConn;
src.property = "results";
src.location = "/rss/channel/item/title";
src.event = "result";
var dest = new mx.data.binding.EndPoint();
dest.component = txtTitle;
dest.property = "text";
new mx.data.binding.Binding(src, dest, null, false);

// Bind xmlconnector to txtDesc, get 3e title
var src = new mx.data.binding.EndPoint();
src.component = xmlConn;
src.property = "results";
src.location = {path: ["rss","channel","item","[n]","title"], indices: [{constant:2}]};
src.event = "result";
var dest = new mx.data.binding.EndPoint();
dest.component = txtDesc;
dest.property = "text";
new mx.data.binding.Binding(src, dest, null, false);

// Bind xmlconnector to combobox
var src = new mx.data.binding.EndPoint();
src.component = xmlConn;
src.property = "results";
src.location = "/rss/channel/item";
src.event = "result";
var dest = new mx.data.binding.EndPoint();
dest.component = cmbSel;
dest.property = "dataprovider";
new mx.data.binding.Binding(src, dest, {cls:mx.data.formatters.RearrangeFields, className:"mx.data.formatters.RearrangeFields", settings:{fields: "label=<title>"}}, false);

// Bind xmlconnector to combobox with selectedindex
var src = new mx.data.binding.EndPoint();
src.component = xmlConn;
src.property = "results.rss.channel.item.title";
src.location = {path: ['rss','channel','item','[n]','title'], indices: [{component: "cmbSel", property: "selectedIndex"}]};
src.event = "result";
var dest = new mx.data.binding.EndPoint();
dest.component = cmbSel;
dest.property = "dataprovider";
new mx.data.binding.Binding(src, dest, null, false);// {cls:mx.data.formatters.RearrangeFields, className:"mx.data.formatters.RearrangeFields", settings:{fields: "label=<title>"}}, false);

xmlConn.trigger();
--- end code ---