francis dcunha
francis dcunha

Reputation: 55

how to put a variable value in arraycollection using action script and flex?

i have a variable and i want to pu this variable in arraycollection or array using actionscript and flex

the following is code

var xyz:int=30;

var myArray:Array = [ {Month: "January", Views_Week1:xyz}, { Month: "June",        

    Views_Week2: 13 }, { Month:" December", Views_Week3: 14} ];

pageViews= new ArrayCollection(myArray);

or

pageViews= new ArrayCollection(myArray);
[
    { Month: January, Views_Week1: xyz},
    { Month:June, Views_Week2: 13},
    { Month: december, Views_Week3: 14}])

plz

Upvotes: 2

Views: 2902

Answers (2)

Florian Salihovic
Florian Salihovic

Reputation: 3951

That does not work that way. When defining a variable, it is basically just present in that scope. You could declare it [Bindable] in the scope of a class, so the class would propagate changes with a PropertyChangeEvent of type PropertyChangeEvent.PROPERTY_CHANGE. This would allow you using BindingUtils, ChangeWatcher and MXML databinding with <{} /> declarative bindings.

You need to define a class, declare the class or a field [Bindable] and then create instances of the class and reference these through the ArrayCollection. Using vanilla object won't get you somewhere, since those can't dispatch Events.

    package
    {
      [Bindable]
      public class Person
      {
        public var name:String;

        public function Person(n:String)
        {
          name = n;
        }
      }
    }

    const source:Array = [new Person('Fred')]
        , collection:IList = new ArrayCollection(source);

Data binding relies on some key mechanisms like event dispatching, that is something to keep in mind. Also, in one way or the other, a reference to the data being changed need to in the different scopes, where the notification of the change is needed.

Upvotes: 1

dirkgently
dirkgently

Reputation: 111120

pageViews= new ArrayCollection(myArray);

Looks fine. The second one however doesn't make sense where you are initializing the ArrayCollection and then trying to assign the objects again (which won't happen as there's the ; end-of-statement marker).

Does that not work for you?

Upvotes: 1

Related Questions