Commit bd60a4ed40ca52fd23e027de8f30e2f094eb6e5c

Authored by Riyad Preukschas
1 parent ae067ee3

Make notes JS know which notes are new in a request

app/assets/javascripts/extensions/array.js 0 → 100644
@@ -0,0 +1,7 @@ @@ -0,0 +1,7 @@
  1 +Array.prototype.first = function() {
  2 + return this[0];
  3 +}
  4 +
  5 +Array.prototype.last = function() {
  6 + return this[this.length-1];
  7 +}
0 \ No newline at end of file 8 \ No newline at end of file
app/assets/javascripts/notes.js
@@ -89,12 +89,12 @@ var NoteList = { @@ -89,12 +89,12 @@ var NoteList = {
89 getContent: 89 getContent:
90 function() { 90 function() {
91 $.ajax({ 91 $.ajax({
92 - type: "GET",  
93 - url: this.notes_path,  
94 - data: this.target_params,  
95 - complete: function(){ $('.notes-status').removeClass("loading")},  
96 - beforeSend: function() { $('.notes-status').addClass("loading") },  
97 - dataType: "script"}); 92 + url: this.notes_path,
  93 + data: this.target_params,
  94 + complete: function(){ $('.notes-status').removeClass("loading")},
  95 + beforeSend: function() { $('.notes-status').addClass("loading") },
  96 + dataType: "script"
  97 + });
98 }, 98 },
99 99
100 /** 100 /**
@@ -102,9 +102,9 @@ var NoteList = { @@ -102,9 +102,9 @@ var NoteList = {
102 * Replaces the content of #notes-list with the given html. 102 * Replaces the content of #notes-list with the given html.
103 */ 103 */
104 setContent: 104 setContent:
105 - function(first_id, last_id, html) {  
106 - this.top_id = first_id;  
107 - this.bottom_id = last_id; 105 + function(newNoteIds, html) {
  106 + this.top_id = newNoteIds.first();
  107 + this.bottom_id = newNoteIds.last();
108 $("#notes-list").html(html); 108 $("#notes-list").html(html);
109 109
110 // init infinite scrolling 110 // init infinite scrolling
@@ -151,12 +151,12 @@ var NoteList = { @@ -151,12 +151,12 @@ var NoteList = {
151 // only load more notes if there are no "new" notes 151 // only load more notes if there are no "new" notes
152 $('.loading').show(); 152 $('.loading').show();
153 $.ajax({ 153 $.ajax({
154 - type: "GET",  
155 url: this.notes_path, 154 url: this.notes_path,
156 data: this.target_params + "&loading_more=1&" + (this.reversed ? "before_id" : "after_id") + "=" + this.bottom_id, 155 data: this.target_params + "&loading_more=1&" + (this.reversed ? "before_id" : "after_id") + "=" + this.bottom_id,
157 complete: function(){ $('.notes-status').removeClass("loading")}, 156 complete: function(){ $('.notes-status').removeClass("loading")},
158 beforeSend: function() { $('.notes-status').addClass("loading") }, 157 beforeSend: function() { $('.notes-status').addClass("loading") },
159 - dataType: "script"}); 158 + dataType: "script"
  159 + });
160 }, 160 },
161 161
162 /** 162 /**
@@ -164,9 +164,10 @@ var NoteList = { @@ -164,9 +164,10 @@ var NoteList = {
164 * Append notes to #notes-list. 164 * Append notes to #notes-list.
165 */ 165 */
166 appendMoreNotes: 166 appendMoreNotes:
167 - function(id, html) {  
168 - if(id != this.bottom_id) {  
169 - this.bottom_id = id; 167 + function(newNoteIds, html) {
  168 + var lastNewNoteId = newNoteIds.last();
  169 + if(lastNewNoteId != this.bottom_id) {
  170 + this.bottom_id = lastNewNoteId;
170 $("#notes-list").append(html); 171 $("#notes-list").append(html);
171 } 172 }
172 }, 173 },
@@ -212,10 +213,10 @@ var NoteList = { @@ -212,10 +213,10 @@ var NoteList = {
212 getNew: 213 getNew:
213 function() { 214 function() {
214 $.ajax({ 215 $.ajax({
215 - type: "GET",  
216 - url: this.notes_path,  
217 - data: this.target_params + "&loading_new=1&after_id=" + (this.reversed ? this.top_id : this.bottom_id),  
218 - dataType: "script"}); 216 + url: this.notes_path,
  217 + data: this.target_params + "&loading_new=1&after_id=" + (this.reversed ? this.top_id : this.bottom_id),
  218 + dataType: "script"
  219 + });
219 }, 220 },
220 221
221 /** 222 /**
@@ -223,7 +224,7 @@ var NoteList = { @@ -223,7 +224,7 @@ var NoteList = {
223 * Replaces the content of #new-notes-list with the given html. 224 * Replaces the content of #new-notes-list with the given html.
224 */ 225 */
225 replaceNewNotes: 226 replaceNewNotes:
226 - function(html) { 227 + function(newNoteIds, html) {
227 $("#new-notes-list").html(html); 228 $("#new-notes-list").html(html);
228 this.updateVotes(); 229 this.updateVotes();
229 }, 230 },
app/views/notes/index.js.haml
1 - unless @notes.blank? 1 - unless @notes.blank?
  2 + - new_note_ids = @notes.map(&:id)
2 - if loading_more_notes? 3 - if loading_more_notes?
3 :plain 4 :plain
4 - NoteList.appendMoreNotes(#{@notes.last.id}, "#{escape_javascript(render 'notes/notes')}"); 5 + NoteList.appendMoreNotes(#{new_note_ids}, "#{escape_javascript(render 'notes/notes')}");
5 6
6 - elsif loading_new_notes? 7 - elsif loading_new_notes?
7 :plain 8 :plain
8 - NoteList.replaceNewNotes("#{escape_javascript(render 'notes/notes')}"); 9 + NoteList.replaceNewNotes(#{new_note_ids}, "#{escape_javascript(render 'notes/notes')}");
9 10
10 - else 11 - else
11 :plain 12 :plain
12 - NoteList.setContent(#{@notes.first.id}, #{@notes.last.id}, "#{escape_javascript(render 'notes/notes')}"); 13 + NoteList.setContent(#{new_note_ids}, "#{escape_javascript(render 'notes/notes')}");
13 14
14 - else 15 - else
15 - if loading_more_notes? 16 - if loading_more_notes?