/*
 * Refresh rendered file through mfr
 */

'use strict';

var $ = require('jquery');
var $osf = require('js/osfHelpers');

var FileRenderer = {
    start: function(url, selector){
        this.url = url;
        this.tries = 0;
        this.ALLOWED_RETRIES = 10;
        this.element = $(selector);
        this.getCachedFromServer();
    },

    getCachedFromServer: function() {
        var self = this;
        $.ajax({
            method: 'GET',
            url: self.url,
            beforeSend: $osf.setXHRAuthorization
        }).done(function(data) {
            if (data) {
                self.element.html(data.rendered);
            } else {
                self.handleRetry();
            }
        }).fail(self.handleRetry);
    },

    handleRetry: $osf.throttle(function() {
        var self = FileRenderer;
        self.tries += 1;

        if(self.tries > self.ALLOWED_RETRIES){
            self.element.html('Timeout occurred while loading, please refresh the page');
        } else {
            self.getCachedFromServer();
        }
    }, 1000)
};

module.exports = FileRenderer;

@@ -42,23 +42,8 @@
 */%0A%0A
-'use strict';%0A%0A
 var 
@@ -103,19 +103,24 @@
 ers');%0A%0A
-var
+function
  FileRen
@@ -128,32 +128,8 @@
 erer
- = %7B%0A    start: function
 (url
@@ -143,22 +143,41 @@
 tor)
+ 
 %7B%0A    
-    this
+var self = this;%0A%0A    self
 .url
@@ -188,24 +188,20 @@
 rl;%0A    
-    this
+self
 .tries =
@@ -208,24 +208,50 @@
  0;%0A    
-    this
+self.selector = selector;%0A    self
 .ALLOWED
@@ -269,24 +269,20 @@
 10;%0A    
-    this
+self
 .element
@@ -301,20 +301,52 @@
 r);%0A
-  
+%0A%0A
     
-  this
+self.start = function() %7B%0A        self
 .get
@@ -374,35 +374,28 @@
    %7D
-,
+;
 %0A%0A    
-getCachedFromServer:
+self.reload =
  fun
@@ -412,32 +412,105 @@
         
-var self = this;
+self.tries = 0;%0A        self.start();%0A    %7D;%0A%0A    self.getCachedFromServer = function() %7B
 %0A       
@@ -721,17 +721,8 @@
 data
-.rendered
 );%0A 
@@ -835,15 +835,20 @@
    %7D
-,
+;
 %0A%0A    
+self.
 hand
@@ -854,17 +854,18 @@
 dleRetry
-:
+ =
  $osf.th
@@ -888,41 +888,8 @@
 ) %7B%0A
-        var self = FileRenderer;%0A
     
@@ -1130,11 +1130,11 @@
 000)
+;
 %0A%7D
-;
 %0A%0Amo

Refactor to OOP ish design