Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jun 19th, 2012  |  syntax: JavaScript  |  size: 2.25 KB  |  hits: 17  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
This paste has a previous version, view the difference. Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. EW.models.Speaker = $.extend(Em.Object.extend(), {
  2.         factory: function (id, forceAjax, options) {
  3.             var model;
  4.             options = options || {};
  5.  
  6.             if (id) {
  7.                 model = this.getModel(id, forceAjax);
  8.             } else {
  9.                 ember_assert('You must provide a profile id when trying to use the speaker factory', false);
  10.                 return false;
  11.             }
  12.  
  13.             if (model !== null)
  14.             {
  15.                 $.extend(model, options);
  16.                 return this.create(model);
  17.             }
  18.         },
  19.         getModel: function (id, forceAjax) {
  20.             // if forceAjax, always pull for server
  21.             var _this = this;
  22.             forceAjax = typeof forceAjax !== 'boolean' ? false : forceAjax,
  23.             model = {};
  24.  
  25.             if (!forceAjax) {
  26.                 // try to pull from cache first, based on profile id
  27.                 model = EW.cache.get('profile').findProperty('id', id);
  28.                 if (model) {
  29.                     console.info('found model in cache');
  30.                     return model;
  31.                 }
  32.             }
  33.  
  34.             // if not in cache, pull from API
  35.             var ajaxReturn = $.ajax({
  36.                 url: 'http://' + EW.configs.api_server + '/profile/' + id + '?_depth=10',
  37.                 dataType: 'json',
  38.                 async: false,
  39.                 success: function(data, textStatus, jqXHR) {
  40.                     model = _this.store(data);
  41.                 },
  42.                 error: function(jqXHR, textStatus, errorThrown) {
  43.                     console.info('Error getting the profile model from within Speaker Factory! ', '   jqXHR: '  + jqXHR, '   textStatus: ' + textStatus, '   errorThrown: ', errorThrown);
  44.                 }
  45.             });
  46.  
  47.             return model;
  48.         },
  49.         store: function(model) {
  50.             // take data and store it in cache, then call the factory method which will return a reference of the profile model stored in cache
  51.             if (model && typeof model === 'object') {
  52.                 return this.factory(EW.cache.get('profile').pushObject(model).id);
  53.             } else ember_assert('You must pass in a valid speaker model to the Speaker Store', false);
  54.         }
  55.     });