Error de extensión de ArrayCollection

i extend ArrayCollection class for add push method

package com.cargo.collections
{
    import mx.collections.ArrayCollection;

    public class DataCollection extends ArrayCollection {

        public function DataCollection(source:Array = null) {
            super(source);
        }

        public function push(...parameters):uint {
            var i:uint = source.push(parameters);
            this.refresh();
            return i;
        }
    }
}

but pushed data is array :/

var test:DataCollection = new DataCollection({id: 1});
test.source.push({id: 2});
test.push({id: 3});

la salida es

test = Array( {id: 1}, {id: 2}, Array({id: 3}) )

preguntado el 27 de agosto de 11 a las 22:08

Why is this output wrong? What exactly were you expecting? -

1 Respuestas

En tu ejemplo ...parameters creates an array containing all the arguments passed to that function. This should work as expected:

public function push(...parameters):uint {
    var i:uint = source.push(parameters[0]);
    this.refresh();
    return i;
}

Alternatively, if your purpose is to enable the pushing of multiple parameters you can use the Function.apply() method, which will translate a given array into multiple parameters:

public function push(...parameters):uint {
    var i:uint = source.push.apply(null,parameters);
    this.refresh();
    return i;
}

This is the equivalent of saying

var i:uint = source.push(parameters[0],parameters[1],parameters[2]);  // etc

Respondido 28 ago 11, 03:08

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.