You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@couchdb.apache.org by Gert Cuykens <ge...@gmail.com> on 2011/09/26 01:33:06 UTC
generate _update body using _show function
Can you call a show function inside a update function to generate a update body?
Example
{
"updates":{
"post":"function(doc,req){
for(i in req.form)doc[i]=req.form[i]
return [doc,{'headers':{'Content-Type':'text/html'},
'body':'\
<label>adress:</label> <input name=\"adress\"
value=\"'+doc.adress+'\"/><br/>\\n\
<label>city:</label> <input name=\"city\"
value=\"'+doc.city+'\"/><br/>\\n\
<label>country:</label> <input name=\"country\"
value=\"'+doc.country+'\"/><br/>\\n\
<label>phone:</label> <input name=\"phone\"
value=\"'+doc.phone+'\"/><br/>\\n\
<label>email:</label> <input name=\"email\"
value=\"'+doc.email+'\"/><br/>\\n\
<input name=\"_id\" value=\"'+doc._id+'\" type=\"hidden\"/>\\n\
<input name=\"_rev\" value=\"'+doc._rev+'\" type=\"hidden\"/>\\n\
<img id=\"picture\" src=\"/users/'+doc._id+'/picture.png\"/>'
}]
}"
},
"shows":{
"html":"function(doc,req){
return {headers:{'Content-Type':'text/html'},
body:'\
<label>adress:</label> <input name=\"adress\"
value=\"'+doc.adress+'\"/><br/>\\n\
<label>city:</label> <input name=\"city\"
value=\"'+doc.city+'\"/><br/>\\n\
<label>country:</label> <input name=\"country\"
value=\"'+doc.country+'\"/><br/>\\n\
<label>phone:</label> <input name=\"phone\"
value=\"'+doc.phone+'\"/><br/>\\n\
<label>email:</label> <input name=\"email\"
value=\"'+doc.email+'\"/><br/>\\n\
<input name=\"_id\" value=\"'+doc._id+'\" type=\"hidden\"/>\\n\
<input name=\"_rev\" value=\"'+doc._rev+'\" type=\"hidden\"/>\\n\
<img id=\"picture\" src=\"/users/'+doc._id+'/picture.png\"/>'
}
}"
}
}
Re: generate _update body using _show function
Posted by Gert Cuykens <ge...@gmail.com>.
Unfortunately templates can only be a static string and not a function?
{
"validate_doc_update":"function(n,o,u){if(!n['_id'])throw({forbidden:'no
_id'})}",
"updates":{
"post":"function(doc,req){
for(i in req.form)doc[i]=req.form[i]
return [doc,{headers:{'Content-Type':'text/html'},body:this.innerHTML(doc)}]
}"
},
"shows":{"html":"function(doc,req){return
{headers:{'Content-Type':'text/html'},body:this.innerHTML(doc)}}"},
"filters":{"design":"function(doc,req){return
'_design/'===doc._id.substr(0,8)}"},
"innerHTML":"function(doc){return '\
<label>adress:</label> <input name=\"adress\"
value=\"'+doc.adress+'\"/><br/>\\n\
<label>city:</label> <input name=\"city\"
value=\"'+doc.city+'\"/><br/>\\n\
<label>country:</label> <input name=\"country\"
value=\"'+doc.country+'\"/><br/>\\n\
<label>phone:</label> <input name=\"phone\"
value=\"'+doc.phone+'\"/><br/>\\n\
<label>email:</label> <input name=\"email\"
value=\"'+doc.email+'\"/><br/>\\n\
<input name=\"_id\" value=\"'+doc._id+'\" type=\"hidden\"/>\\n\
<input name=\"_rev\" value=\"'+doc._rev+'\" type=\"hidden\"/>\\n\
<img id=\"picture\" src=\"/users/'+doc._id+'/picture.png\"/>'
}"
}
PS syntax debugging design docs in couch db is !!!!!! because it does
not show the the character the parser did not like.
On Mon, Sep 26, 2011 at 5:13 PM, Zachary Zolton
<za...@gmail.com> wrote:
> Gert,
>
> Move your HTML templates to somewhere in the design doc, for example
> make an object property called templates.
>
> Now, within your update/list/show functions, you can then access them
> as follows:
>
> this.templates.templateName
>
> Also, I'd strongly suggest using a templating library (e.g.
> Mustache.js) instead of string concatenation as this will further
> simplify your code.
>
>
> Cheers,
>
> Zach
>
> On Sun, Sep 25, 2011 at 6:36 PM, Gert Cuykens <ge...@gmail.com> wrote:
>> On Mon, Sep 26, 2011 at 1:33 AM, Gert Cuykens <ge...@gmail.com> wrote:
>>> Can you call a show function inside a update function to generate a update body?
>>> Example
>>> {
>>> "updates":{
>>> "post":"function(doc,req){
>>> for(i in req.form)doc[i]=req.form[i]
>>> return [doc,{'headers':{'Content-Type':'text/html'},
>>> 'body':'\
>>> <label>adress:</label> <input name=\"adress\"
>>> value=\"'+doc.adress+'\"/><br/>\\n\
>>> <label>city:</label> <input name=\"city\"
>>> value=\"'+doc.city+'\"/><br/>\\n\
>>> <label>country:</label> <input name=\"country\"
>>> value=\"'+doc.country+'\"/><br/>\\n\
>>> <label>phone:</label> <input name=\"phone\"
>>> value=\"'+doc.phone+'\"/><br/>\\n\
>>> <label>email:</label> <input name=\"email\"
>>> value=\"'+doc.email+'\"/><br/>\\n\
>>> <input name=\"_id\" value=\"'+doc._id+'\" type=\"hidden\"/>\\n\
>>> <input name=\"_rev\" value=\"'+doc._rev+'\" type=\"hidden\"/>\\n\
>>> <img id=\"picture\" src=\"/users/'+doc._id+'/picture.png\"/>'
>>> }]
>>> }"
>>> },
>>> "shows":{
>>> "html":"function(doc,req){
>>> return {headers:{'Content-Type':'text/html'},
>>> body:'\
>>> <label>adress:</label> <input name=\"adress\"
>>> value=\"'+doc.adress+'\"/><br/>\\n\
>>> <label>city:</label> <input name=\"city\"
>>> value=\"'+doc.city+'\"/><br/>\\n\
>>> <label>country:</label> <input name=\"country\"
>>> value=\"'+doc.country+'\"/><br/>\\n\
>>> <label>phone:</label> <input name=\"phone\"
>>> value=\"'+doc.phone+'\"/><br/>\\n\
>>> <label>email:</label> <input name=\"email\"
>>> value=\"'+doc.email+'\"/><br/>\\n\
>>> <input name=\"_id\" value=\"'+doc._id+'\" type=\"hidden\"/>\\n\
>>> <input name=\"_rev\" value=\"'+doc._rev+'\" type=\"hidden\"/>\\n\
>>> <img id=\"picture\" src=\"/users/'+doc._id+'/picture.png\"/>'
>>> }
>>> }"
>>> }
>>> }
>>>
>>
>> Or assign a global variable that can be used in _show and _update functions?
>>
>
Re: generate _update body using _show function
Posted by Zachary Zolton <za...@gmail.com>.
Gert,
Move your HTML templates to somewhere in the design doc, for example
make an object property called templates.
Now, within your update/list/show functions, you can then access them
as follows:
this.templates.templateName
Also, I'd strongly suggest using a templating library (e.g.
Mustache.js) instead of string concatenation as this will further
simplify your code.
Cheers,
Zach
On Sun, Sep 25, 2011 at 6:36 PM, Gert Cuykens <ge...@gmail.com> wrote:
> On Mon, Sep 26, 2011 at 1:33 AM, Gert Cuykens <ge...@gmail.com> wrote:
>> Can you call a show function inside a update function to generate a update body?
>> Example
>> {
>> "updates":{
>> "post":"function(doc,req){
>> for(i in req.form)doc[i]=req.form[i]
>> return [doc,{'headers':{'Content-Type':'text/html'},
>> 'body':'\
>> <label>adress:</label> <input name=\"adress\"
>> value=\"'+doc.adress+'\"/><br/>\\n\
>> <label>city:</label> <input name=\"city\"
>> value=\"'+doc.city+'\"/><br/>\\n\
>> <label>country:</label> <input name=\"country\"
>> value=\"'+doc.country+'\"/><br/>\\n\
>> <label>phone:</label> <input name=\"phone\"
>> value=\"'+doc.phone+'\"/><br/>\\n\
>> <label>email:</label> <input name=\"email\"
>> value=\"'+doc.email+'\"/><br/>\\n\
>> <input name=\"_id\" value=\"'+doc._id+'\" type=\"hidden\"/>\\n\
>> <input name=\"_rev\" value=\"'+doc._rev+'\" type=\"hidden\"/>\\n\
>> <img id=\"picture\" src=\"/users/'+doc._id+'/picture.png\"/>'
>> }]
>> }"
>> },
>> "shows":{
>> "html":"function(doc,req){
>> return {headers:{'Content-Type':'text/html'},
>> body:'\
>> <label>adress:</label> <input name=\"adress\"
>> value=\"'+doc.adress+'\"/><br/>\\n\
>> <label>city:</label> <input name=\"city\"
>> value=\"'+doc.city+'\"/><br/>\\n\
>> <label>country:</label> <input name=\"country\"
>> value=\"'+doc.country+'\"/><br/>\\n\
>> <label>phone:</label> <input name=\"phone\"
>> value=\"'+doc.phone+'\"/><br/>\\n\
>> <label>email:</label> <input name=\"email\"
>> value=\"'+doc.email+'\"/><br/>\\n\
>> <input name=\"_id\" value=\"'+doc._id+'\" type=\"hidden\"/>\\n\
>> <input name=\"_rev\" value=\"'+doc._rev+'\" type=\"hidden\"/>\\n\
>> <img id=\"picture\" src=\"/users/'+doc._id+'/picture.png\"/>'
>> }
>> }"
>> }
>> }
>>
>
> Or assign a global variable that can be used in _show and _update functions?
>
Re: generate _update body using _show function
Posted by Gert Cuykens <ge...@gmail.com>.
On Mon, Sep 26, 2011 at 1:33 AM, Gert Cuykens <ge...@gmail.com> wrote:
> Can you call a show function inside a update function to generate a update body?
> Example
> {
> "updates":{
> "post":"function(doc,req){
> for(i in req.form)doc[i]=req.form[i]
> return [doc,{'headers':{'Content-Type':'text/html'},
> 'body':'\
> <label>adress:</label> <input name=\"adress\"
> value=\"'+doc.adress+'\"/><br/>\\n\
> <label>city:</label> <input name=\"city\"
> value=\"'+doc.city+'\"/><br/>\\n\
> <label>country:</label> <input name=\"country\"
> value=\"'+doc.country+'\"/><br/>\\n\
> <label>phone:</label> <input name=\"phone\"
> value=\"'+doc.phone+'\"/><br/>\\n\
> <label>email:</label> <input name=\"email\"
> value=\"'+doc.email+'\"/><br/>\\n\
> <input name=\"_id\" value=\"'+doc._id+'\" type=\"hidden\"/>\\n\
> <input name=\"_rev\" value=\"'+doc._rev+'\" type=\"hidden\"/>\\n\
> <img id=\"picture\" src=\"/users/'+doc._id+'/picture.png\"/>'
> }]
> }"
> },
> "shows":{
> "html":"function(doc,req){
> return {headers:{'Content-Type':'text/html'},
> body:'\
> <label>adress:</label> <input name=\"adress\"
> value=\"'+doc.adress+'\"/><br/>\\n\
> <label>city:</label> <input name=\"city\"
> value=\"'+doc.city+'\"/><br/>\\n\
> <label>country:</label> <input name=\"country\"
> value=\"'+doc.country+'\"/><br/>\\n\
> <label>phone:</label> <input name=\"phone\"
> value=\"'+doc.phone+'\"/><br/>\\n\
> <label>email:</label> <input name=\"email\"
> value=\"'+doc.email+'\"/><br/>\\n\
> <input name=\"_id\" value=\"'+doc._id+'\" type=\"hidden\"/>\\n\
> <input name=\"_rev\" value=\"'+doc._rev+'\" type=\"hidden\"/>\\n\
> <img id=\"picture\" src=\"/users/'+doc._id+'/picture.png\"/>'
> }
> }"
> }
> }
>
Or assign a global variable that can be used in _show and _update functions?