Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 210 lines (177 sloc) 6.871 kb
9a4e6a25 » Brandon Tilley
2011-04-17 Backbone.js rewrite
1 # Function to see if a string starts with another string.
2 String::startsWith = (other, case_cmp = false) ->
3 first = this
4 second = other || ""
5
6 unless case_cmp
7 first = first.toUpperCase()
8 second = second.toUpperCase()
9
10 first.indexOf(second) == 0
11
12 # Text helpers to format strings the way we want them.
13 window.TextHelpers =
14 type: (type) ->
15 switch type
16 when "pve" then "PvE"
17 when "pvp" then "PvP"
18 when "rp" then "RP"
19 when "rppvp" then "RP PvP"
20 else type
21
22 population: (pop) ->
23 switch pop
24 when "low" then "Low"
25 when "medium" then "Medium"
26 when "high" then "High"
27 else pop
28
29 status: (status) ->
30 if status then "Up" else "Down"
31
32 queue: (queue) ->
33 if queue then "Yes" else "No"
34
66c15413 » Brandon Tilley
2011-04-16 Convert main app logic to coffeescript and backbone
35 $ ->
9a4e6a25 » Brandon Tilley
2011-04-17 Backbone.js rewrite
36 # Use {{ Mustache-style }} templates
66c15413 » Brandon Tilley
2011-04-16 Convert main app logic to coffeescript and backbone
37 _.templateSettings = {
38 interpolate: /\{\{(.+?)\}\}/g
39 };
40
9a4e6a25 » Brandon Tilley
2011-04-17 Backbone.js rewrite
41 # Router
42 # ============================================================================
43 window.Controller = Backbone.Controller.extend
44 routes:
45 ":realm": "realm"
46
47 # Match any string in the hash tag to be the name of a realm to search for.
48 realm: (realm)->
49 Realms.filter(realm)
50
66c15413 » Brandon Tilley
2011-04-16 Convert main app logic to coffeescript and backbone
51 # Model for an individual realm
9a4e6a25 » Brandon Tilley
2011-04-17 Backbone.js rewrite
52 # ============================================================================
66c15413 » Brandon Tilley
2011-04-16 Convert main app logic to coffeescript and backbone
53 window.Realm = Backbone.Model.extend
54 initialize: ->
9a4e6a25 » Brandon Tilley
2011-04-17 Backbone.js rewrite
55 # Only set default data if it doesn't already exist.
56 unless this.get("name")?
66c15413 » Brandon Tilley
2011-04-16 Convert main app logic to coffeescript and backbone
57 this.set
58 name: "unknown"
59 slug: "unknown"
60 type: "n/a"
61 population: "n/a"
62 queue: false
63 status: false
64
65 # Model for a list of realms
9a4e6a25 » Brandon Tilley
2011-04-17 Backbone.js rewrite
66 # ============================================================================
66c15413 » Brandon Tilley
2011-04-16 Convert main app logic to coffeescript and backbone
67 window.RealmList = Backbone.Collection.extend
68 model: Realm
9a4e6a25 » Brandon Tilley
2011-04-17 Backbone.js rewrite
69 url: "http://us.battle.net/api/wow/realm/status?jsonp=?"
70
66c15413 » Brandon Tilley
2011-04-16 Convert main app logic to coffeescript and backbone
71 initialize: ->
9a4e6a25 » Brandon Tilley
2011-04-17 Backbone.js rewrite
72 _.bindAll this, 'update', 'parse', 'processUpdate', 'filter'
73 # After the first refresh, we no longer want to replace the models
74 # in the collection with new ones. Thus, we will use our own 'refresh'
75 # method which simplu updates all the models with the new data.
76 this.bind 'refresh', ->
77 this.refresh = this.processUpdate
78
79 # A wrapper around fetch() to (1) fire a "loading" event and
80 # (2) ensure that a timer is set so another update happens in the future.
66c15413 » Brandon Tilley
2011-04-16 Convert main app logic to coffeescript and backbone
81 update: ->
9a4e6a25 » Brandon Tilley
2011-04-17 Backbone.js rewrite
82 this.trigger 'refresh:start' # Indicates data is being collected.
66c15413 » Brandon Tilley
2011-04-16 Convert main app logic to coffeescript and backbone
83 this.fetch()
9a4e6a25 » Brandon Tilley
2011-04-17 Backbone.js rewrite
84 window.setTimeout this.update, 1000 * 60 * 5 # update every 5 minutes
85
86 # Custom parse method (1) extracts the array from Blizzard's JSON API and
87 # (2) adds an "id" attribute to every model equal to that Realm's slug.
66c15413 » Brandon Tilley
2011-04-16 Convert main app logic to coffeescript and backbone
88 parse: (response) ->
9a4e6a25 » Brandon Tilley
2011-04-17 Backbone.js rewrite
89 _.each response.realms, (realm) ->
90 realm.id = realm.slug
91 this.trigger 'refresh:end' # Indicates data collection has ended.
66c15413 » Brandon Tilley
2011-04-16 Convert main app logic to coffeescript and backbone
92 response.realms
93
9a4e6a25 » Brandon Tilley
2011-04-17 Backbone.js rewrite
94 # Our replacement 'refresh' method, for every refresh after the first.
95 # Iterates over the models in the collection and updates each one's data.
96 processUpdate: (models, options) ->
97 list = this
98 _.each models, (model) ->
99 id = model.id
100 original_model = list.get(id)
101 original_model.set(model)
102
103 # Called by our controller when the hash tag changes.
104 # We simply fire an event indicating the term has changed, and any view
105 # that is interested will handle the work of hiding or showing elements.
106 filter: (term) ->
107 this.trigger 'filter:change', term
66c15413 » Brandon Tilley
2011-04-16 Convert main app logic to coffeescript and backbone
108
109 # View for an individual realm
9a4e6a25 » Brandon Tilley
2011-04-17 Backbone.js rewrite
110 # ============================================================================
66c15413 » Brandon Tilley
2011-04-16 Convert main app logic to coffeescript and backbone
111 window.RealmView = Backbone.View.extend
9a4e6a25 » Brandon Tilley
2011-04-17 Backbone.js rewrite
112 tagName: "div"
66c15413 » Brandon Tilley
2011-04-16 Convert main app logic to coffeescript and backbone
113 template: _.template $("#realm_template").html()
9a4e6a25 » Brandon Tilley
2011-04-17 Backbone.js rewrite
114
66c15413 » Brandon Tilley
2011-04-16 Convert main app logic to coffeescript and backbone
115 initialize: ->
9a4e6a25 » Brandon Tilley
2011-04-17 Backbone.js rewrite
116 _.bindAll this, 'render', 'show', 'hide'
117 # Whenever the data changes, the view with automatically re-render itself.
66c15413 » Brandon Tilley
2011-04-16 Convert main app logic to coffeescript and backbone
118 this.model.bind 'change', this.render
119 this.model.view = this
9a4e6a25 » Brandon Tilley
2011-04-17 Backbone.js rewrite
120
121 # Render a single realm based on the template embedded in the HTML.
66c15413 » Brandon Tilley
2011-04-16 Convert main app logic to coffeescript and backbone
122 render: ->
123 $(this.el).html(this.template(this.model.toJSON()))
124 this
9a4e6a25 » Brandon Tilley
2011-04-17 Backbone.js rewrite
125
126 show: ->
127 $(this.el).show()
128
129 hide: ->
130 $(this.el).hide()
66c15413 » Brandon Tilley
2011-04-16 Convert main app logic to coffeescript and backbone
131
132 # View for the application
9a4e6a25 » Brandon Tilley
2011-04-17 Backbone.js rewrite
133 # ============================================================================
66c15413 » Brandon Tilley
2011-04-16 Convert main app logic to coffeescript and backbone
134 window.AppView = Backbone.View.extend
135 el: $("#main")
9a4e6a25 » Brandon Tilley
2011-04-17 Backbone.js rewrite
136 events:
137 "keyup input": "search"
138
66c15413 » Brandon Tilley
2011-04-16 Convert main app logic to coffeescript and backbone
139 initialize: ->
9a4e6a25 » Brandon Tilley
2011-04-17 Backbone.js rewrite
140 _.bindAll this, 'addOne', 'addAll', 'search', 'initSearch', 'filter', 'startLoading', 'stopLoading', 'updateTime'
141 Realms.bind 'refresh', this.addAll
142 Realms.bind 'refresh:start', this.startLoading
143 Realms.bind 'refresh:end', this.stopLoading
144 Realms.bind 'refresh:end', this.updateTime
145 Realms.bind 'filter:change', this.filter
66c15413 » Brandon Tilley
2011-04-16 Convert main app logic to coffeescript and backbone
146 Realms.update()
9a4e6a25 » Brandon Tilley
2011-04-17 Backbone.js rewrite
147 this.initSearch()
148
149 # Add a single realm to the page.
66c15413 » Brandon Tilley
2011-04-16 Convert main app logic to coffeescript and backbone
150 addOne: (realm) ->
151 view = new RealmView(model: realm)
152 this.el.append view.render().el
9a4e6a25 » Brandon Tilley
2011-04-17 Backbone.js rewrite
153
154 # Add an entire list of realms to the page.
155 # Deletages to this.addOne.
66c15413 » Brandon Tilley
2011-04-16 Convert main app logic to coffeescript and backbone
156 addAll: ->
157 view = this
9a4e6a25 » Brandon Tilley
2011-04-17 Backbone.js rewrite
158 _.each Realms.models, (realm) ->
159 unless realm.view?
160 view.addOne realm
161 # Now that the initial data is shown, start the controller's routing.
162 Backbone.history.start()
163
164 # Called when the "keyup" event is fired from the input box.
165 # Sets the hash tag, which the controller picks up on and fires
166 # events to ask the app to filter based on the input.
167 search: ->
168 window.location.hash = this.$("input").val()
169
170 # Set the initial value of the search box to be whatever is in the URL.
171 initSearch: ->
172 this.$("input").val(window.location.hash.substring(1))
173 this.$("input").focus()
174
175 # Called when the realm list's filter:change event is triggered.
176 # Iterates over the realms to see if each matches the search value.
177 filter: (term) ->
d05a70e3 » Brandon Tilley
2011-04-17 Search UI improvements
178 this.$("input").val(term)
179 if term == ""
180 this.$("#reset").hide()
181 else
182 this.$("#reset").show()
9a4e6a25 » Brandon Tilley
2011-04-17 Backbone.js rewrite
183 Realms.each (realm) ->
184 if realm.get('name').startsWith(term)
185 realm.view.show()
186 else
187 realm.view.hide()
188
189 startLoading: ->
190 this.$("#loading img").show()
191
192 stopLoading: ->
193 this.$("#loading img").hide()
194
195 updateTime: ->
66c15413 » Brandon Tilley
2011-04-16 Convert main app logic to coffeescript and backbone
196 now = new Date
197 hours = now.getHours()
198 minutes = now.getMinutes()
199 seconds = now.getSeconds()
200 meridian = if hours < 12 then "AM" else "PM"
201
202 hours -= 12 if hours > 12
203 hours = 12 if hours == 0
204 minutes = "0#{minutes}" if minutes < 10
205 seconds = "0#{seconds}" if seconds < 10
206 this.$("#time span").text("#{hours}:#{minutes}:#{seconds} #{meridian}")
207
9a4e6a25 » Brandon Tilley
2011-04-17 Backbone.js rewrite
208 window.Router = new Controller
209 window.Realms = new RealmList
210 window.App = new AppView
Something went wrong with that request. Please try again.