Skip to content

Instantly share code, notes, and snippets.

@mdamaceno
Created November 3, 2016 12:12
Show Gist options
  • Select an option

  • Save mdamaceno/d553dd81466cf23efb249874012320a9 to your computer and use it in GitHub Desktop.

Select an option

Save mdamaceno/d553dd81466cf23efb249874012320a9 to your computer and use it in GitHub Desktop.
uso indiscriminado de setTimeout
openModalSelectValues: function(product) {
var _this = this;
var getAttributesURL = '{{apiUrl}}/attributes?fields[attributes]=id,name&sort=name';
var attrs = [];
_this.configurable_product.modal.product = product;
_this.configurable_product.modal.attributes = [];
_this.$http.get(getAttributesURL).then((response) => {
var attributes = response.data.data;
attributes.forEach(function(a, k) {
if (_.contains(_this.configurable_product.attributes.selected, a.attributes.id)) {
attrs.push({
id: a.attributes.id,
name: a.attributes.name,
values: []
});
}
});
setTimeout(function() {
attrs.forEach(function(a) {
var getAttributeValuesURL = '{{apiUrl}}/attributes/' + a.id + '/values';
_this.$http.get(getAttributeValuesURL).then((response) => {
var values = response.data.data;
// a.values = values;
values.forEach(function(v, k) {
a.values.push({
id: v.attributes.id,
name: v.attributes.name
});
});
a.values = _.uniq(a.values);
}, (response) => {});
});
}, 300);
}, (response) => {});
$('select#configurable_product-attribute-values').empty();
setTimeout(function() {
_.uniq(attrs).forEach(function(a) {
var optgroup = $('<optgroup>');
optgroup.attr('label', a.name);
a.values.forEach(function(v) {
var option = $("<option>");
option.val(v.id);
option.text(v.name);
optgroup.append(option);
});
$('select#configurable_product-attribute-values').append(optgroup);
});
$('select#configurable_product-attribute-values').multiselect('rebuild');
}, 600);
$('#modal-select-values').modal('show');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment