PageRenderTime 51ms CodeModel.GetById 26ms RepoModel.GetById 1ms app.codeStats 0ms

/js/states.js

https://github.com/akshayrawat/DirectoryOfMPs
JavaScript | 59 lines | 52 code | 7 blank | 0 comment | 1 complexity | be620897e52d200bbf889b56891f2e50 MD5 | raw file
  1. var State = Backbone.Model.extend({
  2. htmlId : function() {
  3. return "state-" + this.id;
  4. }
  5. });
  6. var States = Backbone.Collection.extend({
  7. model: State,
  8. initialize: function(){
  9. var that = this;
  10. $.ajax({
  11. url : Config.api_host + '/states.json',
  12. dataType : "jsonp",
  13. success : function (data, status, xhr) {
  14. that.refresh($.parseJSON(data.model));
  15. }
  16. });
  17. },
  18. });
  19. var StateView = Backbone.View.extend({
  20. id: 'states',
  21. initialize: function(){
  22. this.handleEvents();
  23. this.states = new States();
  24. this.compiledView = null;
  25. _.bindAll(this, "updateView");
  26. this.states.bind('refresh', this.updateView);
  27. },
  28. events: {
  29. "click #states table tr[id]": "stateSelected"
  30. },
  31. updateView: function() {
  32. this.render();
  33. },
  34. render: function() {
  35. if(this.compiledView) {
  36. return this;
  37. }
  38. var that = this;
  39. $.get('/html/_states.tpl.html', function(statesTemplate) {
  40. that.renderComplete(statesTemplate);
  41. });
  42. return this;
  43. },
  44. renderComplete: function(statesTemplate) {
  45. this.compiledView = $.tmpl(statesTemplate, {stateList: this.states.models});
  46. this.$(this.el).html(this.compiledView);
  47. $("#col-1").html(this.el);
  48. },
  49. stateSelected: function(event){
  50. new MPView({stateId: event.currentTarget.id.replace("state-","")});
  51. }
  52. });