Here is a simple tutorial on how to build a filtered list in CanJS. For example, lets say that we have a list of people and we want to display people that have birthdays this month.
Our mustache template would like something like this:
people is a CanJS model list , birthdayThisMonth is a Mustache helper. This Mustache helper looks like this:
12345678910111213141516171819202122
birthdayThisMonth:function(people,options){varperson,bd,bdMonth,thisMonth;if(people&&people.length){varres=[];for(vara=0;a<people.length;a++){person=people[a];bd=person.attr('birthdate');bdMonth=newDate(bd).getMonth();thisMonth=(newDate).getMonth();if(bdMonth===thisMonth){// we want to display this birthday// so add this to the array// options.fn is a function that will return the final string using the mustache templateres.push(options.fn(person));}}// we have an array, but we need to return a stringreturnres.join(' ');}}
Then we just need to pass the helper to the view (or declare it as global helper):