当前位置: 动力学知识库 > 问答 > 编程问答 >

backbone.js - How can I bind datasourse of bootstrap typeahead in my backbone view?

问题描述:

I have a backbone view. I want to set datasource of bootstrap typeahead from server in every change occur in the search text field. How can I do this?? This is my Search view

window.SearchByChoiceView = Backbone.View.extend({

initialize: function () {

this.model = new SearchByChoiceModel();

this.render();

},

render: function () {

$(this.el).empty().html(this.template(this.model.toJSON()));

return this;

},

});

window.SearchByChoiceModel = Backbone.Model.extend({

initialize: function () {

},

defaults: {

id: 1,

SearchKeyword: "",

"tags": new Array()

}

});

And this is my search field:

 <input type="text" required placeholder="search" class="input-span3" id="SearchKeyword" title= "Search Keyword" name="SearchKeyword" value="<%= SearchKeyword %>" style="margin: 0 auto;" data-provide="typeahead" data-items="4">

Please anyone help. I am a begginer with backbone. May be it is easy but it is very hard to handle for me. I have tried a lot.

网友答案:

I believe you can do this:

$( '.typeahead' ).typeahead( {
    source : typeAhead.people.pluck( 'name' )
  } );
});

Where "typeAhead.people" is your collection instance. In your case

myApp.myCollection.pluck( 'SearchKeyword' )

JSBin:

http://jsbin.com/upigej/1/edit

分享给朋友:
您可能感兴趣的文章:
随机阅读: