start by caching data for preview, remove aggressive preloading for the moment

This commit is contained in:
Emi Matchu 2010-06-04 21:09:19 -04:00
parent 40bb495a2b
commit 45f1ee868e

View file

@ -40,9 +40,10 @@ function LoadError(base_msg) {
} }
function PetType() { function PetType() {
var pet_type = this; var pet_type = this, loaded_data = false, loaded_assets = false;
this.activated = true; this.activated = true;
this.assets = true;
this.deactivate = function (error, args) { this.deactivate = function (error, args) {
var msg; var msg;
@ -60,23 +61,25 @@ function PetType() {
this.load = function () { this.load = function () {
var url = '/species/' + this.species_id + '/color/' + this.color_id + '/pet_type.json', var url = '/species/' + this.species_id + '/color/' + this.color_id + '/pet_type.json',
pet_type = this; pet_type = this;
function onComplete() { Item.current.load(pet_type); loadAssets(); }
if(loaded_data) {
onComplete();
} else {
$.ajax({ $.ajax({
url: url, url: url,
dataType: 'json', dataType: 'json',
success: function (data) { success: function (data) {
pet_type.id = data.id; pet_type.id = data.id;
pet_type.body_id = data.body_id; pet_type.body_id = data.body_id;
Item.current.load(pet_type); loaded_data = true;
$.getJSON('/pet_types/' + data.id + '/swf_assets.json', function (assets) { onComplete();
pet_type.assets = assets;
Preview.update();
});
}, },
error: function () { error: function () {
pet_type.deactivate(PetType.LOAD_ERROR); pet_type.deactivate(PetType.LOAD_ERROR);
} }
}); });
} }
}
this.setAsCurrent = function () { this.setAsCurrent = function () {
PetType.current = this; PetType.current = this;
@ -90,6 +93,18 @@ function PetType() {
} }
} }
function loadAssets() {
function onComplete() { if(pet_type == PetType.current) Preview.update(); }
if(!loaded_assets) {
$.getJSON('/pet_types/' + pet_type.id + '/swf_assets.json', function (assets) {
pet_type.assets = assets;
loaded_assets = true;
onComplete();
});
}
onComplete();
}
function showDeactivationMsg() { function showDeactivationMsg() {
Preview.disable(pet_type.deactivation_msg); Preview.disable(pet_type.deactivation_msg);
} }
@ -111,19 +126,30 @@ PetType.createFromLink = function (link) {
} }
function Item() { function Item() {
this.assets_by_body_id = {};
this.load = function (pet_type) { this.load = function (pet_type) {
var url = '/' + this.id + '/swf_assets.json?body_id=' + pet_type.body_id, var url = '/' + this.id + '/swf_assets.json?body_id=' + pet_type.body_id,
item = this; item = this;
function onComplete() { if(pet_type == PetType.current) Preview.update() }
if(this.getAssetsForPetType(pet_type).length) {
onComplete();
} else {
$.getJSON(url, function (data) { $.getJSON(url, function (data) {
if(data.length) { if(data.length) {
item.assets = data; item.assets_by_body_id[pet_type.body_id] = data;
Preview.update(); onComplete();
} else { } else {
pet_type.deactivate(Item.LOAD_ERROR, { pet_type.deactivate(Item.LOAD_ERROR, {
item: item.name item: item.name
}); });
} }
}) });
}
}
this.getAssetsForPetType = function (pet_type) {
return this.assets_by_body_id[pet_type.body_id] || [];
} }
this.setAsCurrent = function () { this.setAsCurrent = function () {
@ -140,7 +166,7 @@ Item.createFromLocation = function () {
} }
Preview = new function Preview() { Preview = new function Preview() {
var assets = [], swf_id, swf, updateWhenFlashReady = false; var swf_id, swf, updateWhenFlashReady = false;
this.setFlashIsReady = function () { this.setFlashIsReady = function () {
swf = document.getElementById(swf_id); swf = document.getElementById(swf_id);
@ -148,12 +174,15 @@ Preview = new function Preview() {
} }
this.update = function (assets) { this.update = function (assets) {
var assets = []; var assets = [], asset_sources = [
PetType.current.assets,
Item.current.getAssetsForPetType(PetType.current)
];
if(swf) { if(swf) {
$.each([PetType, Item], function () { $.each(asset_sources, function () {
if(this.current.assets) assets = assets.concat(this.current.assets); assets = assets.concat(this);
}); });
assets = $.each(assets, function () { $.each(assets, function () {
this.local_path = this.local_url; this.local_path = this.local_url;
}); });
swf.setAssets(assets); swf.setAssets(assets);
@ -204,8 +233,8 @@ speciesList.each(function () {
MainWardrobe = { View: { Outfit: Preview } }; MainWardrobe = { View: { Outfit: Preview } };
setTimeout(function () { /*setTimeout(function () {
$.each(PetType.all, function () { $.each(PetType.all, function () {
this.load(); this.load();
}); });
}, 5000); }, 5000);*/