incubator-couchdb-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Todd Chambery <todd.chamb...@gmail.com>
Subject Re: storing objects with "function attributes"
Date Mon, 05 Apr 2010 10:15:13 GMT
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.Breunese@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.
>

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message