Skip to content

Instantly share code, notes, and snippets.

@erzr
Created November 7, 2012 01:28
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save erzr/4028975 to your computer and use it in GitHub Desktop.
Save erzr/4028975 to your computer and use it in GitHub Desktop.
Backbone.js and Django-Tastypie Autocomplete
var app = app || {};
app.models = app.models || {};
app.collections = app.collections || {};
app.views = app.views || {};
(function($, window) {
(function(models) {
models.Tag = Backbone.Tastypie.Model.extend({
urlRoot: '/api/v1/tag/'
});
})(app.models);
(function(collections) {
collections.TagCollection = Backbone.Tastypie.Collection.extend({
urlRoot: '/api/v1/tag/',
model: app.models.Tag
});
})(app.collections);
(function(views) {
views.TagView = Backbone.View.extend({
initialize:function () {
this.tag_collection = new app.collections.TagCollection();
this.tag_collection.bind('reset', this.render, this);
},
events:{
'input #tag-input':'loadResults',
'click #tag-list li':'itemClicked'
},
loadResults:function (event) {
query = $(event.currentTarget).val();
this.tag_collection.filters['name__startswith'] = query;
this.tag_collection.fetch();
},
itemClicked:function (event) {
var value = $(event.currentTarget).find('a').html();
$('#tag-input').val($.trim(value));
},
render:function () {
var list = $('#tag-list');
list.html('');
this.tag_collection.each(function (i, item) {
list.append('<li><a href=\'#\'>' + i.get('name') + '</a></li>');
});
list.listview('refresh');
}
});
})(app.views);
$(function() {
window.tagView = new app.views.TagView({
el:$('#tag-selection')
});
});
})(jQuery, window);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment