Skip to content

Instantly share code, notes, and snippets.

@nhunzaker
Last active November 5, 2017 00:07

Revisions

  1. nhunzaker revised this gist Apr 28, 2014. 1 changed file with 4 additions and 3 deletions.
    7 changes: 4 additions & 3 deletions isomorphicish.js
    Original file line number Diff line number Diff line change
    @@ -1,11 +1,12 @@
    var _ = require('underscore');
    var Backbone = require('backbone');
    var _ = require('underscore');
    var cheerio = require('cheerio');
    var cheerio = require('cheerio');
    var request = require('request');

    Backbone.ajax = function(options) {
    options.json = true;

    return require('request')(options, function(error, result) {
    return request(options, function(error, result) {
    if (error) {
    if (_.has(options, 'failure')) options.failure(error);
    } else {
  2. nhunzaker revised this gist Apr 18, 2014. 1 changed file with 3 additions and 11 deletions.
    14 changes: 3 additions & 11 deletions isomorphicish.js
    Original file line number Diff line number Diff line change
    @@ -19,14 +19,6 @@ Backbone.View.prototype.setElement = function($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._createElement = function(tagName) {
    return cheerio.load('<' + tagName + '>');
    }
  3. nhunzaker revised this gist Mar 20, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion isomorphicish.js
    Original file line number Diff line number Diff line change
    @@ -5,7 +5,7 @@ var cheerio = require('cheerio');
    Backbone.ajax = function(options) {
    options.json = true;

    require('request')(options, function(error, result) {
    return require('request')(options, function(error, result) {
    if (error) {
    if (_.has(options, 'failure')) options.failure(error);
    } else {
  4. nhunzaker revised this gist Mar 20, 2014. 1 changed file with 3 additions and 22 deletions.
    25 changes: 3 additions & 22 deletions isomorphicish.js
    Original file line number Diff line number Diff line change
    @@ -1,35 +1,20 @@
    var config = require('./config');
    var Backbone = require('backbone');
    var cheerio = require('cheerio');
    var _ = require('underscore');
    var Q = require('q');
    var url = require('url');
    var cheerio = require('cheerio');

    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));
    if (_.has(options, 'failure')) options.failure(error);
    } else {
    ('success' in options) && options.success(result.body);
    deferred.resolve(result.body, result);
    (if (_.has(options, 'success')) options.success(result.body);
    }
    });

    return deferred.promise;
    };

    Backbone.View.prototype.setElement = function($cheerio) {
    this._$ = $cheerio;
    this.$el = $cheerio("*");
    return this;
    },
    @@ -45,7 +30,3 @@ Backbone.View.prototype._ensureElement = function() {
    this.setElement(_.result(this, 'el'));
    }
    };

    Backbone.View.prototype.toHTML = function() {
    return this._$.html();
    };
  5. nhunzaker created this gist Mar 20, 2014.
    51 changes: 51 additions & 0 deletions isomorphicish.js
    Original 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();
    };