term = null
highlight = (text, search, case_sensitive=true) ->
if search
text = text.toString()
search = search.toString()
bits =
if case_sensitive
text.split(search)
else
regex = new RegExp(search, "gi")
text.split(regex)
str = []
i = 1
for bit in bits
str.push m 'span.highlight', search if i % 2 is 0
str.push m 'span', bit
i++
str
else
text
data = [
{ id: 1, name: 'The quick brown fox jumps through hopes' }
{ id: 2, name: 'The lazy bear slept through the winter' }
{ id: 3, name: 'The stupid pug ate a ton of dirt' }
{ id: 4, name: 'The fat cow was turned into steaks' }
]
window.app =
shared:
header: {}
tasks:
index: {}
app.shared.header.controller = ->
search: (ev)->
term = ev.target.value
#querystring = m.route.buildQueryString {search: term}
m.route '/', {search: term}
app.shared.header.view = (ctrl)->
attrs =
placeholder: 'Search for tasks'
value: term
onkeyup: ctrl.search
m '.header',
m '.search', [
m "input[type='text']", attrs
]
app.tasks.index.controller = ->
m.redraw.strategy 'diff'
app.tasks.index.view = ->
tasks = []
tasks.push m '.task', highlight(t.name,term) for t in data
[
m.component app.shared.header
m '.tasks', tasks
]
routes =
'/': app.tasks.index
m.route.mode = 'pathname'
m.route document.body, '/', routes
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Fiddle Pages
Your fiddles accessible under a custom domain and a vanity path - the simplest hosting for your personal project.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Get Fiddle Pages with PRO
Your fiddles accessible under a custom domain and a vanity path - the simplest hosting for your personal project.