Advertisement
Guest User

Backbone Marionette with CollectionView

a guest
Jan 29th, 2014
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // collection view
  2. define(["marionette", "text!app/templates/posts/collection.html", "app/collections/posts", "app/views/posts/item",
  3.   // we could just create a dead view that's sole job is including these, maybe /posts/filter?
  4.   'app/views/posts/partials/status',
  5.   'app/views/posts/partials/type',
  6.   'app/views/posts/partials/client_id',
  7.   'app/views/posts/partials/location_id',
  8.   'app/views/posts/partials/publish_from_datetime',
  9.   'app/views/posts/partials/publish_to_datetime',
  10.   'app/views/posts/partials/publish_target'
  11. ],
  12.   function(Marionette, Template, Collection, Item,
  13.            StatusView, TypeView, ClientIdView, LocationIdView, PublishFromDateTimeView,
  14.            PublishToDateTimeView, PublishTargetView) {
  15.     "use strict"
  16.     return Backbone.Marionette.CompositeView.extend({
  17.  
  18.  
  19.       template: Template,
  20.       itemView: Item,
  21.       itemViewContainer: "tbody",
  22.  
  23.  
  24.       filter: {
  25.         from: 0,
  26.         to: 15
  27.       },
  28.  
  29.  
  30.       events: {
  31.         'change .filterBy': 'onClickFilter',
  32.         'change .filterByDate': 'onClickFilterDate'
  33.       },
  34.  
  35.  
  36.       collectionEvents: {
  37.         'sync': 'hideLoading'
  38.       },
  39.  
  40.  
  41.       initialize: function(options) {
  42.  
  43.         //set loading, important we do this because we re-trigger the collection
  44.         this.setLoading();
  45.  
  46.         // don't call a new collection unless its the init load, we lose collection automatically triggered events otherwise
  47.         if (_.isEmpty(options) || !_.has(options, 'newCollection')) {
  48.           // load the postFilter from local storage
  49.           var postFilter = localStorage.getObject('postFilter');
  50.  
  51.           // if the local storage isn't empty, then set it as the current filter
  52.           if (!_.isEmpty(postFilter)) {
  53.             this.filter = postFilter;
  54.           }
  55.  
  56.           // get the collection
  57.           this.collection = new Collection()
  58.         } else {
  59.           // set the filter on memory
  60.           localStorage.setObject('postFilter', this.filter);
  61.         }
  62.  
  63.         //strip any null key values from this.filter so the api doesn't filter crap
  64.         this.filter = _.cleanNullFieldsFromObject(this.filter);
  65.  
  66.         //fetch the collection
  67.         return this.collection.fetch({data: this.filter})
  68.       },
  69.  
  70.  
  71.       // date was triggered, so get the details
  72.       onClickFilterDate: function() {
  73.  
  74.         //fetch the from and to values
  75.         var publishFrom = new Date($('#publish_from_date').val());
  76.         var publishTo = new Date($('#publish_to_date').val());
  77.  
  78.         this.filter.publish_from = $('#publish_from_date').val();
  79.         this.filter.publish_from_date = _.dateToYMD(publishFrom);
  80.         this.filter.publish_from_time = _.dateToHM(publishFrom);
  81.  
  82.         this.filter.publish_to = $('#publish_to_date').val();
  83.         this.filter.publish_to_date = _.dateToYMD(publishTo);
  84.         this.filter.publish_to_time = _.dateToHM(publishTo);
  85.  
  86.         // from time is greater than two time, then fetch the collection
  87.         if ( (publishFrom.getTime() / 1000) < (publishTo.getTime() / 1000) ) {
  88.           this.initialize({newCollection: true});
  89.         }
  90.       },
  91.  
  92.  
  93.       // a typical filter is clicked, so figure out whats happening
  94.       onClickFilter: function (ev) {
  95.  
  96.         // get the data value of the filter being touched
  97.         var type = $('#'+ev.currentTarget.id).data('type')
  98.  
  99.         // switch statement to figure out which filter was used
  100.         switch (type) {
  101.           case 'status':
  102.             this.filter.status = $('#filterStatus').val();
  103.             break;
  104.           case 'publish_target':
  105.             this.filter.publish_target = $('#filterPublishTarget').val();
  106.             break;
  107.           case 'type':
  108.             this.filter.type = $('#filterType').val();
  109.             break;
  110.           case 'client_id':
  111.             this.filter.client_id = $('#filterClientId').val();
  112.             break;
  113.           case 'location_id':
  114.             this.filter.location_id = $('#filterLocationId').val();
  115.             break;
  116.         }
  117.         this.initialize({newCollection: true});
  118.       },
  119.  
  120.  
  121.       // hide the loading state
  122.       hideLoading: function() {
  123.         this.$el.find('.loading-latch').removeClass('loading-active');
  124.       },
  125.  
  126.  
  127.       // set loading by appending to the latch
  128.       setLoading: function() {
  129.         this.$el.find('.loading-latch').addClass('loading-active');
  130.       },
  131.  
  132.  
  133.       onRender: function () {
  134.         this.subViews = {
  135.           status: new StatusView(this.filter).render(),
  136.           type: new TypeView(this.filter).render(),
  137.           publish_target: new PublishTargetView(this.filter).render(),
  138.           location_id: new LocationIdView(this.filter).render(),
  139.           client_id: new ClientIdView(this.filter).render(),
  140.           publish_from_datetime: new PublishFromDateTimeView(this.filter).render(),
  141.           publish_to_datetime: new PublishToDateTimeView(this.filter).render()
  142.         };
  143.  
  144.         this.$el.find('#postsPartialStatus').html(this.subViews.status.el);
  145.         this.$el.find('#postsPartialPublishTarget').html(this.subViews.publish_target.el);
  146.         this.$el.find('#postsPartialType').html(this.subViews.type.el);
  147.         this.$el.find('#postsPartialLocationId').html(this.subViews.location_id.el);
  148.         this.$el.find('#postsPartialClientId').html(this.subViews.client_id.el);
  149.         this.$el.find('#postsPartialPublishFromDateTime').html(this.subViews.publish_from_datetime.el);
  150.         this.$el.find('#postsPartialPublishToDateTime').html(this.subViews.publish_to_datetime.el);
  151.       }
  152.  
  153.  
  154.     })
  155.   })
  156.  
  157. // item view
  158. define(["marionette", "text!app/templates/posts/item.html"],
  159.   function(Marionette, Template) {
  160.   "use strict"
  161.   return Backbone.Marionette.ItemView.extend({
  162.     template: Template,
  163.     tagName: "tr",
  164.  
  165.  
  166.     initialize: function () {
  167.       this.model.set('statusReadable', this.model.getStatus());
  168.     }
  169.  
  170.   })
  171. })
  172. // status filter view
  173. define(["marionette", "text!app/templates/posts/partials/status.html", 'app/models/post'],
  174.   function(Marionette, Template, Model) {
  175.     "use strict"
  176.     return Backbone.Marionette.ItemView.extend({
  177.       template: Template,
  178.  
  179.  
  180.       initialize: function(value) {
  181.         this.values = new Model().getStatuses();
  182.         this.value = value.status;
  183.       },
  184.  
  185.  
  186.       serializeData: function() {
  187.         return {values: this.values, value: this.value};
  188.       }
  189.  
  190.  
  191.     })
  192.   })
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement