-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackbone.renderer.coffee
More file actions
110 lines (73 loc) · 2.77 KB
/
Copy pathbackbone.renderer.coffee
File metadata and controls
110 lines (73 loc) · 2.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
if require?
Backbone = require "backbone"
_ = require "underscore"
else
Backbone = window.Backbone
_ = window._
class Backbone.View extends Backbone.View
_.extend @prototype, Backbone.Ancestry
constructor: (options) ->
# Retain model if we have one.
options.model.retain this if options?.model?.retain?
# Initialize empty insertedElements
@insertedElements = {}
super
# Insert an element inside a template. Retain that element in @insertedElements
# and return a <tag> HTML text element, to be replaced after rendering. tag is
# used to make sure the returned text element can be apenned, e.g. tag should
# be "option" if element is to be inserted within a <select> tag..
insertElement: (tag, element) ->
index = _.uniqueId("inserted_view_")
@insertedElements[index] = element
"<#{tag} data-inserted-view='#{index}'></#{tag}>"
insertEachElement: (elements) ->
_.map(elements, ([tag, element]) =>
@insertElement tag, element).join "\n"
insertView: (view) ->
@insertElement view.tagName, view.el
insertEachView: (views) ->
@insertEachElement _.map(views, (view) ->
[view.tagName, view.el])
# Remove this view. Unbinds all incoming and outgoing events,
# removes all children, releases @model if it exists, and removes
# this view from its parent if it has one. Triggers the `removing`
# event, returns `this`.
remove: ->
@trigger "removing"
# Release our model if we had one.
@model.release this if @model?.release?
# Remove from the DOM.
super
# Kill all bindings for stuff that bound to us.
@off()
# Tell all our children to remove themselves.
@eachChild (c) -> c.remove()
# Remove ourselves from our parent, if we had one.
@getParent().removeChild this if @hasParent()
this
render: ->
@trigger "rendering"
# Detach all inserted views
_.each @insertedElements, (el) -> $(el).detach()
# Reset inserted index and views
@insertedElements = {}
# Compile template.
@$el.html @renderer()
@delegateEvents()
@trigger "rendered"
# Reattach inserted elements. We re-attach _after_ triggering
# "rendered" to make sure that all handlers on this event
# do not mess with elements added now. Typical example:
# @on "rendered" ->
# Backbone.ModelBinding.bind this
# If elements are added before this, then Backbone.ModelBinding
# will also change their values..
# Same goes for @delegateEvents...
_.each @insertedElements, (el, index) =>
@$("[data-inserted-view='#{index}']").replaceWith el
# Now we trigger "populated" if y'all need to do something when
# sub-views have been attached.
@trigger "populated"
this
renderer: ->
"MISSING IMPLEMENTATION"