couchdb-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Clement Hallet <clem...@challet.eu>
Subject Re: rewrite rules for alphabetic browse
Date Tue, 21 Jun 2011 15:28:50 GMT
Glad to hear it helped.
I can't tell why it's still sorted, but to be sure it remains like that i would do :

View

function(doc) {
   if (doc.type && doc.name) {
     var lowerCased = doc.name.toLowerCase();
     var firstLetter = lowerCased.toLowerCase()[0];
     emit([doc.type, firstLetter, lowerCased], doc.name);
   }
 }

Rewrite

 {
   "from": "artists/:key",
   "to": "_list/artists/type_name",
   "query": {
     "startkey": ["artist", ":key"],
     "endkey": ["artist", ":key", {}]
   }
 }

So the 3rd element of the key is not used as a filter but more as a sorting field.

-- 
Clément


Le 21 juin 2011 à 17:17, Gabriel Farrell a écrit :

> On Tue, Jun 21, 2011 at 10:29 AM, Clement Hallet <clement@challet.eu> wrote:
>> Hello Gabriel,
>> 
>> Why not making a key using only the first letter ?
>> 
>>  function(doc) {
>>   if (doc.type && doc.name) {
>>     emit([doc.type, doc.name[0]);
>>   }
>>  }
>> 
>> 
>> and querying it with that key = ["artist","a"]
> 
> Interesting idea. If I only use the first letter (and lowercase it as
> well to catch names starting with "A"), then I can get the URLs I
> want. I thought I would lose the sorting within each letter, but
> they're still sorted! I don't understand why that is, but it's a nice
> surprise.
> 
> My view now looks like this:
> 
>  function(doc) {
>    if (doc.type && doc.name) {
>      var firstLetter = doc.name.toLowerCase()[0];
>      emit([doc.type, firstLetter], doc.name);
>    }
>  }
> 
> And the rewrite is simplified to this:
> 
>  {
>    "from": "artists/:key",
>    "to": "_list/artists/type_name",
>    "query": {
>      "key": ["artist", ":key"]
>    }
>  },
> 
> Thanks!
> 
>> --
>> Clément
>> 
>> Le 21 juin 2011 à 16:18, Gabriel Farrell a écrit :
>> 
>>> I'm putting together a site where I have artists and works of art. I
>>> would like to browse those artists and works alphabetically, with URLs
>>> like http://example.com/artists/a and http://example.com/works/b. I
>>> have a view called type_name:
>>> 
>>>  function(doc) {
>>>    if (doc.type && doc.name) {
>>>      emit([doc.type, doc.name]);
>>>    }
>>>  }
>>> 
>>> My "artists" list renders the results from that view in a template. I
>>> can get the first page of artists with a rewrite like this:
>>> 
>>>  {
>>>    "from": "artists/a",
>>>    "to": "_list/artists/type_name",
>>>    "query": {
>>>      "startkey": ["artist", "a"],
>>>      "endkey": ["artist", "aZZZZZ"]
>>>    }
>>>  },
>>> 
>>> How would I generalize this for all letters of the alphabet? I want to
>>> do something like the following, but the last ":startkey" isn't
>>> substituted:
>>> 
>>>  {
>>>    "from": "artists/:startkey",
>>>    "to": "_list/artists/type_name",
>>>    "query": {
>>>      "startkey": ["artist", ":startkey"],
>>>      "endkey": ["artist", ":startkeyZZZZZ"]
>>>    }
>>>  },
>>> 
>>> I can achieve something close by extending the URL to
>>> http://example.com/artists/a/aZZZZZ and using the following rewrite:
>>> 
>>>  {
>>>    "from": "artists/:startkey/:endkey",
>>>    "to": "_list/artists/type_name",
>>>    "query": {
>>>      "startkey": ["artist", ":startkey"],
>>>      "endkey": ["artist", ":endkey"]
>>>    }
>>>  },
>>> 
>>> The URL is uglier but it works. Is there any way to make the shorter
>>> URL work? Frankly, I think some of my trouble is that, coming from
>>> other web frameworks, I'm not used to my URLs being constrained in
>>> this way. I want to be able to grab the request path, munge it all
>>> over with JavaScript, then send it on to my lists, shows, views, etc.
>> 
>> 


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