outfit loading, yaay

This commit is contained in:
Emi Matchu 2010-11-12 16:14:08 -05:00
parent 6b92c2aa33
commit 54eb96a6de
2 changed files with 89 additions and 14 deletions

View file

@ -277,12 +277,18 @@ View.Hash = function (wardrobe) {
if(!arraysMatch(new_data.closet, data.closet)) { if(!arraysMatch(new_data.closet, data.closet)) {
wardrobe.outfit.setClosetItemsByIds(new_data.closet.slice(0)); wardrobe.outfit.setClosetItemsByIds(new_data.closet.slice(0));
} }
} else if(!arraysMatch(new_data.objects, data.closet)) { } else if(new_data.objects && !arraysMatch(new_data.objects, data.closet)) {
wardrobe.outfit.setClosetItemsByIds(new_data.objects.slice(0)); wardrobe.outfit.setClosetItemsByIds(new_data.objects.slice(0));
} else {
wardrobe.outfit.setClosetItemsByIds([]);
} }
if(new_data.objects) {
if(!arraysMatch(new_data.objects, data.objects)) { if(!arraysMatch(new_data.objects, data.objects)) {
wardrobe.outfit.setWornItemsByIds(new_data.objects.slice(0)); wardrobe.outfit.setWornItemsByIds(new_data.objects.slice(0));
} }
} else {
wardrobe.outfit.setWornItemsByIds([]);
}
if(new_data.name != data.name && new_data.name) { if(new_data.name != data.name && new_data.name) {
wardrobe.base_pet.setName(new_data.name); wardrobe.base_pet.setName(new_data.name);
} }
@ -338,14 +344,20 @@ View.Hash = function (wardrobe) {
setInterval(checkQuery, 100); setInterval(checkQuery, 100);
} }
wardrobe.outfit.bind('updateClosetItems', function (items) { function singleOutfitResponse(event_name, response) {
wardrobe.outfit.bind(event_name, function () {
if(!wardrobe.outfit.in_transaction) response.apply(this, arguments);
});
}
singleOutfitResponse('updateClosetItems', function (items) {
var item_ids = items.map('id'); var item_ids = items.map('id');
if(!arraysMatch(item_ids, data.closet)) { if(!arraysMatch(item_ids, data.closet)) {
changeQuery({closet: item_ids}); changeQuery({closet: item_ids});
} }
}); });
wardrobe.outfit.bind('updateWornItems', function (items) { singleOutfitResponse('updateWornItems', function (items) {
var item_ids = items.map('id'), changes = {}; var item_ids = items.map('id'), changes = {};
if(!arraysMatch(item_ids, data.objects)) { if(!arraysMatch(item_ids, data.objects)) {
changes.objects = item_ids; changes.objects = item_ids;
@ -358,7 +370,7 @@ View.Hash = function (wardrobe) {
if(changes.objects || changes.closet) changeQuery(changes); if(changes.objects || changes.closet) changeQuery(changes);
}); });
wardrobe.outfit.bind('updatePetType', function (pet_type) { singleOutfitResponse('updatePetType', function (pet_type) {
if(pet_type.color_id != data.color || pet_type.species_id != data.species) { if(pet_type.color_id != data.color || pet_type.species_id != data.species) {
changeQuery({ changeQuery({
color: pet_type.color_id, color: pet_type.color_id,
@ -368,17 +380,27 @@ View.Hash = function (wardrobe) {
} }
}); });
wardrobe.outfit.bind('petTypeNotFound', function () { singleOutfitResponse('petTypeNotFound', function () {
window.history.back(); window.history.back();
}); });
wardrobe.outfit.bind('updatePetState', function (pet_state) { singleOutfitResponse('updatePetState', function (pet_state) {
var pet_type = wardrobe.outfit.getPetType(); var pet_type = wardrobe.outfit.getPetType();
if(pet_state.id != data.state && pet_type && (data.state || pet_state.id != pet_type.pet_state_ids[0])) { if(pet_state.id != data.state && pet_type && (data.state || pet_state.id != pet_type.pet_state_ids[0])) {
changeQuery({state: pet_state.id}); changeQuery({state: pet_state.id});
} }
}); });
wardrobe.outfit.bind('setOutfit', function (outfit) {
changeQuery({
closet: outfit.getClosetItemIds(),
color: outfit.pet_type.color_id,
objects: outfit.getWornItemIds(),
species: outfit.pet_type.species_id,
state: outfit.pet_state.id
});
});
wardrobe.search.bind('updateRequest', function (request) { wardrobe.search.bind('updateRequest', function (request) {
if(request.offset != data.search_offset || request.query != data.search) { if(request.offset != data.search_offset || request.query != data.search) {
changeQuery({ changeQuery({
@ -482,6 +504,10 @@ View.Outfits = function (wardrobe) {
outfit_el.addClass('hiding').hide('normal', function () { outfit_el.remove() }); outfit_el.addClass('hiding').hide('normal', function () { outfit_el.remove() });
}); });
$('#preview-outfits h4').live('click', function () {
wardrobe.outfit.load($(this).tmplItem().data.clone());
});
$('input.outfit-url').live('mouseover', function () { $('input.outfit-url').live('mouseover', function () {
this.focus(); this.focus();
}).live('mouseout', function () { }).live('mouseout', function () {

View file

@ -223,15 +223,30 @@ function Wardrobe() {
new_record = true; new_record = true;
if(typeof data != 'undefined') { if(typeof data != 'undefined') {
this.color_id = data.color_id;
this.id = data.id; this.id = data.id;
this.name = data.name; this.name = data.name;
this.pet_state_id = data.pet_state_id;
this.starred = data.starred; this.starred = data.starred;
this.species_id = data.species_id;
this.worn_and_unworn_item_ids = data.worn_and_unworn_item_ids;
worn_item_ids = this.worn_and_unworn_item_ids.worn;
closet_item_ids = this.worn_and_unworn_item_ids.unworn.
concat(this.worn_and_unworn_item_ids.worn);
new_record = false; new_record = false;
} }
this.closet_items = []; this.closet_items = [];
this.worn_items = []; this.worn_items = [];
this.getWornItemIds = function () { // TODO just expose the worn_item_ids
return worn_item_ids;
}
this.getClosetItemIds = function () { // TODO just expose the closet_item_ids
return closet_item_ids;
}
function getRestrictedZones() { function getRestrictedZones() {
// note: may contain duplicates - loop through assets, not these, for // note: may contain duplicates - loop through assets, not these, for
// best performance // best performance
@ -323,7 +338,10 @@ function Wardrobe() {
this.setClosetItemsByIds = function (ids, updateItemsCallback) { this.setClosetItemsByIds = function (ids, updateItemsCallback) {
if(ids) closet_item_ids = ids; if(ids) closet_item_ids = ids;
if(ids && ids.length) { if(ids && ids.length) {
this.closet_items = Item.loadByIds(ids, updateItemsCallback); Item.loadByIds(ids, function (items) {
outfit.closet_items = items;
updateItemsCallback(items);
});
} else { } else {
this.closet_items = []; this.closet_items = [];
updateItemsCallback(this.closet_items); updateItemsCallback(this.closet_items);
@ -408,6 +426,21 @@ function Wardrobe() {
return {worn_item_ids: worn_item_ids, unworn_item_ids: unworn_item_ids}; return {worn_item_ids: worn_item_ids, unworn_item_ids: unworn_item_ids};
} }
this.clone = function () {
return new Outfit({
color_id: outfit.color_id,
id: outfit.id,
name: outfit.name,
species_id: outfit.species_id,
starred: outfit.starred,
pet_state_id: outfit.pet_state_id,
worn_and_unworn_item_ids: {
worn: outfit.worn_and_unworn_item_ids.worn.slice(0),
unworn: outfit.worn_and_unworn_item_ids.unworn.slice(0)
}
});
}
this.destroy = function (success) { this.destroy = function (success) {
$.ajax({ $.ajax({
url: '/outfits/' + outfit.id + '.json', url: '/outfits/' + outfit.id + '.json',
@ -615,6 +648,12 @@ function Wardrobe() {
var controller = this; var controller = this;
this.events = {}; this.events = {};
function fireEvent(event_name, subarguments) {
$.each(controller.events[event_name], function () {
this.apply(controller, subarguments);
});
}
this.bind = function (event, callback) { this.bind = function (event, callback) {
if(typeof this.events[event] == 'undefined') { if(typeof this.events[event] == 'undefined') {
this.events[event] = []; this.events[event] = [];
@ -624,10 +663,7 @@ function Wardrobe() {
this.event = function (event_name) { this.event = function (event_name) {
return function () { return function () {
var subarguments = arguments; fireEvent(event_name, arguments);
$.each(controller.events[event_name], function () {
this.apply(controller, subarguments);
});
} }
} }
@ -635,7 +671,7 @@ function Wardrobe() {
var subarguments, event; var subarguments, event;
if(controller.events[event_name]) { if(controller.events[event_name]) {
subarguments = Array.prototype.slice.apply(arguments, [1]); subarguments = Array.prototype.slice.apply(arguments, [1]);
controller.event(event_name).apply(controller, subarguments); fireEvent(event_name, subarguments);
} }
} }
} }
@ -645,6 +681,8 @@ function Wardrobe() {
Controller.all.Outfit = function OutfitController() { Controller.all.Outfit = function OutfitController() {
var controller = this, outfit = new Outfit; var controller = this, outfit = new Outfit;
this.in_transaction = false;
this.closetItem = function (item) { this.closetItem = function (item) {
outfit.closetItem( outfit.closetItem(
item, item,
@ -668,6 +706,17 @@ function Wardrobe() {
return outfit.worn_items; return outfit.worn_items;
} }
this.load = function (new_outfit) {
outfit = new_outfit;
this.in_transaction = true;
controller.setPetTypeByColorAndSpecies(outfit.color_id, outfit.species_id);
controller.setPetStateById(outfit.pet_state_id);
controller.setClosetItemsByIds(outfit.getClosetItemIds());
controller.setWornItemsByIds(outfit.getWornItemIds());
this.in_transaction = false;
controller.events.trigger('setOutfit', outfit);
}
this.save = function (starred, name) { this.save = function (starred, name) {
outfit.starred = starred; outfit.starred = starred;
outfit.name = name; outfit.name = name;