
Untitled
By: a guest on
Jun 19th, 2012 | syntax:
JavaScript | size: 2.26 KB | hits: 21 | expires: Never
EW.models.Speaker = $.extend(Em.Object.extend(), {
factory: function (id, forceAjax, options) {
var model = {},
_this = this;
forceAjax = typeof forceAjax !== 'boolean' ? false : forceAjax,
options = options || {};
if (id) {
// if forceAjax, always pull for server
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');
$.extend(model, options);
return this.create(model);
}
}
// reset model
model = {};
// if not in cache, pull from API
$.ajax({
url: 'http://' + EW.configs.api_server + '/profile/' + id + '?_depth=10',
dataType: 'json',
success: function(data, textStatus, jqXHR) {
_this.store(data);
var newModel = _this.create($.extend(data, options));
$.extend(model, newModel);
},
error: function(jqXHR, textStatus, errorThrown) {
console.info('Error getting the profile model from within Speaker Factory! ', ' jqXHR: ' + jqXHR, ' textStatus: ' + textStatus, ' errorThrown: ', errorThrown);
}
});
return model;
} else {
ember_assert('You must provide a profile id when trying to use the speaker factory', false);
return false;
}
},
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') {
EW.cache.get('profile').pushObject(model);
} else ember_assert('You must pass in a valid speaker model to the Speaker Store', false);
}
});