From: Dobrica Pavlinusic Date: Wed, 9 Jan 2013 23:34:25 +0000 (+0100) Subject: create typeahead view on demand X-Git-Tag: 0.4~13 X-Git-Url: http://git.rot13.org/?a=commitdiff_plain;h=4d99a57488de9a6e3ceec84bf0384130ae8b4f3a;p=angular-drzb create typeahead view on demand --- diff --git a/angular-server.pl b/angular-server.pl index d65215b..9ed7925 100755 --- a/angular-server.pl +++ b/angular-server.pl @@ -186,6 +186,33 @@ get '/:database/_design/:design/_view/:view' => sub { } warn "CouchDB proxy $url"; my $json = _couchdb_get($url); + + if ( exists $json->{error} ) { + warn "creating CouchDB view because of ", dump($json); + my $url = "/" . $self->param('database') . "/_design/registration"; + _couchdb_put $url, { + _id => '_design/registration', + language => 'javascript', + views => { + organizations => { + map => q| function(doc) { +if ( doc.user.organization != '' ) { + emit(doc.user.organization, 1); + if ( doc.user.persons ) { + doc.user.persons.forEach( function(person) { + emit(person.organization, 1); + }); + } +} + }q|, + reduce => q| function(keys,values,rereduce) { return sum(values); } |, + } + } + }; + $json = _couchdb_get($url) + || die "give up!"; + } + if ( $format eq 'key_array' ) { # array of keys sorted by value $json->{rows} = [ map { $_->{key} } sort { $b->{value} <=> $a->{value} } @{ $json->{rows} } ]; }