[extensions-web] review: Don't make a bogus request when there's no diff



commit a2d194703583ef2defc1d651e36b6365378fd640
Author: Jasper St. Pierre <jstpierre mecheye net>
Date:   Tue Jun 5 12:06:48 2012 -0400

    review: Don't make a bogus request when there's no diff

 sweettooth/static/js/review.js |  148 ++++++++++++++++++++--------------------
 1 files changed, 75 insertions(+), 73 deletions(-)
---
diff --git a/sweettooth/static/js/review.js b/sweettooth/static/js/review.js
index a8f7e68..9ada48c 100644
--- a/sweettooth/static/js/review.js
+++ b/sweettooth/static/js/review.js
@@ -54,92 +54,94 @@ define(['jquery', 'diff'], function($, diff) {
     }
 
     $.fn.reviewify = function(diff) {
-        var $elem = $(this);
-        var $fileList = $('<ul>', {'class': 'filelist'}).appendTo($elem);
-        var pk = $elem.data('pk');
-
-        var $fileDisplay = $('<div>', {'class': 'filedisplay'}).appendTo($elem);
-        $fileDisplay.css('position', 'relative');
- 
-        var currentFilename;
-        var $currentFile = null;
-
-        var req = $.ajax({
-            type: 'GET',
-            dataType: 'json',
-            url: REVIEW_URL_BASE + '/get-file-list/' + pk,
-        });
-
-        function showTable(filename, $file, $selector) {
-            $fileList.find('li a.fileselector').removeClass('selected');
-            $selector.addClass('selected');
-
-            $file.css('position', 'relative');
+        return this.each(function() {
+            var $elem = $(this);
+            var $fileList = $('<ul>', {'class': 'filelist'}).appendTo($elem);
+            var pk = $elem.data('pk');
+
+            var $fileDisplay = $('<div>', {'class': 'filedisplay'}).appendTo($elem);
+            $fileDisplay.css('position', 'relative');
+
+            var currentFilename;
+            var $currentFile = null;
+
+            var req = $.ajax({
+                type: 'GET',
+                dataType: 'json',
+                url: REVIEW_URL_BASE + '/get-file-list/' + pk,
+            });
+
+            function showTable(filename, $file, $selector) {
+                $fileList.find('li a.fileselector').removeClass('selected');
+                $selector.addClass('selected');
+
+                $file.css('position', 'relative');
+
+                if ($currentFile != null) {
+                    $currentFile.css({'position': 'absolute',
+                                      'top': '0'});
+                    $currentFile.fadeOut();
+                    $file.fadeIn();
+                } else {
+                    $file.show();
+                }
 
-            if ($currentFile != null) {
-                $currentFile.css({'position': 'absolute',
-                                  'top': '0'});
-                $currentFile.fadeOut();
-                $file.fadeIn();
-            } else {
-                $file.show();
+                currentFilename = filename;
+                $currentFile = $file;
             }
 
-            currentFilename = filename;
-            $currentFile = $file;
-        }
-
-        req.done(function(files) {
-            function createFileSelector(tag, filename) {
-                var $selector = $('<a>').
-                    addClass(tag).
-                    addClass('fileselector').
-                    text(filename);
+            req.done(function(files) {
+                function createFileSelector(tag, filename) {
+                    var $selector = $('<a>').
+                        addClass(tag).
+                        addClass('fileselector').
+                        text(filename);
 
-                var $file = null;
+                    var $file = null;
 
-                if (diff && isBinary(filename)) {
-                    // We don't show binary files in the diff view.
-                    return;
-                }
-
-                $('<li>').append($selector).appendTo($fileList);
+                    if (diff && isBinary(filename)) {
+                        // We don't show binary files in the diff view.
+                        return;
+                    }
 
-                if (isBinary(filename)) {
-                    $selector.addClass('binary');
-                    return;
-                }
+                    $('<li>').append($selector).appendTo($fileList);
 
-                $selector.click(function() {
-                    if ($selector.hasClass('selected'))
+                    if (isBinary(filename)) {
+                        $selector.addClass('binary');
                         return;
-
-                    if ($file === null) {
-                        var d = (diff ? createDiffView : createFileView)(filename, pk, diff);
-                        currentFilename = filename;
-                        d.done(function($table) {
-                            $file = $table;
-                            $file.hide().appendTo($fileDisplay);
-                            if (currentFilename === filename)
-                                showTable(filename, $file, $selector);
-                        });
-                    } else {
-                        showTable(filename, $file, $selector);
                     }
 
-                });
-            }
+                    $selector.click(function() {
+                        if ($selector.hasClass('selected'))
+                            return;
+
+                        if ($file === null) {
+                            var d = (diff ? createDiffView : createFileView)(filename, pk, diff);
+                            currentFilename = filename;
+                            d.done(function($table) {
+                                $file = $table;
+                                $file.hide().appendTo($fileDisplay);
+                                if (currentFilename === filename)
+                                    showTable(filename, $file, $selector);
+                            });
+                        } else {
+                            showTable(filename, $file, $selector);
+                        }
+
+                    });
+                }
 
-            $.each(files.changed, function() { createFileSelector('changed', this); });
-            $.each(files.added, function() { createFileSelector('added', this); });
-            $.each(files.deleted, function() { createFileSelector('deleted', this); });
+                $.each(files.changed, function() { createFileSelector('changed', this); });
+                $.each(files.added, function() { createFileSelector('added', this); });
+                $.each(files.deleted, function() { createFileSelector('deleted', this); });
 
-            // Don't show the 'unchanged' section in a diff view.
-            if (!diff)
-                $.each(files.unchanged, function() { createFileSelector('unchanged', this); });
+                // Don't show the 'unchanged' section in a diff view.
+                if (!diff)
+                    $.each(files.unchanged, function() { createFileSelector('unchanged', this); });
 
-            // Select the first item.
-            $fileList.find('li a.fileselector').first().click();
+                // Select the first item.
+                $fileList.find('li a.fileselector').first().click();
+            });
         });
     };
 });



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]