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