Disclaimer: like any solution in Backbone, there are several ways to solve this problem, the next answer is probably overdoing it and should be taken with all due care.
Define your models
They will represent your data and how they should be processed. Here I define a question model (empty, but you never know), a collection of questions (where filtering is defined for a specific term), and a search state controller that is responsible for saving the current term and related questions:
var Question = Backbone.Model.extend(); var Questions = Backbone.Collection.extend({ model: Question, matches: function (term) { if (term==="") return []; term = term.toLowerCase(); return this.filter(function(model) { return model.get('question').toLowerCase().indexOf(term)!==-1 }); } }); var QuestionController = Backbone.Model.extend({ defaults: { term: "" }, initialize: function(opts) { this.questions = opts.questions;
Define your views
They will display the state of models in the DOM and will handle user interactions. Suppose you have a SearchView that will process input and offers that will display current offers:
var SearchView = Backbone.View.extend({ events: { 'keyup ': function (e) { this.model.set('term', this.$el.val()); }, 'focus ': function (e) { this.trigger('start'); }, 'blur ': function (e) { this.trigger('stop'); } } }); var SuggestionsView = Backbone.View.extend({ initialize: function () { _.bindAll(this, 'activate', 'deactivate', 'render'); this.listenTo(this.model.matches, 'reset', this.render); }, activate: function () { this.$el.addClass('active'); this.render(); }, deactivate: function () { this.$el.removeClass('active'); this.$el.html(" "); }, render: function () { var html = ''; if (this.model.get("term") === "") { html = "<p> start typing a question </p>"; } else { if (this.model.matches.length === 0) { html = "<p> No match </p>"; } else { this.model.matches.each(function(model) { html += '<p>'+model.get('question')+'</p>'; }); } } this.$el.html(html); } });
Bring models and views together
Run, bind events and run things
var questions = new Questions([ {question: "what is your name"}, {question: "How old are you"}, {question: "what is your mothers name"}, {question: "where do work/or study"} ]); var controller = new QuestionController({ questions: questions }); var vSearch = new SearchView({ el: '#suggestinput', model: controller }); var vSuggestions=new SuggestionsView({ el: '#suggestions', model: controller }); vSuggestions.listenTo(vSearch, 'start', vSuggestions.activate); vSuggestions.listenTo(vSearch, 'stop', vSuggestions.deactivate);
And a script to play with http://jsfiddle.net/nikoshr/QSE95/
Of course, you could greatly simplify this by having a single type of processing for all interactions, but where would it be fun?