From 2f607036f2e2abc7f29a90a41e89388f00dfdb9f Mon Sep 17 00:00:00 2001 From: Matchu Date: Fri, 23 Aug 2013 15:58:49 -0400 Subject: [PATCH] Bug fix: wardrobe's AJAX load order won't affect closet behavior Fun bug! If you edit an outfit, but the outfit loads before the closet items do, then we clone the outfit to give it its new identity and therefore forget about its item load callbacks. Now we have a cheap hack to forward item load data to the outfit's clones. Hooray! Hope this doesn't break tons of things! --- app/assets/javascripts/wardrobe.js | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/app/assets/javascripts/wardrobe.js b/app/assets/javascripts/wardrobe.js index 230f8fa0..b005f8df 100644 --- a/app/assets/javascripts/wardrobe.js +++ b/app/assets/javascripts/wardrobe.js @@ -270,6 +270,8 @@ function Wardrobe() { var outfit = this, previous_pet_type, worn_item_ids = [], closet_item_ids = [], new_record = true; + this.attribute_clones = [this]; + this.setWornAndUnwornItemIds = function (new_ids) { this.worn_and_unworn_item_ids = new_ids; worn_item_ids = new_ids.worn; @@ -456,7 +458,16 @@ function Wardrobe() { if(ids) closet_item_ids = ids; if(ids && ids.length) { Item.loadByIds(ids, function (items) { - outfit.closet_items = items; + // HACK: If this outfit is cloned before its items load, then the + // clone won't know to get the items. So, forward the results to our + // clones. (attribute_clones is initialized with this in it, for + // simplicity.) + for(var i = 0; i < outfit.attribute_clones.length; i++) { + outfit.attribute_clones[i].closet_items = items; + } + // HACK: And make sure we don't further cross-contaminate. + outfit.attribute_clones = [outfit]; + updateItemsCallback(items); }); } else { @@ -573,6 +584,10 @@ function Wardrobe() { new_ids.worn = base_ids.worn.slice(0); new_ids.unworn = base_ids.unworn.slice(0); outfit.setWornAndUnwornItemIds(new_ids); + + // HACK: Let the base outfit know that I'm a clone so I receive callbacks + // for requests it's already made. + base_outfit.attribute_clones.push(outfit); } function updateFromSaveResponse(data) {