Filtering by multiple things

26 views
Skip to first unread message

Nelson Pecora

unread,
Mar 13, 2013, 6:22:35 PM3/13/13
to spi...@googlegroups.com
Hi all. I have three Spine models (Artist, Album, Song), and I have an app that allows you to select which type (of the three) to display, and makes a list of records from the selected model. So far so good.

I wanted to filter the records displayed, and I made a filter() method in each model. Works great.

Now I want to filter by multiple things, and I can't figure out how to do it without creating a bunch of filter methods and calling them with a huge if/then list. My filters are artist (matches id), album (matches id), genres (an array of ids), and query (a catchall that matches name). The cumbersome and non-DRY way to do it would be:

if (artist && album && genres && query) { 
    model.matchAll()
} else if (artist && album && genres) {
    model.matchArtAlbGen() 
} else if (artist && album && query) {
    model.matchArtAlbQu() 
} else if (artist && genres && query) {

...and so on, iterating through each possible match and calling the relevant filtering method. This is complicated, unreadable, non-DRY, and gives me a visceral feeling of disgust when I contemplate coding it, and I'm certain there must be a better way. Got any ideas? Is it possible to pass the results of a filter into another filter? (I tried filtering in my controller, but I need to do sorting on the results afterwards, and I can't call model.sort() on a variable I create in the controller unless I create it with model.all() or model.select().)

Like I said, there must be some better way to do it, but I'm stumped.

Nelson Pecora

unread,
Mar 13, 2013, 7:37:39 PM3/13/13
to spi...@googlegroups.com
Is there some way I can filter, then filter those results? And basically chain the filters together as needed? I tried doing this, but it didn't work:


Thanks again.

Nelson Pecora

unread,
Mar 13, 2013, 7:52:44 PM3/13/13
to spi...@googlegroups.com
Looks like it's because items doesn't have access to the methods from its model. How do I fix this?

Macario Ortega

unread,
Apr 14, 2014, 3:39:31 PM4/14/14
to spi...@googlegroups.com

I've just saw your post.

I made a tiny lib to add support for chain filtering records. I haven't published as an extension yet because I would like it to support associations (collections).
But here's as a gist.


I've included the specs in lieu of documentation.

The specs filter by Person name:

You can do things like this:
Person.startsWith('M').endsWith('o')

Or like this:
Person.startsWith('M').not.endsWith('o')

Nelson Pecora

unread,
Apr 14, 2014, 4:47:21 PM4/14/14
to spi...@googlegroups.com
Oh, neat! I'll take a look at that.

--
You received this message because you are subscribed to a topic in the Google Groups "Spine" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/spinejs/4rTrJpNCj-k/unsubscribe.
To unsubscribe from this group and all its topics, send an email to spinejs+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages