Skip to content

Instantly share code, notes, and snippets.

@benjamw
Last active August 29, 2015 14:27
Show Gist options
  • Save benjamw/f6d5d682caddd4c1e506 to your computer and use it in GitHub Desktop.
Save benjamw/f6d5d682caddd4c1e506 to your computer and use it in GitHub Desktop.
; // the thing
(function($) {
"use strict";
/**
* The Thing
*
* @constructor
*/
function Thing( ) {
// properties
this.data = {};
this.dfd = undefined;
this.url = [
/* path */,
/* id */
];
}
Thing.prototype.pull = function(url, callback, args, type) {
args = args || {};
type = type || 'GET';
$.ajax({
type: type,
url: foo.root+ url,
data: $.extend({ }, args),
dataType: 'json',
success: callback,
error: this.dfd.reject
})
};
Thing.prototype.pull_As = function(dfd) {
dfd = dfd || this.dfd;
var that = this;
var callback = function(data) {
// ... do As data cleanup
dfd.resolve( );
};
this.pull('a', callback, this.query);
};
Thing.prototype.pull_D = function( ) {
// there technically isn't anything to pull directly
this.pull_As( );
};
Thing.prototype.pull_C = function(id, dfd) {
if ("Object" === typeof id) {
dfd = id;
id = not_defined;
}
id = id || url[2];
dfd = dfd || this.dfd;
var that = this;
var callback = function(data) {
// ... do C data cleanup
dfd.resolve( );
};
this.pull('c/'+ id, callback);
};
Thing.prototype.pull_B = function(dfd) {
dfd = dfd || this.dfd;
var step2,
that = this;
step2 = dfd.then(function(data) {
console.log('INSIDE THEN');
console.log(data);
var def = new $.Deferred( );
that.pull_C(data.C_id, def);
return def.promise( );
}, function( ) {
// something broke
return false;
});
step2.done(function( ) {
// this needs to go in here to override
// the data that came through in the C request
// ... do B data cleanup
dfd.resolve( );
});
var callback = function(data) {
console.log('INSIDE CALLBACK');
console.log(data);
// ... do initial B data cleanup
// not sure how to get it to pass along to step2 here...
};
this.pull('b/'+ this.url[2], callback);
};
Thing.prototype.pull_A = function(dfd) {
dfd = dfd || this.dfd;
this.nav = false;
var that = this;
var callback = function(data) {
// ... do A data cleanup
that.data.a = data;
dfd.resolve( );
};
this.pull('a/'+ this.url[2], callback);
};
Thing.prototype.render = function( ) {
var i, len,
html,
that = foo.thing, // because 'this' is the promise object
title = document.title.split('|');
for (i = 0, len = title.length; i < len; i += 1) {
title[i] = $.trim(title[i]);
}
title[0] = $.trim(that.title);
document.title = title.join(' | ');
console.log(that.data);
html = Hogan.wrapper.render({
'data': that.data,
});
console.log(html);
$('#thing_wrapper').empty( ).append(html);
};
Thing.prototype.init = function( ) {
this.data = {};
this.dfd = $.Deferred( );
var $when = $.when(this.dfd);
switch (this.url[1].toLowerCase( )) {
case 'a' :
console.log("A");
this.pull_A( );
break;
case 'b' :
console.log("B");
this.pull_B( );
break;
case 'c' :
console.log("C");
this.pull_C( );
break;
case '' : // no break
default :
console.log("D(efault)");
this.pull_D( );
break;
}
// render when all deferred actions are complete
$when.done(
this.render
);
};
// expose the constructor
window.Thing = Thing;
})(jQuery);
// elsewhere in the site...
if ( ! foo) { foo = { }; }
foo.thing = new Thing( );
foo.thing.init( );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment