Calling multiple views in CouchApp query

☆樱花仙子☆ 提交于 2019-12-11 11:59:31

问题


I need to search the CouchDB based on several criteria entered in a form. Name, an array of Tags and so on. I would then need various views to index on these fields. Ultimately, all the results will be collated in data.js and provided to mustache.html. Say there are 3 views - docsByName, docsByTags, docsById.

What I don't know is, how to query all these views in query.js. Can this be done and how ?

Or should the approach be of that to write one view that makes multiple emits for each search somehow ?

Thank you.


回答1:


From what you say I assume you are using Evently, so I will quote from Evently primer:

The async function is the main star, which in this case makes an Ajax request (but it can do anything it wants). Another important thing to note is that the first argument to the async function is a callback which you use to tell Evently when you are done with your asynchronous action. [...] Whatever you pass to the callback function then becomes the first item passed to the data function.

In short: put your Ajax requests in async.js.

As a side note: Evently is only one of the possible choices to write a couchapp and it is not clear if it is maintained. However it works and it is easy to rearrange the code to not use it.

EDIT: here is a sample async function (cut&paste from an old program):

function(cb, e) {
  var app = $$(this).app
    ;
  app.db.openDoc('SOMEDOCID', {
    error: function(code, error, reason) {
      alert("Error("+code+" "+error+"): "+reason);
    }
  , success: function(doc) {
      app.view('SOMEVIEWNAME', {
        include_docs: true
      , error: function(code, error, reason) {
          alert("Error("+code+" "+error+"): "+reason);
        }
      , success: function(resp) {
          resp.doc = doc;
          cb(resp);
        }
      });
    }
  });
}


来源:https://stackoverflow.com/questions/6074017/calling-multiple-views-in-couchapp-query

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!