Created
November 7, 2012 01:28
-
-
Save erzr/4028975 to your computer and use it in GitHub Desktop.
Backbone.js and Django-Tastypie Autocomplete
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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