Skip to content

Instantly share code, notes, and snippets.

@lzsucceed
Last active August 29, 2015 14:02
Show Gist options
  • Save lzsucceed/e768035bc0b0a2930e84 to your computer and use it in GitHub Desktop.
Save lzsucceed/e768035bc0b0a2930e84 to your computer and use it in GitHub Desktop.
class SearchView extends Backbone.View
el: $ '#submitbtn'
initialize: ->
_.bindAll @, "searchStart", "makeSelectedTextHighLight"
@.collection = new Dichelp.Collections.SelectedWords
@.sources = Dichelp.Collections.WebAPIs
return undefined
events: 'click': 'searchStart'
makeSelectedTextHighLight: (targetText) ->
console.log "set HighLight on text"
textArray = $('#selectionArea').text().split " "
resultHtml = ""
_.map textArray, (content) ->
content = content.trim()
if content == targetText
console.log "=="
resultHtml = resultHtml + " <span class='text-high-light'> " + content + "</span>"
else
resultHtml = resultHtml + " " + content
$('#selectionArea').html resultHtml
return
searchStart: ->
console.log "search start!"
text = $('#searchtext').val()
if text == ""
console.log "empty"
return
text = text.trim()
if @.collection.where({ word : text}).length == 0
newWord = new Dichelp.Models.SelectedWordModel(
{
word : text
urls : @.sources
}
)
@.collection.add newWord
@.makeSelectedTextHighLight(text)
collectionView = new Dichelp.Views.ResultCollections(
{ model : newWord}
)
collectionView.runTask()
else
console.log $('#searchtext').val() + "already exists"
return undefined
searchView = new SearchView
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment