# Implementing a Search Route

**URL:** <https://discuss.emberjs.com/t/implementing-a-search-route/2326>\
**Category:** Uncategorized\
**Created:** [August 29, 2013, 3:15am UTC](https://discuss.emberjs.com/t/implementing-a-search-route/2326 "2013-08-29T03:15:36Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![bittersweetryan](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/bittersweetryan/32/4555_2.png) [@bittersweetryan](https://discuss.emberjs.com/u/bittersweetryan)\
**Post date:** [August 29, 2013, 3:15am UTC](https://discuss.emberjs.com/t/implementing-a-search-route/2326/1 "2013-08-29T03:15:36Z")

</div>

Hi Everyone,

I’d love some advice on an implementation of a search function. The code works right now, however I’m not sure that I’m doing it the best way. It uses a custom view called SearchCardsView:

```
App.SearchCardsView = Ember.TextField.extend({
    keyUp : function(){
        this.get('controller').transitionToRoute( 'search', this.get( 'controller.query' ) );
    }
});

```

The query text will search on objects that are part of a hasMany collection on a model. In addition the application may have more than one list displayed at a time:

```
App.List = DS.Model.extend({
    listName : DS.attr( 'string' ),
    cards : DS.hasMany( 'App.Card' )
});.  

```

The card model’s code looks like this:

```
App.Card = DS.Model.extend({
	description : DS.attr( 'string' ),
	list : DS.belongsTo( 'App.List' ),
	show : DS.attr( 'boolean', { defaultValue : true } )
});

```

My implementation of the route uses the dynamic search string to set the “show” property of each card in a list:

```
model : function( params ){
    var lists = App.List.find( ),
        query = params.query,
        re = new RegExp( query, 'i' );

    this.set( 'query', query );

    lists.forEach( function( list ){
        var cards = list.get( 'cards' ).forEach( function( card ){

            card.set( 'show',
                ( query.length ) ? re.test( card.get( 'description' ) ) : true
            );
        } );
    });

    return lists;
}

```

Lastly the view uses a `#if` helper on the show property to show the cards that match the search string. I’d love any suggestions on how I can make this code better?

-Ryan
