PageRenderTime 47ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/pub/js/bundles/template/index.js

https://github.com/oliver96/adsys
JavaScript | 74 lines | 66 code | 8 blank | 0 comment | 1 complexity | f3184c3dc8fc20edb048e8ada7026f6f MD5 | raw file
  1. define([
  2. 'jquery'
  3. , 'underscore'
  4. , 'backbone'
  5. , 'vendors/backbone/backbone-pcollection'
  6. , 'vendors/backbone/backbone-paginator'
  7. , 'bundles/_public/utils'
  8. , 'bundles/_models/template'
  9. ], function($, _, Backbone) {
  10. var AdtplItemView = Backbone.View.extend({
  11. tagName : "tr"
  12. , template : _.template($('#adtpl_item_template').html())
  13. , events: {
  14. 'click a.edit-link': 'editEvent'
  15. }
  16. , initialize : function() {
  17. this.model.bind('change', this.render, this);
  18. this.model.bind('destroy', this.remove, this);
  19. }
  20. , render : function() {
  21. var $el = $(this.el);
  22. $el.html(this.template(this.model.toJSON()));
  23. return this;
  24. }
  25. , editEvent : function(e) {
  26. var id = parseInt($(e.target).closest('tr').find('input').val())
  27. , url = App.router.url({'m' : 'template', 'a' : 'edit', 'id' : id});
  28. window.location.href = url;
  29. }
  30. });
  31. var AdtplList = Backbone.PaginatedCollection.extend({
  32. 'model': AdTemplate
  33. , 'url': App.router.url({
  34. 'm' : 'template'
  35. , 'a' : 'rows'
  36. })
  37. });
  38. var adtplList = new AdtplList();
  39. var Paginator = Backbone.Paginator.extend({
  40. collection : adtplList
  41. , el : $('.pagination')
  42. });
  43. new Paginator();
  44. var MainView = Backbone.View.extend({
  45. el: $("body")
  46. , events: {
  47. }
  48. , initialize: function() {
  49. this.listenTo(adtplList, 'reset', this.onRenderList);
  50. adtplList.fetch();
  51. }
  52. , onRenderList: function() {
  53. this.$("#adtpl_list").empty();
  54. if(adtplList.length > 0) {
  55. adtplList.each(this.addAdtplRow, this);
  56. }
  57. }
  58. , addAdtplRow: function(model) {
  59. var view = new AdtplItemView({
  60. 'model' : model
  61. });
  62. this.$("#adtpl_list").append(view.render().el);
  63. }
  64. });
  65. new MainView();
  66. });