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;
          }
        });
      });
    }
  };
Example #2
0
 function render(reactElt, opts) {
   opts = opts || {};
   var scriptPath = 'javascript/compiled' + (PROD ? '.min' : '') + '.js';
   var scriptVersion = getHashForFilename('build/' + scriptPath);
   var stylePath = 'styles/main' + (PROD ? '.min' : '') + '.css';
   var styleVersion = getHashForFilename('build/' + stylePath);
   var sub = {
     EXTRA_HEAD: opts.extraHead || '',
     BODY_CLASS: opts.bodyClass || '',
     BODY_CONTENT: React.renderComponentToString(reactElt),
     BOOTSTRAP_DATA: JSON.stringify(opts.data || null),
     BOOTSTRAP_USER: JSON.stringify(opts.user || null),
     USER_ID: req.session.uid || 'null',
     CSRF_TOKEN: req.csrfToken(),
     SCRIPT_PATH: '/' + scriptPath + '?v=' + scriptVersion,
     STYLE_PATH: '/' + stylePath + '?v=' + styleVersion,
     PAGE_TITLE: opts.title || 'IRLMoji'
   };
   var re = new RegExp('{{ (' + _.keys(sub).join('|') + ') }}', 'g');
   res.writeHead(opts.statusCode || 200, {'Content-Type': 'text/html'});
   res.end(('' + PAGE_TEMPLATE).replace(re, function(m) {
     return sub[m.substring(3, m.length - 3)];
   }));
 }