Created
February 17, 2012 03:00
-
-
Save alivedise/1850168 to your computer and use it in GitHub Desktop.
[Grooveshark] BaseController
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
jQuery.Controller.extend("GS.Controllers.BaseController", { | |
setup: function() { | |
this._super.apply(this, arguments); | |
this.preSetup && this.preSetup() | |
}, | |
init: function() { | |
this._super(); | |
if ( this.onWindow && !this.isGSSingleton ) new this($(window)); | |
else this.onElement && !this.isGSSingleton && new this($(this.onElement)) | |
}, | |
instance: function() { | |
if ( this.isGSSingleton ) return new this(this.onElement && $(this.onElement) || this.onWindow && window || document.documentElement); | |
if ( this.onDocument ) return $(document.documentElement).controller(this._shortName); | |
if ( this.onWindow ) return $(window).controller(this._shortName); | |
if ( this.onElement ) return $(this.onElement).controller(this._shortName); | |
if ( this.hasActiveElement ) return $(this.hasActiveElement).controller(this._shortName); | |
throw "BaseController. controller, " + this._shortName + ", is improperly embedded on page"; | |
}, | |
singletonCallback: function( a, b ) { | |
a = "get" + _.ucwords(a); | |
var c = arguments; | |
return function() { | |
var g = GS[a](), | |
h = jQuery.makeArray(c).slice(2); | |
h.push.apply(h, arguments); | |
if ( g.onUpdateSubcription && !g.loaded ) { | |
var k = | |
$.subscribe(g.onUpdateSubcription, g.callback(function() { | |
g.loaded = true; | |
g[b].apply(g, h); | |
$.unsubscribe(k) | |
})); | |
return null | |
} | |
return g[b].apply(g, h) | |
} | |
}, | |
viewBundles: {}, | |
bundleVersions: {} | |
}, { | |
init: function() { | |
this.subscribe("gs.app.ready", this.callback(this.appReady)) | |
}, | |
appReady: function() {}, | |
destroy: function() { | |
if ( $.isArray(this.subscriptions) ) for (; this.subscriptions.length; ) $.unsubscribe(this.subscriptions.pop()); | |
this._super() | |
}, | |
subscribe: function( a, b, c ) { | |
c = _.orEqual(c, true); | |
if (!_.defined(this.subscriptions) ) this.subscriptions = []; | |
if ( c ) { | |
a = $.subscribe(a, b); | |
this.subscriptions.push(a); | |
return a | |
} else return $.subscribe(a, b) | |
}, | |
view: function( a, b, c, g ) { | |
var h = ["gs", "views"]; | |
if ( a.match(/^themes/) ) h = [a]; | |
else if ( a.match(/^\//) ) h.push(a.replace(/^\//, "")); | |
else { | |
h.push(this.Class._shortName); | |
h.push(a) | |
} | |
h = "/" + h.join("/"); | |
h += $.View.ext; | |
var k = h.replace(/[\/\.]/g, "_").replace(/_+/g, "_").replace(/^_/, ""), | |
m = GS.Controllers.BaseController.viewBundles[k], | |
p = GS.Controllers.BaseController.bundleVersions[m] || "", | |
n = "", | |
o = true; | |
b = _.orEqual(b, this); | |
c = this.calculateHelpers.call(this, c); | |
if ( $.View.preCached[k] || !m ) return $.View(h, b, c); | |
g = _.orEqual(g, 0); | |
if (!(g >= 3)) { | |
if ( g > 0 ) o = false; | |
k = { | |
contentType: "application/json", | |
dataType: "json", | |
type: "GET", | |
url: "/gs/views/" + m + ".json?" + p, | |
async: false, | |
cache: o, | |
success: this.callback(function( t ) { | |
if ( t ) { | |
_.forEach(t, function( w, x ) { | |
$.View.preCached[x] = w | |
}); | |
n = $.View(h, b, c) | |
} else { | |
g++; | |
setTimeout(this.callback(function() { | |
this.view(a, b, c, g) | |
}), g * 100) | |
} | |
}), | |
error: this.callback(function() { | |
g++; | |
setTimeout(this.callback(function() { | |
this.view(a, b, c, g) | |
}), g * 100) | |
}) | |
}; | |
if ( window.gsConfig && window.gsConfig.viewsJSONP ) { | |
k.url = gsConfig.assetHost + "/gs/views/" + m + ".json?" + p; | |
k.dataType = "jsonp"; | |
k.jsonp = false; | |
k.jsonpCallback = window.gsConfig.viewsJSONP + m | |
} | |
$.ajax(k); | |
return n | |
} | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment