couchdb-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From J Chris Anderson <jch...@apache.org>
Subject Re: get emit(doc.name,doc.url) to javascript variable
Date Fri, 13 Aug 2010 14:56:29 GMT

On Aug 9, 2010, at 5:50 PM, whimsica wrote:

> 
> So far I'm very confused about how to get couchdb to talk to javascript on
> the web page using show or view.
> 

The challenge here is figuring out which javascript code runs in which context. All the JS
you've shown, runs on the server, not the browser. Here are some tips about how to run JS
in the browser and talk to CouchDB:

The simplest way to do this is with static HTML served as an attachment to the browser, which
makes Ajax queries to the CouchDB. This way you don't need to fuss with a show function (which
is awesome, but another layer of indirection.)

Take a look at the example code on this page (and follow the exercise):

http://couchapp.org/page/what-is-couchapp#/

Where it says:

    $.couch.allDbs({
      success : function(dbs) {
        dbs.forEach(function(db) {
          $("#databases").append('<li><a href="/_utils/database.html?'+db+'">'+db+'</a></li>');
        });
      }
    });
you might try replacing it with this, to do a view query instead (assuming your db is called
"mydb" and your view is called "myview":

    var db = $.couch.db("mydb");
    db.view("myview",{
      limit : 10,
      success : function(resp) {
        resp.rows.forEach(function(row) {
          $("#databases").append('<li>'+row.key+'</li>');
        });
      }
    });
Hope that helps.

> Let's say I have a database with things like names of users and a url that
> users submit
> I want to emit the results of a view in raw json form and alert it on the
> page using javascript alert()
> 
> How do I do that.
> 
> I tried putting a javascript function in a view (alert(1)) just to see but
> it gave back an error.
> 
> So far I was able to call a javascript function in a show command below but
> it's not the json from the view.
> 
> database
> {
>   "_id": "81c76a15131be24115f53e2cafaea4e8",
>   "_rev": "2-5df7ac370489e818b4e72d5293069bfb",
>   "name": "moe",
>   "url": "http://www.moeshomepage.com"
> }
> 
> 
> {
>   "_id": "_design/yo",
>   "_rev": "11-3ac3428c8d47bb485b67f262248c8192",
>   "language": "javascript",
>   "views": {
>       "users": {
>           "map": "function(doc) {emit(doc.name,doc.url)}"
>       }
>   },
>   "shows": {
>       "showall": "function(doc,req){return <script>alert(1)</script>}"
>   }
> }
> 
> Thank you,
> 
> Dan
> 
> -- 
> View this message in context: http://couchdb-development.1959287.n2.nabble.com/get-emit-doc-name-doc-url-to-javascript-variable-tp5391246p5391246.html
> Sent from the CouchDB Development mailing list archive at Nabble.com.


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