-- lightflowmark <1...@lightflowinterrupted.com> wrote
(on Wednesday, 22 April 2009, 05:44 AM -0700):
> I've been trying to assemble data suitable for building a Dojo tree widget. 
> I've assembled my data as an associative array (actually an assoc. arr. of
> objects, but that's not relevant), which seems intuitive to me - in any
> event, there's no way around it that I can see for my use-case.
> 
> The problem comes when I try to cast it to a Dojo_Data object.  Passing an
> associative array to Zend_Dojo_Data gives the (somewhat misleading) error
> message 'Only arrays and objects may be attached'.  I've worked around it by
> converting my assoc. array to a simple array, but it seems ugly & bizarre to
> me.
> 
> So, my question is, (how) should Zend_Dojo_Data handle associative arrays? 
> I'm happy to write some code for it, but am not clear exactly what the
> solution should be.  Any input?

dojo.data expects a format like the following:

    {
        identifier: "name",
        items:      []
    }

The "items" parameter needs to be an array of items. Each item in that
array should be an associative array. In your example below, $testArr is
a *single* item to send. You would then provide it to Zend_Dojo_Data as
this:

    $data = new Zend_Dojo_Data('id', array($testArr), 'description');

(I'm assuming that 'description' would be the field displayed, and thus
it corresponds to the 'label' in a dojo.data payload.)

Typically you'll send more than a single item back, in which case you
should send an array of such items, or some iterable object of items.

> 
>    $testArr = array(
>       'id'  => 1,
>       'description' => 'some description',
>       'children' => array(
>         'someName' => array(
>           'id' => 2,
>           'description' => 'some description2',
>           'children' => array(
>             'someDeepName' => array(
>               'id'  => 3,
>               'description' => 'some deep description2',
>               ),
>             'someOtherDeepName' => array(
>               'id'  => 4,
>               'description' => 'some deep description4',
>               ),
>             ),
>           ),
>         ),
>       );
> 
>     $data = new Zend_Dojo_Data('id', $testArr);
> 
> 
> Exception:
> Error: Only arrays and objects may be attached.

-- 
Matthew Weier O'Phinney
Project Lead            | matt...@zend.com
Zend Framework          | http://framework.zend.com/

Reply via email to