backbone.js - Filter list of items -
i have list of items , query textfield. when come in query in textfield, want filter list of items based on query. how can this?
in view added event:
events: -> 'submit #query_form' : 'filterlinks'
this triggers function first empty list , want show items meet query criteria:
filterlinks: -> query = $('#query').val() @collection.reset()
how can filter list?
edit: finish view:
template: jst['links/index'] initialize: -> @collection.on('reset', @render, this) @collection.on('add', @appendlink, this) events: -> 'submit #new_link' : 'createlink' 'submit #query_form' : 'filterlinks' render: -> $(@el).html(@template()) @collection.each(@appendlink) createlink: (event) -> event.preventdefault() @collection.create title: $('#title').val() description: $('#description').val() url: $('#url').val() category: $('#category').val() votes: 0 $('#message').append('<div>link has been added succesfully!</div>').fadeout(5000) appendlink: (link) -> view = new hotlynx.views.link(model: link) $('#all_links').append(view.render().el) filterlinks: -> alert($('#query').val()) @collection.reset()
you can utilize underscore's filter
method filter collection , homecoming results want. see: http://underscorejs.org/#filter
then you'd utilize result of filter re-render view displays items.
backbone.js
No comments:
Post a Comment