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

/js/backbone.baseapp.js

https://github.com/joon1030/Backbone.BaseApp
JavaScript | 154 lines | 113 code | 38 blank | 3 comment | 4 complexity | 592e07a5cd1ff0e80ec818bb6e139d30 MD5 | raw file
  1. /**
  2. * Created by joon1030 on 2014. 6. 13..
  3. */
  4. ;(function($){
  5. var callbackCnt = 0;
  6. var Model = Backbone.Model.extend({
  7. "url" : function(){
  8. return this.API + "?" + $.param(this.params);
  9. },
  10. "load" : function(options){
  11. this.apiType = "jsonp";
  12. Backbone.Model.prototype.fetch.call(this, _.extend({
  13. "reset" : true,
  14. "dataType" : "jsonp",
  15. "cache" : true,
  16. "jsonpCallback" : "BaseApp" + callbackCnt++
  17. }, options));
  18. },
  19. });
  20. var Item = Backbone.View.extend({
  21. "tagName" : "li",
  22. "template" : _.template("<div><%=obj%></div>"),
  23. "initialize" : function(options){
  24. _.bindAll(this, "render");
  25. _.extend(this, options);
  26. this.on("preRender" , this.preRender);
  27. this.on("postRender" , this.postRender);
  28. this.postInitialize();
  29. },
  30. "postInitialize" : function(){
  31. },
  32. "render" : function(){
  33. this.trigger("preRender");
  34. this.$el.html(this.template(this.model.toJSON()));
  35. this.trigger("postRender");
  36. return this;
  37. },
  38. "preRender" : function(){
  39. },
  40. "postRender" : function(){
  41. }
  42. });
  43. var Collection = Backbone.Collection.extend({
  44. "params" : {
  45. },
  46. "API" : "",
  47. "url" : function(){
  48. return this.API + "?" + $.param(this.params);
  49. },
  50. "model" : Model,
  51. "fetch" : function(options){
  52. Backbone.Collection.prototype.fetch.call(this, _.extend({
  53. "reset" : true
  54. }, options));
  55. },
  56. "load" : function(options){
  57. this.apiType = "jsonp";
  58. Backbone.Collection.prototype.fetch.call(this, _.extend({
  59. "reset" : true,
  60. "dataType" : "jsonp",
  61. "cache" : true,
  62. "jsonpCallback" : "BaseApp" + callbackCnt++
  63. }, options));
  64. },
  65. "next" : function(paramKey){
  66. this.params[paramKey] = parseInt(this.params[paramKey]) + 1;
  67. if(this.apiType == "jsonp"){
  68. this.load();
  69. }
  70. else this.fetch();
  71. }
  72. });
  73. var AppView = Backbone.View.extend({
  74. "views" : [],
  75. "Item" : Item,
  76. "Collection" : Collection,
  77. "initialize" : function(_options){
  78. _.extend(this, _options);
  79. _.bindAll(this,"render", "append");
  80. this.on("preRender", this.preRender);
  81. this.on("postRender", this.postRender);
  82. this.collection = new this.Collection();
  83. this.collection.on("reset" , this.render);
  84. this.firstRender = true;
  85. this.postInitialize();
  86. },
  87. "postInitialize" : function(){
  88. },
  89. "render" : function(){
  90. this.trigger("preRender");
  91. this.collection.each(this.append);
  92. if(this.firstRender){
  93. this.firstRender = false;
  94. this.trigger("firstRender");
  95. }
  96. this.trigger("postRender");
  97. },
  98. "append" : function(model , idx){
  99. var view = new this.Item({
  100. "model" : model.set("idx" , idx)
  101. });
  102. this.$el.append(view.render().el);
  103. this.views.push[view];
  104. },
  105. "preRender" : function(){
  106. },
  107. "postRender" : function(){
  108. }
  109. });
  110. Backbone.BaseApp = {
  111. "Model" : Model,
  112. "Item" : Item,
  113. "Collection" : Collection,
  114. "AppView" : AppView
  115. };
  116. })(jQuery);