You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@cocoon.apache.org by Robby Pelssers <ro...@ciber.com> on 2009/12/15 12:21:41 UTC

how-to iterate over attributes of javascript object within template block

Hi all,

 

I know i'm kinda pushing it by what I'm trying to accomplish here but it would be cool if it would work.

 

Suppose I have following objects defined in my flowscript:

 

var topics = [

    {

      "title": "Product profile", 

      "id": "product_profile"

    },

    {

      "title": "Pinning information", 

      "id": "pinning_information",

      

    },

    {

      "title": "Limiting values", 

      "id": "limiting_values",

      "type": "parametric",

   }

];

 

What I would like to generate is something like:

<Topics>

  <Topic id="product_profile" title="Product profile"/>

  <Topic id="pinning_information" title="Pinning information"/>

  <Topic id="limiting_values" title="Limiting values" type="parametric"/>

</Topics

 

  

So written in pseudo-code:

Iterate over all topics and generate a <topic> with it's corresponding attributes.  The only thing I don't know is if the forEach iterator also can handle something like we can do to iterate over all keys of an object and then retrieving the value.

 

for (var key in object) {

    someFunction(key, object[key]);

}

 

    <Topics>

      <jx:forEach var="topic" items="${data.topics}">

        <Topic>

          <!-- 

          <jx:forEach var="attribute" items="${topic}">

            <jx:attribute name="${attribute}" value="${topic[attribute]}"/>    à  this does not work because the @items does not seem to be able to handle the wanted behaviour in case we don't pass on a collection but a javascript object.  In that case default behaviour should be in my opinion to iterate over the keys of the object.

          </jx:forEach>

           -->

        </Topic>

      </jx:forEach>

    </Topics>

 

What do you guys think?

 

Kind regards,

Robby


RE: Re: how-to iterate over attributes of javascript object within template block [SOLVED]

Posted by Robby Pelssers <ro...@ciber.com>.
Hi Luca,

I think it's a rather nice solution myself.

So basically you could now even write a generic macro to export JSON to
xml... should be a breeze ;-)

And I always share solutions... others might have similar use cases but
it also opens discussion(s) which might lead to better implementations.

Cheers,
Robby Pelssers
http://robbypelssers.blogspot.com/


-----Original Message-----
From: news [mailto:news@ger.gmane.org] On Behalf Of Luca Morandini
Sent: Tuesday, December 15, 2009 4:07 PM
To: users@cocoon.apache.org
Subject: Re: how-to iterate over attributes of javascript object within
template block [SOLVED]

On 15/12/09 14:03, Robby Pelssers wrote:
> I managed to find a very easy solution...I wrote some extensions on
the
> native javascript Object and Array and put that file in the flowscript
> folder.  So whenever Cocoon is started, the flowscript folder is
scanned
> and all my objects inherit the extensions automatically.

Rather cool... thanks for sharing it with us.

Regards,

--------------------
    Luca Morandini
www.lucamorandini.it
--------------------


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@cocoon.apache.org
For additional commands, e-mail: users-help@cocoon.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@cocoon.apache.org
For additional commands, e-mail: users-help@cocoon.apache.org


Re: how-to iterate over attributes of javascript object within template block [SOLVED]

Posted by Luca Morandini <lm...@ieee.org>.
On 15/12/09 14:03, Robby Pelssers wrote:
> I managed to find a very easy solution…I wrote some extensions on  the
> native javascript Object and Array and put that file in the flowscript
> folder.  So whenever Cocoon is started, the flowscript folder is scanned
> and all my objects inherit the extensions automatically.

Rather cool... thanks for sharing it with us.

Regards,

--------------------
    Luca Morandini
www.lucamorandini.it
--------------------


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@cocoon.apache.org
For additional commands, e-mail: users-help@cocoon.apache.org


RE: how-to iterate over attributes of javascript object within template block [SOLVED]

Posted by Robby Pelssers <ro...@ciber.com>.
I managed to find a very easy solution...I wrote some extensions on  the native javascript Object and Array and put that file in the flowscript folder.  So whenever Cocoon is started, the flowscript folder is scanned and all my objects inherit the extensions automatically.

 

Some extension functions I wrote:

 

 

/**

* Extension function on Native Javascript Array which calls a function f with (@param index, @param object) for all objects in the array.

*/

Array.prototype.forEach = function(f) {

    for (var i=0; i < this.length; i++) {

      f(i, this[i]);

    } 

}

 

/**

* Extension function on Native Javascript Object which calls a function f with (@param key, @param value) for all 'own' properties.

*/

Object.prototype.forEach = function(f) {

      var that = this;

      this.keys().forEach(

            function(index, key) {

                  f(key, that[key]);

            }

      )

}

 

/**

 * Extension function on Native Javascript Object which returns an array of keys for all 'own' properties

 */

Object.prototype.keys = function() {

    var keys = [];

    for (var key in this) {

      if (this.hasOwnProperty(key)) {

          keys.push(key);

        }

    }

    return keys;

}

 

/**

 * Extension function on Native Javascript Object which returns an array of values for all 'own' properties.

 */

Object.prototype.values = function() {

      var values = [];

      var that = this;

      this.keys().forEach(

          function(index, key) {

            values.push(that[key]);

          }

      )

    return values;

}

 

 

So now I can do following in my jx-template:

 

      <jx:forEach var="topic" items="${data.topics}">

        <Topic id="${topic.id}">

          <jx:forEach var="key" items="${topic.keys()}">

            <jx:attribute name="${key}" value="${topic[key]}"/>

          </jx:forEach>

        </Topic>

      </jx:forEach>

    </Topics>

 

Works like a charm ;-)

 

Kind regards,

Robby Pelssers

 

 

From: Robby Pelssers [mailto:robby.pelssers@ciber.com] 
Sent: Tuesday, December 15, 2009 12:22 PM
To: users@cocoon.apache.org
Subject: how-to iterate over attributes of javascript object within template block

 

Hi all,

 

I know i'm kinda pushing it by what I'm trying to accomplish here but it would be cool if it would work.

 

Suppose I have following objects defined in my flowscript:

 

var topics = [

    {

      "title": "Product profile", 

      "id": "product_profile"

    },

    {

      "title": "Pinning information", 

      "id": "pinning_information",

      

    },

    {

      "title": "Limiting values", 

      "id": "limiting_values",

      "type": "parametric",

   }

];

 

What I would like to generate is something like:

<Topics>

  <Topic id="product_profile" title="Product profile"/>

  <Topic id="pinning_information" title="Pinning information"/>

  <Topic id="limiting_values" title="Limiting values" type="parametric"/>

</Topics

 

  

So written in pseudo-code:

Iterate over all topics and generate a <topic> with it's corresponding attributes.  The only thing I don't know is if the forEach iterator also can handle something like we can do to iterate over all keys of an object and then retrieving the value.

 

for (var key in object) {

    someFunction(key, object[key]);

}

 

    <Topics>

      <jx:forEach var="topic" items="${data.topics}">

        <Topic>

          <!-- 

          <jx:forEach var="attribute" items="${topic}">

            <jx:attribute name="${attribute}" value="${topic[attribute]}"/>    à  this does not work because the @items does not seem to be able to handle the wanted behaviour in case we don't pass on a collection but a javascript object.  In that case default behaviour should be in my opinion to iterate over the keys of the object.

          </jx:forEach>

           -->

        </Topic>

      </jx:forEach>

    </Topics>

 

What do you guys think?

 

Kind regards,

Robby