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

/pub/js/bundles/advertising/index.js

https://github.com/oliver96/adsys
JavaScript | 89 lines | 66 code | 8 blank | 15 comment | 1 complexity | e4e490ade4b309c812018efd7aec8122 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/advertising'
  9. ], function($, _, Backbone) {
  10. var AdItemView = Backbone.View.extend({
  11. //列表标签.
  12. tagName : "tr"
  13. // 为单个元素缓存模板.
  14. , template : _.template($('#ad_item_template').html())
  15. // 注册事件
  16. , events: {
  17. 'click a.edit-link': 'editEvent'
  18. }
  19. // AdvView视图监听 model的事件变化,重新渲染
  20. // **Advertiser** 和 **AdvView** 成一一对应的关系.
  21. , initialize : function() {
  22. this.model.bind('change', this.render, this);
  23. this.model.bind('destroy', this.remove, this);
  24. }
  25. // 重新渲染单条广告主的列表行.
  26. , render : function() {
  27. var $el = $(this.el);
  28. $el.html(this.template(this.model.toJSON()));
  29. return this;
  30. }
  31. , editEvent : function(e) {
  32. var id = parseInt($(e.target).closest('tr').find('input').val())
  33. , url = App.router.url({'m' : 'advertising', 'a' : 'edit', 'id' : id});
  34. window.location.href = url;
  35. }
  36. });
  37. // 创建一个带分页的数据行的集合
  38. var AdList = Backbone.PaginatedCollection.extend({
  39. 'model': Advertising
  40. , 'url': App.router.url({
  41. 'm' : 'advertising'
  42. , 'a' : 'rows'
  43. })
  44. });
  45. // 列表对象实例化
  46. var adList = new AdList();
  47. // 分页条对象实例化
  48. var Paginator = Backbone.Paginator.extend({
  49. collection : adList
  50. , el : $('.pagination')
  51. });
  52. new Paginator();
  53. var MainView = Backbone.View.extend({
  54. el: $("body")
  55. , events: {
  56. }
  57. // 初始化事件
  58. , initialize: function() {
  59. // 绑定RESET事件,当advList数据重置时确发onRenderList方法
  60. this.listenTo(adList, 'reset', this.onRenderList);
  61. //this.listenTo(advList, 'change', this.onRenderList);
  62. // 从服务器获取广告主列表数据
  63. adList.fetch();
  64. }
  65. // 渲染广告主列表
  66. , onRenderList: function() {
  67. this.$("#ad_list").empty();
  68. if(adList.length > 0) {
  69. adList.each(this.addAdRow, this);
  70. }
  71. }
  72. // 添加一行广告主数据行
  73. , addAdRow: function(model) {
  74. var view = new AdItemView({
  75. 'model' : model
  76. });
  77. this.$("#ad_list").append(view.render().el);
  78. }
  79. });
  80. new MainView();
  81. });