Hey Everyone,
I am hoping to get help with optimizing my code. I have multiple reference fields that are connected to 10 difference repeaters. Each one acts as a submission/dynamic form which a user signs up and fills out and then can go back and update.
I have a lot of redundant code (wixData.isReferenced, and wixData.queryReferenced) which is calling the network multiple times and is slowing down the page.
NOTE : it is important that the checkboxes be either inserted or replaced as they may be blank to start.
Additionally, the code doesn’t seem to capture all the checkboxes when selected. Let’s say I check every item displayed in the repeater and save, only maybe 1/3 or half of them get saved in the collection
Anyone have suggestions on how to optimize/improve?
$w.onReady(() => {
$w(“#dynamicDataset”).onReady(() => {
$w.onReady( function () {
wixData.query("Members")
.eq("_owner", wixUsers.currentUser.id)
.find()
.then((results4) => {
let OwnerRegistered = results4.items;
let MemberID = OwnerRegistered[0]._id;
let MemberOwner = OwnerRegistered[0]._owner;
let Profile = $w(“#dynamicDataset”).getCurrentItem();
let ProfileID = Profile._id;
$w("#DecorRepeater").forEachItem(($item, itemData, index) => {
let DecorCheckbox = $item(“#checkbox405”);
let DecorID = itemData._id
wixData.isReferenced("DesignProfile", "items", ProfileID, DecorID).then((result3) => {
let isReferenced3 = result3;
if (isReferenced3 === true ) {
DecorCheckbox.checked = true
} else { DecorCheckbox.checked = false }
})
wixData.queryReferenced("DesignProfile", ProfileID, "items")
.then((results11) => {
if (results11.items.length > 0) {
///ON SAVE - REPLACE REFERENCES///
$w(‘#SaveProfile’).onClick((event, $w) => {
let checked = [];
if (DecorCheckbox.checked)
checked.push(DecorID);
wixData.replaceReferences(“DesignProfile”, “items”, ProfileID, checked)
.then(() => {
})
. **catch** ((error) => {
});
});
} **else** {
//ON SAVE - INSERT REFERENCES////
$w(‘#SaveProfile’).onClick((event, $w) => {
let checked = [];
if (DecorCheckbox.checked)
checked.push(DecorID);
wixData.insertReference(“DesignProfile”, “items”, ProfileID, checked)
.then(() => {
})
})
}
});
});
$w("#RoomRepeater").forEachItem(($item, itemData, index) => {
let RoomsCheckbox = $item(“#checkbox406”);
let RoomsID = itemData._id
wixData.isReferenced("DesignProfile", "rooms", ProfileID, RoomsID).then((result3) => {
let isReferenced3 = result3;
if (isReferenced3 === true ) {
RoomsCheckbox.checked = true
} else { RoomsCheckbox.checked = false }
})
wixData.queryReferenced("DesignProfile", ProfileID, "rooms")
.then((results11) => {
if (results11.items.length > 0) {
///ON SAVE - REPLACE REFERENCES///
$w(‘#SaveProfile’).onClick((event, $w) => {
let checked = [];
if (RoomsCheckbox.checked)
checked.push(RoomsID);
wixData.replaceReferences(“DesignProfile”, “rooms”, ProfileID, checked)
.then(() => {
})
. **catch** ((error) => {
});
});
} **else** {
//ON SAVE - INSERT REFERENCES////
$w(‘#SaveProfile’).onClick((event, $w) => {
let checked = [];
if (RoomsCheckbox.checked)
checked.push(RoomsID);
wixData.insertReference(“DesignProfile”, “rooms”, ProfileID, checked)
.then(() => {
})
})
}
});
});
$w("#StyleRepeater").forEachItem(($item, itemData, index) => {
let StyleCheckbox = $item(“#checkbox407”);
let StyleID = itemData._id
wixData.isReferenced("DesignProfile", "style", ProfileID, StyleID).then((result3) => {
let isReferenced3 = result3;
if (isReferenced3 === true ) {
StyleCheckbox.checked = true
} else { StyleCheckbox.checked = false }
})
wixData.queryReferenced("DesignProfile", ProfileID, "style")
.then((results11) => {
if (results11.items.length > 0) {
///ON SAVE - REPLACE REFERENCES///
$w(‘#SaveProfile’).onClick((event, $w) => {
let checked = [];
if (StyleCheckbox.checked)
checked.push(StyleID);
wixData.replaceReferences(“DesignProfile”, “style”, ProfileID, checked)
.then(() => {
})
. **catch** ((error) => {
});
});
} **else** {
//ON SAVE - INSERT REFERENCES////
$w(‘#SaveProfile’).onClick((event, $w) => {
let checked = [];
if (StyleCheckbox.checked)
checked.push(StyleID);
wixData.insertReference(“DesignProfile”, “style”, ProfileID, checked)
.then(() => {
})
})
}
});
});
$w("#VibeRepeater").forEachItem(($item, itemData, index) => {
let VibeCheckbox = $item(“#checkbox408”);
let VibeID = itemData._id
wixData.isReferenced("DesignProfile", "vibe", ProfileID, VibeID).then((result3) => {
let isReferenced3 = result3;
if (isReferenced3 === true ) {
VibeCheckbox.checked = true
} else { VibeCheckbox.checked = false }
})
wixData.queryReferenced("DesignProfile", ProfileID, "vibe")
.then((results11) => {
if (results11.items.length > 0) {
///ON SAVE - REPLACE REFERENCES///
$w(‘#SaveProfile’).onClick((event, $w) => {
let checked = [];
if (VibeCheckbox.checked)
checked.push(VibeID);
wixData.replaceReferences(“DesignProfile”, “vibe”, ProfileID, checked)
.then(() => {
})
. **catch** ((error) => {
});
});
} **else** {
//ON SAVE - INSERT REFERENCES////
$w(‘#SaveProfile’).onClick((event, $w) => {
let checked = [];
if (VibeCheckbox.checked)
checked.push(VibeID);
wixData.insertReference(“DesignProfile”, “vibe”, ProfileID, checked)
.then(() => {
})
})
}
});
});
$w("#PriorityRepeater").forEachItem(($item, itemData, index) => {
let PrioritiesCheckbox = $item(“#checkbox409”);
let PrioritiesID = itemData._id
wixData.isReferenced("DesignProfile", "priorities", ProfileID, PrioritiesID).then((result3) => {
let isReferenced3 = result3;
if (isReferenced3 === true ) {
PrioritiesCheckbox.checked = true
} else { PrioritiesCheckbox.checked = false }
})
wixData.queryReferenced("DesignProfile", ProfileID, "priorities")
.then((results11) => {
if (results11.items.length > 0) {
///ON SAVE - REPLACE REFERENCES///
$w(‘#SaveProfile’).onClick((event, $w) => {
let checked = [];
if (PrioritiesCheckbox.checked)
checked.push(PrioritiesID);
wixData.replaceReferences(“DesignProfile”, “priorities”, ProfileID, checked)
.then(() => {
})
. **catch** ((error) => {
});
});
} **else** {
//ON SAVE - INSERT REFERENCES////
$w(‘#SaveProfile’).onClick((event, $w) => {
let checked = [];
if (PrioritiesCheckbox.checked)
checked.push(PrioritiesID);
wixData.insertReference(“DesignProfile”, “priorities”, ProfileID, checked)
.then(() => {
})
})
}
});
});
$w("#BaseColorRepeater").forEachItem(($item, itemData, index) => {
let BaseColorCheckbox = $item(“#checkbox403”);
let BaseColorID = itemData._id
wixData.isReferenced("DesignProfile", "baseColors", ProfileID, BaseColorID).then((result3) => {
let isReferenced3 = result3;
if (isReferenced3 === true ) {
BaseColorCheckbox.checked = true
} else { BaseColorCheckbox.checked = false }
})
wixData.queryReferenced("DesignProfile", ProfileID, "baseColors")
.then((results11) => {
if (results11.items.length > 0) {
///ON SAVE - REPLACE REFERENCES///
$w(‘#SaveProfile’).onClick((event, $w) => {
let checked = [];
if (BaseColorCheckbox.checked)
checked.push(BaseColorID);
wixData.replaceReferences(“DesignProfile”, “baseColors”, ProfileID, checked)
.then(() => {
})
. **catch** ((error) => {
});
});
} **else** {
//ON SAVE - INSERT REFERENCES////
$w(‘#SaveProfile’).onClick((event, $w) => {
let checked = [];
if (BaseColorCheckbox.checked)
checked.push(BaseColorID);
wixData.insertReference(“DesignProfile”, “baseColors”, ProfileID, checked)
.then(() => {
})
})
}
});
});
$w("#AccentColorRepeater").forEachItem(($item, itemData, index) => {
let AccentColorCheckbox = $item(“#checkbox404”);
let AccentColorID = itemData._id
wixData.isReferenced("DesignProfile", "accentColors", ProfileID, AccentColorID).then((result3) => {
let isReferenced3 = result3;
if (isReferenced3 === true ) {
AccentColorCheckbox.checked = true
} else { AccentColorCheckbox.checked = false }
})
wixData.queryReferenced(“DesignProfile”, ProfileID, “accentColors”)
.then((results12) => {
if (results12.items.length > 0) {
///ON SAVE - REPLACE REFERENCES///
$w(‘#SaveProfile’).onClick((event, $w) => {
let checked = [];
if (AccentColorCheckbox.checked)
checked.push(AccentColorID);
wixData.replaceReferences(“DesignProfile”, “accentColors”, ProfileID, checked)
.then(() => {
})
. **catch** ((error) => {
});
});
} **else** {
//ON SAVE - INSERT REFERENCES////
$w(‘#SaveProfile’).onClick((event, $w) => {
let checked = [];
if (AccentColorCheckbox.checked)
checked.push(AccentColorID);
wixData.insertReference(“DesignProfile”, “accentColors”, ProfileID, checked)
.then(() => {
})
})
}
});
});
$w("#MaterialsRepeater").forEachItem(($item, itemData, index) => {
let MaterialsColorCheckbox = $item(“#checkbox411”);
let MaterialsColorID = itemData._id
wixData.isReferenced("DesignProfile", "materials", ProfileID, MaterialsColorID).then((result3) => {
let isReferenced3 = result3;
if (isReferenced3 === true ) {
MaterialsColorCheckbox.checked = true
} else { MaterialsColorCheckbox.checked = false }
})
wixData.queryReferenced(“DesignProfile”, ProfileID, “materials”)
.then((results12) => {
if (results12.items.length > 0) {
///ON SAVE - REPLACE REFERENCES///
$w(‘#SaveProfile’).onClick((event, $w) => {
let checked = [];
if (MaterialsColorCheckbox.checked)
checked.push(MaterialsColorID);
wixData.replaceReferences(“DesignProfile”, “materials”, ProfileID, checked)
.then(() => {
})
. **catch** ((error) => {
});
});
} **else** {
//ON SAVE - INSERT REFERENCES////
$w(‘#SaveProfile’).onClick((event, $w) => {
let checked = [];
if (MaterialsColorCheckbox.checked)
checked.push(MaterialsColorID);
wixData.insertReference(“DesignProfile”, “materials”, ProfileID, checked)
.then(() => {
})
})
}
});
});
$w("#PatternsRepeater").forEachItem(($item, itemData, index) => {
let PatternsColorCheckbox = $item(“#checkbox410”);
let PatternsColorID = itemData._id
wixData.isReferenced("DesignProfile", "patterns", ProfileID, PatternsColorID).then((result3) => {
let isReferenced3 = result3;
if (isReferenced3 === true ) {
PatternsColorCheckbox.checked = true
} else { PatternsColorCheckbox.checked = false }
})
wixData.queryReferenced(“DesignProfile”, ProfileID, “patterns”)
.then((results12) => {
if (results12.items.length > 0) {
///ON SAVE - REPLACE REFERENCES///
$w(‘#SaveProfile’).onClick((event, $w) => {
let checked = [];
if (PatternsColorCheckbox.checked)
checked.push(PatternsColorID);
wixData.replaceReferences(“DesignProfile”, “patterns”, ProfileID, checked)
.then(() => {
})
. **catch** ((error) => {
});
});
} **else** {
//ON SAVE - INSERT REFERENCES////
$w(‘#SaveProfile’).onClick((event, $w) => {
let checked = [];
if (PatternsColorCheckbox.checked)
checked.push(PatternsColorID);
wixData.insertReference(“DesignProfile”, “patterns”, ProfileID, checked)
.then(() => {
})
})
}
});
});
$w("#WhyHomeWellRepeater").forEachItem(($item, itemData, index) => {
let WhyColorCheckbox = $item(“#checkbox413”);
let WhyColorID = itemData._id
wixData.isReferenced("Members", "whyHomeWell", MemberID, WhyColorID).then((result3) => {
let isReferenced3 = result3;
if (isReferenced3 === true ) {
WhyColorCheckbox.checked = true
} else { WhyColorCheckbox.checked = false }
})
wixData.queryReferenced(“Members”, MemberID, “whyHomeWell”)
.then((results12) => {
if (results12.items.length > 0) {
///ON SAVE - REPLACE REFERENCES///
$w(‘#SaveProfile’).onClick((event, $w) => {
let checked = [];
if (WhyColorCheckbox.checked)
checked.push(WhyColorID);
wixData.replaceReferences(“Members”, “whyHomeWell”, MemberID, checked)
.then(() => {
})
. **catch** ((error) => {
});
});
} **else** {
//ON SAVE - INSERT REFERENCES////
$w(‘#SaveProfile’).onClick((event, $w) => {
let checked = [];
if (WhyColorCheckbox.checked)
checked.push(WhyColorID);
wixData.insertReference(“Members”, “whyHomeWell”, MemberID, checked)
.then(() => {
})
})
}
});
});
$w("#SaveProfile").onClick((event, $w) => {
$w("#SaveProfile").disable()
wixLocation.to('/MyAccount/Home/' + MemberID)
});
});
});
});
});
@stcroppe @liran-kurtz-wix @sapirh @jenee-washington @yisrael-wix @yoav-wix @mikemoynihan99