-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.coffee
76 lines (65 loc) · 1.6 KB
/
app.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
App = Ember.Application.create()
Ember.Fetchable = Ember.Mixin.create
fetchItems: ((max_id) ->
_this = @
return $.Deferred((defer) ->
return $.ajax(
url: _this.get('url'),
dataType: 'json',
data:
after: max_id
).done((res) ->
return defer.resolve(res.results)
)
).promise()
),
fetch: ((callback) ->
_this = @
@fetchItems(@get('maxId')).done((tweets) ->
items = tweets.map((v) ->
v['recent'] = true
Ember.Object.create(v)
).reverse()
_this.get('content').unshiftObjects(items)
if callback
callback()
)
)
# Controllers
App.tweets = Ember.ArrayController.create Ember.Fetchable,
content: [],
maxId: 3, # adhoc
url: (->
'./item.json?after=' + @get('maxId')
).property('maxId'),
recentCount: (->
@get('content').filterProperty('recent', true).length
).property('content.@each.recent'),
expandRecent: (->
@get('content').forEach((v) ->
v.set('recent', false)
)
)
# Views
App.ApplicationView = Ember.View.extend()
App.TwitterListView = Ember.View.extend
templateName: 'twitter_list',
contentBinding: 'App.tweets.content',
recentCountBinding: 'App.tweets.recentCount',
expandRecent: (->
App.tweets.expandRecent()
),
hasRecentItems: (->
@get('recentCount') > 0
).property('recentCount'),
items: (->
@get('content').filterProperty('recent', false)
).property('content.@each.recent')
# Setup
setInterval((->
if App.tweets.get('recentCount') < 10
App.tweets.fetch()
), 5000)
App.tweets.fetch(->
App.tweets.expandRecent()
)