PDA

Просмотр полной версии : Runtime Binding. Вопрос знатокам.


rem_v
26.10.2005, 19:07
Господа. Следующая ситуация.

Необходимо создать связку между DataSet и например Label.
Как это делается - я в курсе, но вот столкнулся со следующей ситуацией: не могу указать свойство, куда привязываться.

Пример:
1. Создайте новый ролик.
2. На рабочее пространство драгайте компонент Label. Дайте ему имя "my_lbl".
3. На рабочее пространство драгайте компонент DataSet. Дайте ему имя "my_ds".
4. В первом фрейме ролика втыкаем следующий простой код:

var srcEndPoint:Object = {component:my_ds, property:"selectedIndex", event:"iteratorScrolled"};
var destEndPoint:Object = {component:my_lbl, property:"text"};
new mx.data.binding.Binding(srcEndPoint, destEndPoint);

my_ds.addItem("some text 1");
my_ds.addItem("some text 2");
my_ds.addItem("some text 3");

5. Запускаем.

В результате текст компонента Label изменился на 2, само собой понятно почему.

Собственно вопрос:
Как мне создать Binding с фиксированной записью в DataSet (например с my_ds.items[0])?


Для наглядности прилагаю работающий пример написанного кода.

7thsky™
26.10.2005, 19:44
var srcEndPoint:Object = {component:my_ds, property:"selectedIndex", event:"iteratorScrolled"};

ты же сам написал, зачем связываешь selectedIndex?
потом смотри в хелпе описание EndPoint.location

rem_v
27.10.2005, 11:24
var srcEndPoint:Object = {component:my_ds, property:"selectedIndex", event:"iteratorScrolled"};

ты же сам написал, зачем связываешь selectedIndex?
потом смотри в хелпе описание EndPoint.location

selectedIndex - чисто как пример.
А вот как описать endPoint для элемента массива, или для элемента Dataset-а, я пока что не нашел.

То есть:
sourceEndPoint.location=["position","x"];
указывает только на массив, а как указать какой-то из элементов - непоняно.

7thsky™
27.10.2005, 11:41
EndPoint.location
Availability
Flash Player 6 (6.0 79.0).

Edition
Flash MX Professional 2004.

Usage
endPointObj.location

Description
Property; specifies the location of a data field within the property of the component instance. There are four ways to specify a location: as a string that contains an XPath expression, as a string that contains an ActionScript path, as an array of strings, or as an object.

XPath expressions can only be used when the data is an XML object. (See Example 1 below.) For a list of supported XPath expressions, see "Adding bindings using path expressions" in Using Flash.

For XML and ActionScript objects, you can also specify a string that contains an ActionScript path. An ActionScript path contains the names of fields separated by dots (for example, "a.b.c").

You can also specify an array of strings as a location. Each string in the array “drills down” another level of nesting. You can use this technique with both XML and ActionScript data. (See Example 2 below.) When used with ActionScript data, an array of strings is equivalent to use of an ActionScript path; that is, the array ["a","b","c"] is equivalent to "a.b.c".

If you specify an object as the location, the object must specify two properties: path and indices. The path property is an array of strings, as discussed above, except that one or more of the specified strings may be the special token "[n]". For each occurrence of this token in path, there must be a corresponding index item in indices. As the path is evaluated, the indices are used to index into arrays. The index item can be any EndPoint object. This type of location can be applied to ActionScript data only—not XML. (See Example 3 below.)

Example
Example 1: This example uses an XPath expression to specify the location of a node named zip in an XML object:

var sourceEndPoint = new mx.databinding.EndPoint();
var sourcObj=new Object();
sourceObj.xml=new XML("<zip>94103</zip>");
sourceEndPoint.component=sourceObj;
sourceEndPoint.property="xml";
sourceEndPoint.location="/zip";//

Example 2: This example uses an array of strings to “drill down” to a nested movie clip property:

var sourceEndPoint = new mx.data.binding.EndPoint();
// assume movieClip1.ball.position exists
sourceEndPoint.component=movieClip1;
sourceEndPoint.property="ball";
// access movieClip1.ball.position.x
sourceEndPoint.location=["position","x"];

Example 3: This example shows how to use an object to specify the location of a data field in a complex data structure:

var city=new Object();
city.theaters = [{theater: "t1", movies: [{name: "Good,Bad,Ugly"}, {name:"Matrix Reloaded"}]}, {theater: "t2", movies: [{name: "Gladiator"}, {name: "Catch me if you can"}]}];
var srcEndPoint = new EndPoint();
srcEndPoint.component=city;
srcEndPoint.property="theaters";
srcEndPoint.location = {path: ["[n]","movies","[n]","name"], indices: [{constant:0},{constant:0}]};