
Untitled
By: a guest on
Jun 19th, 2012 | syntax:
JavaScript | size: 2.25 KB | hits: 17 | expires: Never
EW.models.Speaker = $.extend(Em.Object.extend(), {
factory: function (id, forceAjax, options) {
var model;
options = options || {};
if (id) {
model = this.getModel(id, forceAjax);
} else {
ember_assert('You must provide a profile id when trying to use the speaker factory', false);
return false;
}
if (model !== null)
{
$.extend(model, options);
return this.create(model);
}
},
getModel: function (id, forceAjax) {
// if forceAjax, always pull for server
var _this = this;
forceAjax = typeof forceAjax !== 'boolean' ? false : forceAjax,
model = {};
if (!forceAjax) {
// try to pull from cache first, based on profile id
model = EW.cache.get('profile').findProperty('id', id);
if (model) {
console.info('found model in cache');
return model;
}
}
// if not in cache, pull from API
var ajaxReturn = $.ajax({
url: 'http://' + EW.configs.api_server + '/profile/' + id + '?_depth=10',
dataType: 'json',
async: false,
success: function(data, textStatus, jqXHR) {
model = _this.store(data);
},
error: function(jqXHR, textStatus, errorThrown) {
console.info('Error getting the profile model from within Speaker Factory! ', ' jqXHR: ' + jqXHR, ' textStatus: ' + textStatus, ' errorThrown: ', errorThrown);
}
});
return model;
},
store: function(model) {
// take data and store it in cache, then call the factory method which will return a reference of the profile model stored in cache
if (model && typeof model === 'object') {
return this.factory(EW.cache.get('profile').pushObject(model).id);
} else ember_assert('You must pass in a valid speaker model to the Speaker Store', false);
}
});