function handle_bars(obj, elem) {
  if (_.isString(elem)) {
    return elem.replace(/\{([\.\w]+)\}/g, function(match, what) {
      return get_member(obj, what);
    });
  }

  if (_.isObject(elem)) {
    elem = _.extend({}, elem);

    _.each(elem, function(val, key) {
      elem[key] = handle_bars(obj, val);
    });
  }
}
  Model.prototype._update = function(data, incomplete) {
    // clear the object
    if (!incomplete) {
      for (var n in this) {
        if (this.hasOwnProperty(n) && n !== '_adapter' && n !== '_type') {
          delete this[n];
        }
      }
    }

    // same as init in copying over object
    this._href = data.href;
    this._setValues = {};
    this._deferred = data._deferred || false;
    this.links = data.links || {};
    this._rawData = data;
    this._loadTime = new Date();
    this._keys = _.keys(this);

    if (!this._href) {
      _.extend(this, data);
    } else {
      _.each(data, function(val, key) {
        if (['href', 'links'].indexOf(key) >= 0 || Model.prototype[key]) {
          return;
        }

        Object.defineProperty(Model.prototype, key, {
          enumerable: true,
          get: function() {
            if (this === Model.prototype) {
              return true; // not working on an object
            }

            if (this._deferred) {
              return this.get(key);
            }

            return this._setValues[key] || this._rawData[key] || val;
          },
          set: function(value) {
            this._setValues[key] = value;
          }
        });
      });
    }
  };
  it('can set up an object to listen on another\'s events', function() {
    var childObj = {};
    var childCallback = sinon.spy();
    var callback = sinon.spy();

    _.extend(childObj, Backbone.Events);

    childObj.on('child_event', childCallback);
    myObj.listenTo(childObj, 'child_event', callback);
    childObj.trigger('child_event');

    expect(callback).to.have.been.called;

    // stop listening
    myObj.stopListening(childObj);
    childObj.trigger('child_event');
    expect(callback).to.have.callCount(1); // should be 2 but since stopped listening, its only once!

  });
 beforeEach(function() {
   myObj = {};
   _.extend(myObj, Backbone.Events);
 });
APIClient.prototype._request = function(method, path, parameters, isList) {
  debug('Requesting [%s] %s with data %o', method, path, parameters);
  // Storing self for future uses
  var self = this;

  // Create the request options
  var url = this.baseUrl + path;
  var requestOptions = _.extend({
    method: method,
    url: url
  }, this.options.requestOptions);

  requestOptions.url = requestOptions.url.replace(/([^:])\/+/g, '$1/');

  // Create Promises
  var promise = q.defer();

  JSONPromise(parameters).then(function(parameters) {
      // console.log('done with json promise', parameters);
      if (['GET', 'DELETE'].indexOf(requestOptions.method) >= 0) {
        requestOptions.qs = parameters;
      } else {
        requestOptions.json = parameters;
      }

      // If the options defines a function to process request options
      // Run it with the request options
      if (_.isFunction(self.options.processRequestOptions)) {
        requestOptions = self.options.processRequestOptions(requestOptions);
      }

      // Make the actual Request
      return request(requestOptions, function(err, req, body) {
        // Handle errors
        if (err) {
          return promise.reject(err);
        }

        // Handle 400s
        if (req.statusCode >= 400) {
          var e = new HTTPError('Status Code: ' + req.statusCode + ' Body: ' + (_.isString(body) ? body : JSON.stringify(body, null, 4)));
          return promise.reject(e);
        }

        // Handle no content case
        if (req.statusCode === 204) {
          return promise.resolve(null);
        }

        try {
          // Try to parse
          var json = _.isString(body) ? JSON.parse(req.body) : body;
          var payload = self._normalizePayload(json);
          return promise.resolve(self._processResult(payload, isList));
        } catch (error) {
          return promise.reject(error);
        }
      });
    });

  return promise.promise;
};
Example #6
0
    extend: function (opts) {
        this.config = _.extend(this.config, opts);
	return this;
    },