Advertisement
Guest User

Untitled

a guest
Mar 27th, 2013
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Sites module
  2. define(function (require) {
  3.   "use strict";
  4.  
  5.   /*  DEPS */
  6.  
  7.   // Create a new module.
  8.   var app = require('app'),
  9.     Sites = app.module();
  10.  
  11.   var PageableCollection = require("vendor/backbone-pageable/backbone-pageable");
  12.  
  13.   /*  END DEPS */
  14.  
  15.   /*  MODELS & COLLECTIONS */
  16.  
  17.   // Default Model
  18.   Sites.Models.Site = Backbone.Model.extend({
  19.     defaults: {
  20.       title: ''
  21.     }
  22.   });
  23.  
  24.   // Default Collection
  25.   Sites.Collections.Sites = PageableCollection.extend({
  26.     url: 'http://tadjik1.apiary.io/api/sites',
  27.     model: Sites.Models.Site,
  28.  
  29.     mode: 'client',
  30.  
  31.     parse: function (resp) {
  32.       return resp.sites || resp;
  33.     },
  34.  
  35.     state: {
  36.       pageSize: 5
  37.     },
  38.  
  39.     search: function (val) {
  40.       this.origModels = this.origModels || this.fullCollection.models;
  41.       if (val === "") return this.origModels;
  42.  
  43.       var pattern = new RegExp(val.toLowerCase(), "gi");
  44.       return _.filter(this.origModels, function (model) {
  45.         return pattern.test(model.get("title").toLowerCase());
  46.       });
  47.     },
  48.  
  49.     setActive: function (model) {
  50.       this.trigger('select:model', model);
  51.     },
  52.  
  53.     unselect: function () {
  54.       this.trigger('unselect:model');
  55.     }
  56.  
  57.   });
  58.  
  59.   /* END MODELS & COLLECTIONS */
  60.  
  61.   /*  VIEWS */
  62.  
  63.   Sites.Views.Layout = Backbone.Layout.extend({
  64.     template: "sites/layout",
  65.     className: 'container-fluid',
  66.  
  67.     initialize: function (options) {
  68.       _.bindAll(this);
  69.  
  70.       this.siteId = options.siteId;
  71.       this.collection = new Sites.Collections.Sites();
  72.       this.collection.on('select:model', this.renderSite);
  73.       this.collection.on('unselect:model', this.renderSiteList);
  74.  
  75.       if (this.siteId) {
  76.         var model = new Sites.Models.Site({
  77.           id: this.siteId
  78.         });
  79.         this.renderSite(model);
  80.       } else {
  81.         this.renderSiteList();
  82.       }
  83.     },
  84.  
  85.     beforeRender: function () {
  86.       var actions = new Sites.Views.Actions({
  87.         collection: this.collection
  88.       });
  89.       this.setView('.actions', actions);
  90.     },
  91.  
  92.     renderSite: function (model) {
  93.       var siteView = new Sites.Views.SiteView({
  94.         model: model,
  95.         collection: this.collection
  96.       });
  97.       this.setView('.content', siteView);
  98.  
  99.       siteView.render();
  100.     },
  101.  
  102.     renderSiteList: function () {
  103.       var siteListView = new Sites.Views.SiteList({
  104.         collection: this.collection
  105.       });
  106.       this.setView('.content', siteListView);
  107.  
  108.       if (this.collection.length) {
  109.         siteListView.render();
  110.       } else {
  111.         this.collection.fetch({
  112.           success: siteListView.render
  113.         });
  114.       }
  115.     }
  116.   });
  117.  
  118.   Sites.Views.SiteView = Backbone.View.extend({
  119.     template: 'sites/site',
  120.  
  121.     initialize: function () {
  122.       _.bindAll(this);
  123.  
  124.       if (!this.model.isNew() && !this.model.get('title')) {
  125.         this.model.fetch({
  126.           success: this.render
  127.         });
  128.       } else {
  129.         this.render();
  130.       }
  131.     },
  132.  
  133.     serialize: function () {
  134.       return {
  135.         title: this.model.get('title')
  136.       }
  137.     }
  138.   });
  139.  
  140.   Sites.Views.SiteList = Backbone.View.extend({
  141.     template: 'sites/list',
  142.  
  143.     initialize: function () {
  144.       _.bindAll(this);
  145.       this.listenTo(this.collection, 'reset', this.render);
  146.     },
  147.  
  148.     beforeRender: function () {
  149.       this.collection.each(function (model) {
  150.         var view = new Sites.Views.SiteItem({
  151.           model: model,
  152.           collection: this.collection
  153.         });
  154.         this.insertView('tbody', view);
  155.       }, this);
  156.     }
  157.   });
  158.  
  159.   Sites.Views.SiteItem = Backbone.View.extend({
  160.     template: 'sites/item',
  161.     tagName: 'tr',
  162.  
  163.     events: {
  164.       'click': 'setActive'
  165.     },
  166.  
  167.     initialize: function () {
  168.       _.bindAll(this);
  169.     },
  170.  
  171.     serialize: function () {
  172.       return {
  173.         title: this.model.get('title')
  174.       };
  175.     },
  176.  
  177.     setActive: function () {
  178.       this.collection.setActive(this.model);
  179.     }
  180.   });
  181.  
  182.   Sites.Views.Actions = Backbone.View.extend({
  183.     template: 'sites/actions',
  184.     className: 'container-fluid well',
  185.  
  186.     events: {
  187.       'keyup input[name="filteredByTitle"]': 'filteredByTitle',
  188.       'click .backToList': 'backToList',
  189.       'click .createsite': 'createsite'
  190.     },
  191.  
  192.     initialize: function () {
  193.       _.bindAll(this);
  194.       this.listenTo(this.collection, 'select:model', this.oneElementView);
  195.     },
  196.  
  197.     beforeRender: function () {
  198.       var pagination = new Sites.Views.Pagination({
  199.         collection: this.collection
  200.       });
  201.       this.setView('.pagination', pagination);
  202.     },
  203.  
  204.     oneElementView: function () {
  205.       this.$('.pagination, .filterName').hide();
  206.       this.$('.backbutton').show();
  207.     },
  208.  
  209.     backToList: function () {
  210.       this.collection.unselect();
  211.       this.$('.pagination, .filterName').show();
  212.       this.$('.backbutton').hide();
  213.     },
  214.  
  215.     createsite: function () {
  216.       var model = new this.collection.model();
  217.       this.collection.add(model);
  218.       this.collection.setActive(model);
  219.     },
  220.  
  221.     filteredByTitle: function (e) {
  222.       var models = this.collection.search($(e.target).val());
  223.       if (models.length === 0) {
  224.         this.collection.state.currentPage = null;
  225.       } else {
  226.         this.collection.state.currentPage = 1;
  227.       }
  228.       this.collection.fullCollection.reset(models);
  229.     }
  230.   });
  231.  
  232.   Sites.Views.Pagination = Backbone.View.extend({
  233.     template: 'sites/pagination',
  234.  
  235.     events: {
  236.       'click .page': 'goToPage',
  237.       'click .next': 'goToNext',
  238.       'click .prev': 'goToPrev'
  239.     },
  240.  
  241.     initialize: function () {
  242.       _.bindAll(this);
  243.       this.listenTo(this.collection, 'reset', this.render);
  244.     },
  245.  
  246.     serialize: function () {
  247.       return this.collection.state;
  248.     },
  249.  
  250.     goToPage: function (e) {
  251.       if (!$(e.target).parent().hasClass('disabled')) {
  252.         this.collection.getPage($(e.target).attr('name'));
  253.       }
  254.     },
  255.  
  256.     goToNext: function (e) {
  257.       if (!$(e.target).parent().hasClass('disabled')) {
  258.         this.collection.getNextPage();
  259.       }
  260.     },
  261.  
  262.     goToPrev: function (e) {
  263.       if (!$(e.target).parent().hasClass('disabled')) {
  264.         this.collection.getPreviousPage();
  265.       }
  266.     }
  267.   });
  268.  
  269.  
  270.   /* END VIEWS */
  271.  
  272.   // Return the module for AMD compliance.
  273.   return Sites;
  274. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement