You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@couchdb.apache.org by Todd Chambery <to...@gmail.com> on 2010/04/04 23:29:46 UTC

storing objects with "function attributes"

Hi all,

I don't seem to be able to load "function attributes" of objects into
CouchDB:

If I save
{
  name: "foo",
  func: function() {
     do();
     return;
  }
}

func will not appear on the object.  If I change the func value to a string,
everything's cool.

Can I store function attributes in CouchDB?

Thanks,

Todd

RE: storing objects with "function attributes"

Posted by Nils Breunese <N....@vpro.nl>.
That's not valid JSON. JSON is used for data-interchange, not code. (Although you're free to store code in a JSON string or save it as an attachment to a JSON document in CouchDB.) See http://json.org/ for a description of the JSON format.

Nils Breunese.
________________________________________
Van: Todd Chambery [todd.chambery@gmail.com]
Verzonden: zondag 4 april 2010 23:29
Aan: user@couchdb.apache.org
Onderwerp: storing objects with "function attributes"

Hi all,

I don't seem to be able to load "function attributes" of objects into
CouchDB:

If I save
{
  name: "foo",
  func: function() {
     do();
     return;
  }
}

func will not appear on the object.  If I change the func value to a string,
everything's cool.

Can I store function attributes in CouchDB?

Thanks,

Todd

De informatie vervat in deze  e-mail en meegezonden bijlagen is uitsluitend bedoeld voor gebruik door de geadresseerde en kan vertrouwelijke informatie bevatten. Openbaarmaking, vermenigvuldiging, verspreiding en/of verstrekking van deze informatie aan derden is voorbehouden aan geadresseerde. De VPRO staat niet in voor de juiste en volledige overbrenging van de inhoud van een verzonden e-mail, noch voor tijdige ontvangst daarvan.

Re: storing objects with "function attributes"

Posted by Adam Wolff <aw...@gmail.com>.
Functions aren't part of JSON. http://www.json.org/

A

On Sun, Apr 4, 2010 at 2:46 PM, Andrew Melo <an...@gmail.com> wrote:

> On Sun, Apr 4, 2010 at 4:29 PM, Todd Chambery <to...@gmail.com>
> wrote:
> > Hi all,
> >
> > I don't seem to be able to load "function attributes" of objects into
> > CouchDB:
> >
> > If I save
> > {
> >  name: "foo",
> >  func: function() {
> >     do();
> >     return;
> >  }
> > }
> >
> > func will not appear on the object.  If I change the func value to a
> string,
> > everything's cool.
> >
> > Can I store function attributes in CouchDB?
>
> What do you want the attributes to do?
>
> > Thanks,
> >
> > Todd
> >
>
>
>
> --
> --
> Andrew Melo
>

Re: storing objects with "function attributes"

Posted by Todd Chambery <to...@gmail.com>.
For posterity, this seems to work:

{
   name: "Simple Weapon Proficiency",
   attack: "if(weapon.category == 'simple') { attacks.weapon_proficiency =
0; } return attacks;"
}

When I load the static bits out of CouchDB, I create a new function from the
string.

if(feat.attack) {
feat.attack = new Function("attacks", "weapon", feat.attack);
}

The first two args are the function args, so the above creates:

attack: anonymous(attacks, weapon) { .... }

Todd

On Mon, Apr 5, 2010 at 4:32 AM, Nils Breunese <N....@vpro.nl> wrote:

> I guess you'll either need to serialize the logic code to be able to store
> this in JSON and unserialize after loading the document, or separate the
> logic from the data.
>
> Nils Breunese.
> ________________________________________
> Van: Todd Chambery [todd.chambery@gmail.com]
> Verzonden: maandag 5 april 2010 2:04
> Aan: user@couchdb.apache.org
> Onderwerp: Re: storing objects with "function attributes"
>
> As Nils and Adam have suggested, functions aren't part of JSON (something I
> didn't know).
>
> But as for why:  I have a lot of static objects that I want to handle data
> in object-particular ways.  Eg.
>
> {
>    name: "Simple Weapon Proficiency",
>    attack: function attack(attacks, weapon) {
>       if(weapon.category == 'simple') {
>          attacks.weapon_proficiency = 0;
>       }
>  return attacks;
>    }
> }
>
> So there are a bunch of these, if the user has selected them (in the UI), I
> ask each of the selected objects to modify the attack data however it's
> supposed to.
>
> De informatie vervat in deze  e-mail en meegezonden bijlagen is uitsluitend
> bedoeld voor gebruik door de geadresseerde en kan vertrouwelijke informatie
> bevatten. Openbaarmaking, vermenigvuldiging, verspreiding en/of verstrekking
> van deze informatie aan derden is voorbehouden aan geadresseerde. De VPRO
> staat niet in voor de juiste en volledige overbrenging van de inhoud van een
> verzonden e-mail, noch voor tijdige ontvangst daarvan.
>

Re: storing objects with "function attributes"

Posted by Pavan Kumar <tv...@gmail.com>.
eval() + jquery 'apply' is the probably one way to get the job done.   As
Nils mentioned you can keep your function as a plain text member in JSON,
retrive and use eval and apply to call it dynamically at the client side
browser.

Pavan

On Mon, Apr 5, 2010 at 2:02 PM, Nils Breunese <N....@vpro.nl> wrote:

> I guess you'll either need to serialize the logic code to be able to store
> this in JSON and unserialize after loading the document, or separate the
> logic from the data.
>
> Nils Breunese.
> ________________________________________
> Van: Todd Chambery [todd.chambery@gmail.com]
> Verzonden: maandag 5 april 2010 2:04
> Aan: user@couchdb.apache.org
> Onderwerp: Re: storing objects with "function attributes"
>
> As Nils and Adam have suggested, functions aren't part of JSON (something I
> didn't know).
>
> But as for why:  I have a lot of static objects that I want to handle data
> in object-particular ways.  Eg.
>
> {
>    name: "Simple Weapon Proficiency",
>    attack: function attack(attacks, weapon) {
>       if(weapon.category == 'simple') {
>          attacks.weapon_proficiency = 0;
>       }
>  return attacks;
>    }
> }
>
> So there are a bunch of these, if the user has selected them (in the UI), I
> ask each of the selected objects to modify the attack data however it's
> supposed to.
>
> De informatie vervat in deze  e-mail en meegezonden bijlagen is uitsluitend
> bedoeld voor gebruik door de geadresseerde en kan vertrouwelijke informatie
> bevatten. Openbaarmaking, vermenigvuldiging, verspreiding en/of verstrekking
> van deze informatie aan derden is voorbehouden aan geadresseerde. De VPRO
> staat niet in voor de juiste en volledige overbrenging van de inhoud van een
> verzonden e-mail, noch voor tijdige ontvangst daarvan.
>

RE: storing objects with "function attributes"

Posted by Nils Breunese <N....@vpro.nl>.
I guess you'll either need to serialize the logic code to be able to store this in JSON and unserialize after loading the document, or separate the logic from the data.

Nils Breunese.
________________________________________
Van: Todd Chambery [todd.chambery@gmail.com]
Verzonden: maandag 5 april 2010 2:04
Aan: user@couchdb.apache.org
Onderwerp: Re: storing objects with "function attributes"

As Nils and Adam have suggested, functions aren't part of JSON (something I
didn't know).

But as for why:  I have a lot of static objects that I want to handle data
in object-particular ways.  Eg.

{
    name: "Simple Weapon Proficiency",
    attack: function attack(attacks, weapon) {
       if(weapon.category == 'simple') {
          attacks.weapon_proficiency = 0;
       }
  return attacks;
    }
}

So there are a bunch of these, if the user has selected them (in the UI), I
ask each of the selected objects to modify the attack data however it's
supposed to.

De informatie vervat in deze  e-mail en meegezonden bijlagen is uitsluitend bedoeld voor gebruik door de geadresseerde en kan vertrouwelijke informatie bevatten. Openbaarmaking, vermenigvuldiging, verspreiding en/of verstrekking van deze informatie aan derden is voorbehouden aan geadresseerde. De VPRO staat niet in voor de juiste en volledige overbrenging van de inhoud van een verzonden e-mail, noch voor tijdige ontvangst daarvan.

Re: storing objects with "function attributes"

Posted by Todd Chambery <to...@gmail.com>.
As Nils and Adam have suggested, functions aren't part of JSON (something I
didn't know).

But as for why:  I have a lot of static objects that I want to handle data
in object-particular ways.  Eg.

{
    name: "Simple Weapon Proficiency",
    attack: function attack(attacks, weapon) {
       if(weapon.category == 'simple') {
          attacks.weapon_proficiency = 0;
       }
  return attacks;
    }
}

So there are a bunch of these, if the user has selected them (in the UI), I
ask each of the selected objects to modify the attack data however it's
supposed to.


On Sun, Apr 4, 2010 at 5:46 PM, Andrew Melo <an...@gmail.com> wrote:

> On Sun, Apr 4, 2010 at 4:29 PM, Todd Chambery <to...@gmail.com>
> wrote:
> > Hi all,
> >
> > I don't seem to be able to load "function attributes" of objects into
> > CouchDB:
> >
> > If I save
> > {
> >  name: "foo",
> >  func: function() {
> >     do();
> >     return;
> >  }
> > }
> >
> > func will not appear on the object.  If I change the func value to a
> string,
> > everything's cool.
> >
> > Can I store function attributes in CouchDB?
>
> What do you want the attributes to do?
>
> > Thanks,
> >
> > Todd
> >
>
>
>
> --
> --
> Andrew Melo
>

Re: storing objects with "function attributes"

Posted by Andrew Melo <an...@gmail.com>.
On Sun, Apr 4, 2010 at 4:29 PM, Todd Chambery <to...@gmail.com> wrote:
> Hi all,
>
> I don't seem to be able to load "function attributes" of objects into
> CouchDB:
>
> If I save
> {
>  name: "foo",
>  func: function() {
>     do();
>     return;
>  }
> }
>
> func will not appear on the object.  If I change the func value to a string,
> everything's cool.
>
> Can I store function attributes in CouchDB?

What do you want the attributes to do?

> Thanks,
>
> Todd
>



-- 
--
Andrew Melo