├── README.md ├── LICENSE └── backbone.baseview.js /README.md: -------------------------------------------------------------------------------- 1 | backbone.baseview 2 | ================= 3 | 4 | A simple base view class for Backbone.View, -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | ©2012 Airbnb, Inc. 2 | 3 | Redistribution and use in source and binary forms, with or without 4 | modification, are permitted provided that the following conditions are met: 5 | 6 | Redistributions of source code must retain the above copyright notice, 7 | this list of conditions and the following disclaimer. 8 | 9 | Redistributions in binary form must reproduce the above copyright notice, 10 | this list of conditions and the following disclaimer in the documentation 11 | and/or other materials provided with the distribution. 12 | 13 | Names of the contributors to this software may not be used to endorse or 14 | promote products derived from this software without specific prior written 15 | permission. 16 | 17 | This software is provided by the contributors "as is" and any express or 18 | implied warranties, including, but not limited to, the implied warranties 19 | of merchantability and fitness for a particular purpose are disclaimed. In 20 | no event shall the contributors be liable for any direct, indirect, 21 | incidental, special, exemplary, or consequential damages (including, but not 22 | limited to, procurement of substitute goods or services; loss of use, data, 23 | or profits; or business interruption) however caused and on any theory of 24 | liability, whether in contract, strict liability, or tort (including 25 | negligence or otherwise) arising in any way out of the use of this 26 | software, even if advised of the possibility of such damage. 27 | 28 | This license applies exclusively to the backbone.baseview.js file and the corresponding 29 | files built with the "make build" command. 30 | -------------------------------------------------------------------------------- /backbone.baseview.js: -------------------------------------------------------------------------------- 1 | Backbone.BaseView = Backbone.View.extend({ 2 | template: null, 3 | 4 | initialize: function (options) { 5 | this.bindings(); 6 | this._postInitialize(); 7 | }, 8 | 9 | _postInitialize: function() { 10 | this.postInitialize(); 11 | this.trigger('initialize'); 12 | }, 13 | 14 | getRenderData: function() { 15 | if (this.model) { 16 | return this.model.toJSON(); 17 | } else { 18 | return {}; 19 | } 20 | }, 21 | 22 | getTemplate: function() { 23 | if (this.template && typeof JST !== 'undefined') { 24 | return JST[this.template]; 25 | } 26 | }, 27 | 28 | getHtml: function() { 29 | var template = this.getTemplate(); 30 | if (template) { 31 | return template(this.getRenderData()); 32 | } else { 33 | return ""; 34 | } 35 | }, 36 | 37 | render: function() { 38 | this.$el.html(this.getHtml()); 39 | this.trigger('render'); 40 | this.postRender(); 41 | return this; 42 | }, 43 | 44 | // NOOP will be overriden 45 | postInitialize: function() {}, 46 | 47 | // NOOP will be overriden 48 | postRender: function() {}, 49 | 50 | // Your bindings 51 | // NOOP will be overriden 52 | bindings: function() {}, 53 | 54 | // To be called before view is thrown away. Clean up intervals, events, etc. 55 | // NOOP will be overriden 56 | cleanup: function() { 57 | this.dispose(); 58 | this.remove(); 59 | }, 60 | 61 | // Stole this method from Backbone v0.9.2 bleeding edge. 62 | // https://github.com/documentcloud/backbone/commit/3ae1af6df1b542bfb3e38f2fdfe7a471f2b830a0 63 | // 64 | // Clean up references to this view in order to prevent latent effects and 65 | // memory leaks. 66 | dispose: function() { 67 | this.undelegateEvents(); 68 | if (this.model) this.model.off(null, null, this); 69 | if (this.collection) this.collection.off(null, null, this); 70 | return this; 71 | }, 72 | 73 | // A way to grab elements from the DOM using data-attributes. 74 | // Caches els by default. Pass `true` for `fresh` to ignore cache. 75 | $get: function(key, fresh) { 76 | this._$getEls = this._$getEls || {}; 77 | if (fresh || !this._$getEls[key]) { 78 | this._$getEls[key] = this.$('[data-'+key+']'); 79 | } 80 | return this._$getEls[key]; 81 | } 82 | 83 | }); 84 | 85 | --------------------------------------------------------------------------------