Commit 32c7310f4a2ba8b408f0dd0cc17eba9b92159817

Authored by Dmitriy Zaporozhets
1 parent 5b1984ce

Clear refresh interval for notes. Fixes duplicate note rendering

Signed-off-by: Dmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com>
Showing 1 changed file with 19 additions and 9 deletions   Show diff stats
app/assets/javascripts/notes.js.coffee
1 1 class Notes
  2 + @interval: null
  3 +
2 4 constructor: (notes_url, note_ids) ->
3 5 @notes_url = notes_url
4 6 @notes_url = gon.relative_url_root + @notes_url if gon.relative_url_root?
... ... @@ -60,7 +62,8 @@ class Notes
60 62  
61 63  
62 64 initRefresh: ->
63   - setInterval =>
  65 + clearInterval(Notes.interval)
  66 + Notes.interval = setInterval =>
64 67 @refresh()
65 68 , 15000
66 69  
... ... @@ -74,11 +77,7 @@ class Notes
74 77 success: (data) =>
75 78 notes = data.notes
76 79 $.each notes, (i, note) =>
77   - # render note if it not present in loaded list
78   - # or skip if rendered
79   - if $.inArray(note.id, @note_ids) == -1
80   - @note_ids.push(note.id)
81   - @renderNote(note)
  80 + @renderNote(note)
82 81  
83 82  
84 83 ###
... ... @@ -87,7 +86,19 @@ class Notes
87 86 Note: for rendering inline notes use renderDiscussionNote
88 87 ###
89 88 renderNote: (note) ->
90   - $('ul.main-notes-list').append(note.html)
  89 + # render note if it not present in loaded list
  90 + # or skip if rendered
  91 + if @isNewNote(note)
  92 + @note_ids.push(note.id)
  93 + $('ul.main-notes-list').append(note.html)
  94 +
  95 +
  96 + ###
  97 + Check if note does not exists on page
  98 + ###
  99 + isNewNote: (note) ->
  100 + $.inArray(note.id, @note_ids) == -1
  101 +
91 102  
92 103 ###
93 104 Render note in discussion area.
... ... @@ -95,6 +106,7 @@ class Notes
95 106 Note: for rendering inline notes use renderDiscussionNote
96 107 ###
97 108 renderDiscussionNote: (note) ->
  109 + @note_ids.push(note.id)
98 110 form = $("form[rel='" + note.discussion_id + "']")
99 111 row = form.closest("tr")
100 112  
... ... @@ -219,7 +231,6 @@ class Notes
219 231 Adds new note to list.
220 232 ###
221 233 addNote: (xhr, note, status) =>
222   - @note_ids.push(note.id)
223 234 @renderNote(note)
224 235  
225 236 ###
... ... @@ -228,7 +239,6 @@ class Notes
228 239 Adds new note to list.
229 240 ###
230 241 addDiscussionNote: (xhr, note, status) =>
231   - @note_ids.push(note.id)
232 242 @renderDiscussionNote(note)
233 243  
234 244 ###
... ...