Advertisement
Guest User

Backbone Marionette with LayoutView

a guest
Jan 29th, 2014
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.01 KB | None | 0 0
  1. // layout view
  2. define([
  3. "text!app/templates/posts/index.html",
  4. 'app/views/posts/collection',
  5. 'app/views/posts/partials/status',
  6. 'app/views/posts/partials/type',
  7. 'app/views/posts/partials/client_id',
  8. 'app/views/posts/partials/location_id',
  9. 'app/views/posts/partials/publish_from_datetime',
  10. 'app/views/posts/partials/publish_to_datetime',
  11. 'app/views/posts/partials/publish_target'
  12. ],
  13. function(Template, CollectionView,
  14. FilterStatusView, FilterTypeView, FilterClientIdView, FilterLocationIdView, FilterPublishFromView,
  15. FilterPublishToView, FilterPublishTargetView) {
  16. "use strict"
  17.  
  18. return Backbone.Marionette.Layout.extend({
  19.  
  20.  
  21. template: Template,
  22.  
  23.  
  24. regions: {
  25. filterType: '#filterTypeContainer',
  26. filterStatus: '#filterStatusContainer',
  27. filterPublishTarget: '#filterPublishTargetContainer',
  28.  
  29. filterClientId: '#filterTypeContainer',
  30. filterLocationId: '#filterLocationIdContainer',
  31.  
  32. filterPublishFrom: '#filterPublishFromContainer',
  33. filterPublishTo: '#filterPublishToContainer',
  34.  
  35. postList: '#postList'
  36. },
  37.  
  38.  
  39. initialize: function() {
  40.  
  41. // horrible scope issues with javascript
  42. var that = this;
  43.  
  44. // we give time changing its own vent because we only want to trigger a collection when from is less than to
  45. MyApp.vent.on('filter:changeDateTime', function() {
  46. // get the filter data
  47. var filter = that.getFilter();
  48.  
  49. // if the to time is higher than the from time
  50. if ( filter.publish_from_raw < filter.publish_to_raw) {
  51. that.renderCollection(filter);
  52. }
  53. });
  54.  
  55. // if the filter change event is triggered in a sub view
  56. MyApp.vent.on('filter:change', function() {
  57. // fetch the collection and pass its filters through
  58. that.renderCollection(that.getFilter());
  59. })
  60. },
  61.  
  62.  
  63. getFilter: function() {
  64.  
  65. // create our new filter object using jQuery picking the values
  66. var filter = {
  67. type: $('#filterType').val(),
  68. status: $('#filterStatus').val(),
  69. publish_target: $('#filterPublishTarget').val(),
  70. client_id: $('#filterClientId').val(),
  71. location_id: $('#filterLocationId').val(),
  72. };
  73.  
  74. // date time fields
  75. var publishFrom = new Date($('#filterPublishFrom').val());
  76. var publishTo = new Date($('#filterPublishTo').val());
  77.  
  78. if (!isNaN(parseFloat(publishFrom.getTime() / 1000)) && !isNaN(parseFloat(publishTo.getTime() / 1000))) {
  79.  
  80. filter.publish_from = $('#filterPublishFrom').val();
  81. filter.publish_from_raw = parseInt(publishFrom.getTime() / 1000);
  82. filter.publish_from_date = _.dateToYMD(publishFrom);
  83. filter.publish_from_time = _.dateToHM(publishFrom);
  84.  
  85. filter.publish_to = $('#filterPublishTo').val();
  86. filter.publish_to_raw = parseInt(publishTo.getTime() / 1000);
  87. filter.publish_to_date = _.dateToYMD(publishTo);
  88. filter.publish_to_time = _.dateToHM(publishTo);
  89. }
  90.  
  91. // set the filter object to local storage (extended, not typical)
  92. localStorage.setObject('postFilter', filter);
  93.  
  94. // return the filter after passing it through a null stripper
  95. return _.cleanNullFieldsFromObject(filter);
  96. },
  97.  
  98.  
  99. renderFilterType: function(options) {
  100. this.filterType.show(new FilterTypeView(options));
  101. },
  102.  
  103.  
  104. renderFilterStatus: function(options) {
  105. this.filterStatus.show(new FilterStatusView(options));
  106. },
  107.  
  108.  
  109. renderFilterPublishTarget: function(options) {
  110. this.filterPublishTarget.show(new FilterPublishTargetView(options));
  111. },
  112.  
  113.  
  114. renderFilterClientId: function(options) {
  115. this.filterClientId.show(new FilterClientIdView(options));
  116. },
  117.  
  118.  
  119. renderFilterLocationId: function(options) {
  120. this.filterLocationId.show(new FilterLocationIdView(options));
  121. },
  122.  
  123.  
  124. renderFilterPublishFrom: function(options) {
  125. this.filterPublishFrom.show(new FilterPublishFromView(options));
  126. },
  127.  
  128.  
  129. renderFilterPublishTo: function(options) {
  130. this.filterPublishTo.show(new FilterPublishToView(options));
  131. },
  132.  
  133.  
  134. renderCollection: function(options) {
  135. // render the post list
  136. this.postList.show(new CollectionView(options));
  137. },
  138.  
  139.  
  140. onRender: function () {
  141.  
  142. // do we need to over write the filter object from local storage?
  143. var postFilter = localStorage.getObject('postFilter');
  144.  
  145. // if local storage isn't empty then over ride the filter with it
  146. if (!_.isEmpty(postFilter)) {
  147. this.filter = postFilter;
  148. }
  149.  
  150. // render the filters on our page
  151. this.renderFilterType(this.filter);
  152. this.renderFilterStatus(this.filter);
  153. this.renderFilterPublishTarget(this.filter);
  154.  
  155. this.renderFilterClientId(this.filter);
  156. this.renderFilterLocationId(this.filter);
  157.  
  158. this.renderFilterPublishFrom(this.filter);
  159. this.renderFilterPublishTo(this.filter);
  160.  
  161. // render the collection
  162. console.log(this.filter);
  163. this.renderCollection(this.filter);
  164. }
  165.  
  166.  
  167. })
  168. })
  169.  
  170. // collection view
  171. define([
  172. "text!app/templates/posts/collection.html",
  173. "app/collections/posts",
  174. "app/views/posts/item"
  175. ],
  176. function(Template, Collection, Item) {
  177. "use strict"
  178. return Backbone.Marionette.CompositeView.extend({
  179.  
  180.  
  181. template: Template,
  182. itemView: Item,
  183. itemViewContainer: "tbody",
  184.  
  185.  
  186. collectionEvents: {
  187. 'sync': 'hideLoading'
  188. },
  189.  
  190.  
  191. initialize: function(options) {
  192.  
  193. // set loading, important we do this because we re-trigger the collection
  194. this.setLoading();
  195.  
  196. // call the collection
  197. this.collection = new Collection();
  198.  
  199. // fetch the collection
  200. return this.collection.fetch({data: options})
  201. },
  202.  
  203.  
  204. // hide the loading state
  205. hideLoading: function() {
  206. this.$el.find('.loading-latch').removeClass('loading-active');
  207. },
  208.  
  209.  
  210. // set loading by appending to the latch
  211. setLoading: function() {
  212. this.$el.find('.loading-latch').addClass('loading-active');
  213. }
  214.  
  215.  
  216. })
  217. })
  218. // status view
  219. define(["marionette", "text!app/templates/posts/partials/status.html", 'app/models/post'],
  220. function(Marionette, Template, Model) {
  221. "use strict"
  222. return Backbone.Marionette.ItemView.extend({
  223. template: Template,
  224.  
  225.  
  226. events: {
  227. 'change #filterStatus': 'onFilter'
  228. },
  229.  
  230.  
  231. initialize: function(options) {
  232. this.value = _.isEmpty(options) ? '-' : options.status;
  233. },
  234.  
  235.  
  236. onFilter: function() {
  237. MyApp.vent.trigger('filter:change');
  238. },
  239.  
  240.  
  241. serializeData: function() {
  242. return {values: new Model().getStatuses(), value: this.value};
  243. }
  244.  
  245.  
  246. })
  247. })
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement