JSFiddle - React, Tailwind, and code Playground
by southerd
JavaScript
1 define ->
2 "use strict"
3
4 templates = {}
5
6 Backbone.View.extend
7 template: '<div></div>',
8 _templatize: ->
9 if not templates[ @template ]
10 templates[ @template ] = _.template( @template )
11
12 templates[ @template ]
13 prerender: ->
14 render: ->
15 @prerender()
16 @$el.html( @_templatize()( @serialize() ) )
17 @_setupElements()
18 @postrender()
19 @
20 postrender: ->
21
22 serialize: ->
23 toSerialize = ( @collection || @model || {} )
24 return if toSerialize.toJSON then toSerialize.toJSON() else toSerialize
25
26 elements: [],
27 _setupElements: ->
28 if ( [email protected] )
29 return @
30
31 @elements.forEach ( el ) =>
32 @[ '$' + el ] = @$el.find('[data-el=' + el + ']')
33 @
34
35 predestroy: ->
36 postdestroy: ->
37 destroy: ->
38 @predestroy()
39 @remove()
40 @postdestroy()
41 @