Last active
November 5, 2017 00:07
Revisions
-
nhunzaker revised this gist
Apr 28, 2014 . 1 changed file with 4 additions and 3 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -1,11 +1,12 @@ var _ = require('underscore'); var Backbone = require('backbone'); var cheerio = require('cheerio'); var request = require('request'); Backbone.ajax = function(options) { options.json = true; return request(options, function(error, result) { if (error) { if (_.has(options, 'failure')) options.failure(error); } else { -
nhunzaker revised this gist
Apr 18, 2014 . 1 changed file with 3 additions and 11 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -19,14 +19,6 @@ Backbone.View.prototype.setElement = function($cheerio) { return this; }, Backbone.View.prototype._createElement = function(tagName) { return cheerio.load('<' + tagName + '>'); } -
nhunzaker revised this gist
Mar 20, 2014 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -5,7 +5,7 @@ var cheerio = require('cheerio'); Backbone.ajax = function(options) { options.json = true; return require('request')(options, function(error, result) { if (error) { if (_.has(options, 'failure')) options.failure(error); } else { -
nhunzaker revised this gist
Mar 20, 2014 . 1 changed file with 3 additions and 22 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -1,35 +1,20 @@ var Backbone = require('backbone'); var _ = require('underscore'); var cheerio = require('cheerio'); Backbone.ajax = function(options) { options.json = true; require('request')(options, function(error, result) { if (error) { if (_.has(options, 'failure')) options.failure(error); } else { (if (_.has(options, 'success')) options.success(result.body); } }); }; Backbone.View.prototype.setElement = function($cheerio) { this.$el = $cheerio("*"); return this; }, @@ -45,7 +30,3 @@ Backbone.View.prototype._ensureElement = function() { this.setElement(_.result(this, 'el')); } }; -
nhunzaker created this gist
Mar 20, 2014 .There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,51 @@ var config = require('./config'); var Backbone = require('backbone'); var cheerio = require('cheerio'); var _ = require('underscore'); var Q = require('q'); var url = require('url'); Backbone.ajax = function(options) { var deferred = Q.defer(); options.json = true; // Automatically fill in relative paths with the if (!require('url').parse(options.url).hostname) { options.url = url.resolve(config.API_URL, options.url); } require('request')(options, function(error, result) { if (error) { ('failure' in options) && options.failure(error); deferred.reject(new Error(error)); } else { ('success' in options) && options.success(result.body); deferred.resolve(result.body, result); } }); return deferred.promise; }; Backbone.View.prototype.setElement = function($cheerio) { this._$ = $cheerio; this.$el = $cheerio("*"); return this; }, Backbone.View.prototype._ensureElement = function() { if (!this.el) { var attrs = _.extend({}, _.result(this, 'attributes')); if (this.id) attrs.id = _.result(this, 'id'); if (this.className) attrs['class'] = _.result(this, 'className'); this.setElement(cheerio.load('<' + _.result(this, 'tagName') + '>')); this._setAttributes(attrs); } else { this.setElement(_.result(this, 'el')); } }; Backbone.View.prototype.toHTML = function() { return this._$.html(); };