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

Untitled

By: a guest on Jun 19th, 2012  |  syntax: JavaScript  |  size: 2.26 KB  |  hits: 21  |  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.                 _this = this;
  5.             forceAjax = typeof forceAjax !== 'boolean' ? false : forceAjax,
  6.             options = options || {};
  7.            
  8.  
  9.             if (id) {
  10.                 // if forceAjax, always pull for server
  11.                 if (!forceAjax) {
  12.                     // try to pull from cache first, based on profile id
  13.                     model = EW.cache.get('profile').findProperty('id', id);
  14.                     if (model) {
  15.                         console.info('found model in cache');
  16.                         $.extend(model, options);
  17.                         return this.create(model);
  18.                     }
  19.                 }
  20.  
  21.                 // reset model
  22.                 model = {};
  23.  
  24.                 // if not in cache, pull from API
  25.                 $.ajax({
  26.                     url: 'http://' + EW.configs.api_server + '/profile/' + id + '?_depth=10',
  27.                     dataType: 'json',
  28.                     success: function(data, textStatus, jqXHR) {
  29.                         _this.store(data);
  30.                         var newModel = _this.create($.extend(data, options));
  31.                        
  32.                         $.extend(model, newModel);
  33.                     },
  34.                     error: function(jqXHR, textStatus, errorThrown) {
  35.                         console.info('Error getting the profile model from within Speaker Factory! ', '   jqXHR: '  + jqXHR, '   textStatus: ' + textStatus, '   errorThrown: ', errorThrown);
  36.                     }
  37.                 });
  38.  
  39.                 return model;
  40.             } else {
  41.                 ember_assert('You must provide a profile id when trying to use the speaker factory', false);
  42.                 return false;
  43.             }
  44.         },
  45.         store: function(model) {
  46.             // take data and store it in cache, then call the factory method which will return a reference of the profile model stored in cache
  47.             if (model && typeof model === 'object') {
  48.                 EW.cache.get('profile').pushObject(model);
  49.             } else ember_assert('You must pass in a valid speaker model to the Speaker Store', false);
  50.         }
  51.     });