PageRenderTime 45ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 1ms

/js/mps.js

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