forked from bnolan/Backbone-Mobile
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapplication.coffee
252 lines (182 loc) · 5.58 KB
/
application.coffee
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
#
# Some helper methods
#
app =
activePage: ->
$(".ui-page-active")
reapplyStyles: (el) ->
el.find('ul[data-role]').listview();
el.find('div[data-role="fieldcontain"]').fieldcontain();
el.find('button[data-role="button"]').button();
el.find('input,textarea').textinput();
el.page()
redirectTo: (page) ->
$.mobile.changePage page
goBack: ->
$.historyBack()
#
# Venue class
#
class Venue extends Backbone.Model
getName: ->
@get('name')
getAddress: ->
[@get('address'), @get('city'), @get('state')].join ", "
getImageUrl: ->
@get('photo_url')
getLatitude: ->
@get('geolat')
getLongitude: ->
@get('geolong')
getMapUrl: (width, height) ->
width ||= 300
height ||= 220
"http://maps.google.com/maps/api/staticmap?center=#{@getLatitude()},#{@getLongitude()}&zoom=14&size=#{width}x#{height}&maptype=terrain&markers=color:red|#{@getLatitude()},#{@getLongitude()}&sensor=false"
#
# Venue Collection
#
class VenueCollection extends Backbone.Collection
model : Venue
constructor: ->
super
@refresh($FOURSQUARE_JSON)
this.Venues = new VenueCollection
#
# Edit Venue View
#
class EditVenueView extends Backbone.View
constructor: ->
super
# Get the active page from jquery mobile. We need to keep track of what this
# dom element is so that we can refresh the page when the page is no longer active.
@el = app.activePage()
@template = _.template('''
<form action="#venue-<%= venue.cid %>-update" method="post">
<div data-role="fieldcontain">
<label>Name</label>
<input type="text" value="<%= venue.getName() %>" name="name" />
</div>
<div data-role="fieldcontain">
<label>Address</label>
<input type="text" value="<%= venue.get('address') %>" name="address" />
</div>
<div data-role="fieldcontain">
<label>City</label>
<input type="text" value="<%= venue.get('city') %>" name="city" />
</div>
<div data-role="fieldcontain">
<label>State</label>
<input type="text" value="<%= venue.get('state') %>" name="state" />
</div>
<button type="submit" data-role="button">Save</button>
</form>
''')
# Watch for changes to the model and redraw the view
@model.bind 'change', @render
# Draw the view
@render()
events : {
"submit form" : "onSubmit"
}
onSubmit: (e) ->
@model.set {
name : @$("input[name='name']").val(),
address : @$("input[name='address']").val(),
city : @$("input[name='city']").val(),
state : @$("input[name='state']").val()
}
@model.trigger('change')
app.goBack()
e.preventDefault()
e.stopPropagation()
render: =>
# Set the name of the page
@el.find('h1').text("Editing #{@model.getName()}")
# Render the content
@el.find('.ui-content').html(@template({venue : @model}))
# A hacky way of reapplying the jquery mobile styles
app.reapplyStyles(@el)
# Delegate from the events hash
@delegateEvents()
#
# Show Venue View
#
class ShowVenueView extends Backbone.View
constructor: ->
super
# Get the active page from jquery mobile. We need to keep track of what this
# dom element is so that we can refresh the page when the page is no longer active.
@el = app.activePage()
@template = _.template('''
<div>
<p>
<img style="width: 100%" src="<%= venue.getMapUrl() %>" />
</p>
<address>
<%= venue.getAddress() %>
</address>
<ul data-role="listview" data-inset="true">
<li data-role="list-divider">Actions</li>
<li><a rel="external" href="openmap:q=<%= encodeURIComponent(venue.getAddress) %>">Open Map</li>
<li><a href="#venues-<%= venue.cid %>-edit">Edit</a></li>
</ul>
</div>
''')
# Watch for changes to the model and redraw the view
@model.bind 'change', @render
# Draw the view
@render()
render: =>
# Set the name of the page
@el.find('h1').text(@model.getName())
# Render the content
@el.find('.ui-content').html(@template({venue : @model}))
# A hacky way of reapplying the jquery mobile styles
app.reapplyStyles(@el)
#
# Home View
#
class HomeView extends Backbone.View
constructor: ->
super
@el = app.activePage()
@template = _.template('''
<div>
<ul data-role="listview" data-theme="c" data-filter="true">
<% venues.each(function(venue){ %>
<li><a href="#venues-<%= venue.cid %>"><%= venue.getName() %></a></li>
<% }); %>
</ul>
</div>
''')
@render()
render: =>
# Render the content
@el.find('.ui-content').html(@template({venues : Venues}))
# A hacky way of reapplying the jquery mobile styles
app.reapplyStyles(@el)
#
# Our only controller
#
class HomeController extends Backbone.Controller
routes :
"venues-:cid-edit" : "edit"
"venues-:cid" : "show"
"home" : "home"
constructor: ->
super
@_views = {}
home : ->
@_views['home'] ||= new HomeView
show: (cid) ->
@_views["venues-#{cid}"] ||= new ShowVenueView { model : Venues.getByCid(cid) }
edit: (cid) ->
@_views["venues-#{cid}-edit"] ||= new EditVenueView { model : Venues.getByCid(cid) }
app.homeController = new HomeController()
#
# Start the app
#
$(document).ready ->
Backbone.history.start()
app.homeController.home()
@app = app