- Coupon Add/Edit/Delete rule AJAX
This commit is contained in:
gmorel
2013-09-11 09:48:33 +02:00
parent 8be785e9b3
commit 7e2f57e618
2 changed files with 31 additions and 33 deletions

View File

@@ -41,8 +41,6 @@
$(function($){
miniBrowser(0, '/test_to_remove/datas_coupon_edit.json');
Array.prototype.clean = function(deleteValue) {
for (var i = 0; i < this.length; i++) {
if (this[i] == deleteValue) {
@@ -53,7 +51,6 @@
return this;
};
// Init Rules
var initRules = function() {
var rules = [];
@@ -75,8 +72,6 @@
// Save Rules AJAX
var saveRuleAjax = function() {
console.log('rulesToSave');
console.log(rulesToSave);
var $url = '{$urlAjaxUpdateRules}';
$.ajax({
type: "POST",
@@ -107,12 +102,14 @@
// Add 1 Rule / or update the temporary Rules array then Save Rules via AJAX
var addRuleAjax = function(id) {
// If update
if(typeof id === 'number' && id % 1 == 0) {
rulesToSave[id] = ruleToSave;
} else {
} else { // If create
rulesToSave.push(ruleToSave);
}
// Save
saveRuleAjax();
}
@@ -193,12 +190,15 @@
}
onClickDeleteRule();
// Remove rule on click
// Update rule on click
var onClickUpdateRule = function() {
$('.constraint-update-btn').on('click', function (e) {
e.preventDefault();
var $this = $(this);
updateRuleAjax($this.attr('data-int'));
// Hide row being updated
$this.parent().parent().remove();
});
}
onClickUpdateRule();
@@ -215,21 +215,23 @@
// Reload rule inputs when changing effect
var onRuleChange = function() {
$('#category-rule').on('change', function (e) {
loadRuleInputs($(this).val(), null, function(ruleToSave) {});
loadRuleInputs($(this).val(), null, function(ruleToSave) {literal}{}{/literal});
});
}
onRuleChange();
// Fill in ready to be saved rule array
// var onInputsChange = function()
// In AJAX response
// Rule to save
var ruleToSave = {literal}{}{/literal};
// Rules which will be saved
var rulesToSave = initRules();
});
// Rule to save
var ruleToSave = {literal}{}{/literal};
</script>
{/block}

View File

@@ -72,33 +72,29 @@
<script>
// Init Rules to set
var ruleToSave = {};
ruleToSave['serviceId'] = '{$ruleId}';
ruleToSave['operators'] = {};
ruleToSave['values'] = {};
{foreach from=$inputs.inputs key=name item=input}
ruleToSave['operators']['{$name nofilter}'] = '{foreach from=$inputs.inputs[$name].availableOperators key=keyOperator item=valueOperator name=operators}{if $smarty.foreach.operators.first}{$keyOperator nofilter}{/if}{/foreach}';
ruleToSave['values']['{$name nofilter}'] = '{if count($inputs.inputs[$name].availableValues) != 0}{foreach from=$inputs.inputs[$name].availableValues key=keyValue item=valueValue name=values}{if $smarty.foreach.values.first}{$keyValue nofilter}{/if}{/foreach}{else}to set{/if}';
{/foreach}
ruleToSave['operators'] = {literal}{}{/literal};
ruleToSave['values'] = {literal}{}{/literal};
{foreach from=$inputs.inputs key=name item=input}
ruleToSave['operators']['{$name nofilter}'] = '{foreach from=$inputs.inputs[$name].availableOperators key=keyOperator item=valueOperator name=operators}{if $smarty.foreach.operators.first}{$keyOperator nofilter}{/if}{/foreach}';
ruleToSave['values']['{$name nofilter}'] = '{if count($inputs.inputs[$name].availableValues) != 0}{foreach from=$inputs.inputs[$name].availableValues key=keyValue item=valueValue name=values}{if $smarty.foreach.values.first}{$keyValue nofilter}{/if}{/foreach}{else}to set{/if}';
{/foreach}
// Fill in ready to be saved rule array
var onInputsChange = function() {literal}{{/literal}
{foreach from=$inputs.inputs key=name item=input}
$('#{$name}-operator').change(function (e) {
var $this = $(this);
console.log('{$name}-operator changed by ' + $this.val());
ruleToSave['operators']['{$name nofilter}'] = $this.val();
});
$('#{$name}-value').change(function (e) {
var $this = $(this);
console.log('{$name}-value changed by ' + $this);
ruleToSave['values']['{$name nofilter}'] = $this.val();
});
{/foreach}
console.log('will save ');
console.log(ruleToSave);
{foreach from=$inputs.inputs key=name item=input}
// Operator selector
$('#{$name}-operator').change(function (e) {
var $this = $(this);
ruleToSave['operators']['{$name nofilter}'] = $this.val();
});
// Value input
$('#{$name}-value').change(function (e) {
var $this = $(this);
ruleToSave['values']['{$name nofilter}'] = $this.val();
});
{/foreach}
{literal}}{/literal}
onInputsChange();
</script>