Adobe Campaign:Javascript XML E4X 开关盒
Adobe Campaign: Javascript XML E4X Switch Case
给定以下 table Adobe Campaign 中的数据模型
我开发了以下脚本,它是 javascript、xml、e4x(由 adobe campaign 使用)的混合体。该脚本基本上遍历每一行并为每个开关执行案例内的代码。我正在寻找一种方法来简化 switches/cases,因为它们有点多余?谁能提出更好的方法?
var query = xtk.queryDef.create(
<queryDef schema="temp:enrich" operation="select">
<select>
<node expr="@id"/>
<node expr="@fun"/>
<node expr="@news"/>
<node expr="@events"/>
<node expr="@student"/>
</select>
</queryDef>)
var result = query.ExecuteQuery();
for each(var i in result.enrich)
{
//Debug: logInfo(i.@id+ " "+i.@fun+" " +i.@news+" " +i.@student);
var recipient = <recipient _key = "@id" id = {i.@id} />;
var fun = parseInt(i.@fun);
var news = parseInt(i.@news);
var events = parseInt(i.@events);
var student = parseInt(i.@student);
switch(fun) {
case 0:
nms.subscription.Unsubscribe("uosFunStuff", recipient);
break;
case 1:
nms.subscription.Subscribe("uosFunStuff", recipient,false);
break;
default:
// donothing
}
switch(news) {
case 0:
nms.subscription.Unsubscribe("uosUniversityNews", recipient);
break;
case 1:
nms.subscription.Subscribe("uosUniversityNews", recipient,false);
break;
default:
// donothing
}
switch(events) {
case 0:
nms.subscription.Unsubscribe("uosEvents", recipient);
break;
case 1:
nms.subscription.Subscribe("uosEvents", recipient,false);
break;
default:
// donothing
}
switch(student) {
case 0:
nms.subscription.Unsubscribe("uosStudentLife", recipient);
break;
case 1:
nms.subscription.Subscribe("uosStudentLife", recipient,false);
break;
default:
// donothing
}
}
大量重复代码和少量替换让我想到使用带有地图的单独函数。
function updateSubscription(val, label, recipient) {
if (val) {
nms.subscription.Unsubscribe(label, recipient);
} else {
nms.subscription.Subscribe(label, recipient, false);
}
}
for each(var i in results.enrich()) {
//Debug: logInfo(i.@id+ " "+i.@fun+" " +i.@news+" " +i.@student);
var recipient = <recipient _key = "@id" id = {i.@id} />;
updateSubscription(parseInt(i.@fun), 'uosFunStuff', recipient);
updateSubscription(parseInt(i.@news), 'uosUniversityNews', recipient);
updateSubscription(parseInt(i.@events), 'uosEvents', recipient);
updateSubscription(parseInt(i.@student), 'uosStudentLife', recipient);
}
作为旁注,在您有大约 5 个选项之前,通常最好使用 if-else 而不是 switch。对此有很多注意事项,但对于零或一个,if-else 是更好的主意。
简介
代码可以通过 3 种方式进行优化。由于我只在 javascript 中进行了测试,因此您必须了解适合您的方法。
标签和函数数组
var labels = ["uosFunStuff", "uosUniversityNews", "uosEvents","uosStudentLife"];
var functionArray = [
function(input){return parseInt(input.@fun) },
function(input){return parseInt(input.@news) },
function(input){return parseInt(input.@events) },
function(input){return parseInt(input.@student) }
];
for each(var i in results.enrich()) {
var recipient = <recipient _key = "@id" id = {i.@id} />;
var functionIndex = 0;
for each(var label in labels) {
if(functionArray[functionIndex++](i) == 1){
nms.subscription.Subscribe(label, recipient,false);
}else{
nms.subscription.Unsubscribe(label, recipient);
}
});
}
标签数组,键数组
var labels = ["uosFunStuff", "uosUniversityNews", "uosEvents","uosStudentLife"];
var keys = [ "@fun","@news","@events","@student"];
for each(var i in results.enrich()) {
var recipient = <recipient _key = "@id" id = {i.@id} />;
var functionIndex = 0;
for each(var label in labels) {
if(parseInt(i[keys[functionIndex++]])== 1){
nms.subscription.Subscribe(label, recipient,false);
}else{
nms.subscription.Unsubscribe(label, recipient);
}
});
}
对象(这个可能不行)
var labelsAndKeys = {
uosFunStuff:"@fun",
uosUniversityNews:"@news",
uosEvents:"@events",
uosStudentLife:"@student"
};
for each(var i in results.enrich()) {
var recipient = <recipient _key = "@id" id = {i.@id} />;
for each(var label in labels) {
if(parseInt(i[labelsAndKeys[labels])== 1){
nms.subscription.Subscribe(label, recipient,false);
}else{
nms.subscription.Unsubscribe(label, recipient);
}
});
}
给定以下 table Adobe Campaign 中的数据模型
我开发了以下脚本,它是 javascript、xml、e4x(由 adobe campaign 使用)的混合体。该脚本基本上遍历每一行并为每个开关执行案例内的代码。我正在寻找一种方法来简化 switches/cases,因为它们有点多余?谁能提出更好的方法?
var query = xtk.queryDef.create(
<queryDef schema="temp:enrich" operation="select">
<select>
<node expr="@id"/>
<node expr="@fun"/>
<node expr="@news"/>
<node expr="@events"/>
<node expr="@student"/>
</select>
</queryDef>)
var result = query.ExecuteQuery();
for each(var i in result.enrich)
{
//Debug: logInfo(i.@id+ " "+i.@fun+" " +i.@news+" " +i.@student);
var recipient = <recipient _key = "@id" id = {i.@id} />;
var fun = parseInt(i.@fun);
var news = parseInt(i.@news);
var events = parseInt(i.@events);
var student = parseInt(i.@student);
switch(fun) {
case 0:
nms.subscription.Unsubscribe("uosFunStuff", recipient);
break;
case 1:
nms.subscription.Subscribe("uosFunStuff", recipient,false);
break;
default:
// donothing
}
switch(news) {
case 0:
nms.subscription.Unsubscribe("uosUniversityNews", recipient);
break;
case 1:
nms.subscription.Subscribe("uosUniversityNews", recipient,false);
break;
default:
// donothing
}
switch(events) {
case 0:
nms.subscription.Unsubscribe("uosEvents", recipient);
break;
case 1:
nms.subscription.Subscribe("uosEvents", recipient,false);
break;
default:
// donothing
}
switch(student) {
case 0:
nms.subscription.Unsubscribe("uosStudentLife", recipient);
break;
case 1:
nms.subscription.Subscribe("uosStudentLife", recipient,false);
break;
default:
// donothing
}
}
大量重复代码和少量替换让我想到使用带有地图的单独函数。
function updateSubscription(val, label, recipient) {
if (val) {
nms.subscription.Unsubscribe(label, recipient);
} else {
nms.subscription.Subscribe(label, recipient, false);
}
}
for each(var i in results.enrich()) {
//Debug: logInfo(i.@id+ " "+i.@fun+" " +i.@news+" " +i.@student);
var recipient = <recipient _key = "@id" id = {i.@id} />;
updateSubscription(parseInt(i.@fun), 'uosFunStuff', recipient);
updateSubscription(parseInt(i.@news), 'uosUniversityNews', recipient);
updateSubscription(parseInt(i.@events), 'uosEvents', recipient);
updateSubscription(parseInt(i.@student), 'uosStudentLife', recipient);
}
作为旁注,在您有大约 5 个选项之前,通常最好使用 if-else 而不是 switch。对此有很多注意事项,但对于零或一个,if-else 是更好的主意。
简介
代码可以通过 3 种方式进行优化。由于我只在 javascript 中进行了测试,因此您必须了解适合您的方法。
标签和函数数组
var labels = ["uosFunStuff", "uosUniversityNews", "uosEvents","uosStudentLife"];
var functionArray = [
function(input){return parseInt(input.@fun) },
function(input){return parseInt(input.@news) },
function(input){return parseInt(input.@events) },
function(input){return parseInt(input.@student) }
];
for each(var i in results.enrich()) {
var recipient = <recipient _key = "@id" id = {i.@id} />;
var functionIndex = 0;
for each(var label in labels) {
if(functionArray[functionIndex++](i) == 1){
nms.subscription.Subscribe(label, recipient,false);
}else{
nms.subscription.Unsubscribe(label, recipient);
}
});
}
标签数组,键数组
var labels = ["uosFunStuff", "uosUniversityNews", "uosEvents","uosStudentLife"];
var keys = [ "@fun","@news","@events","@student"];
for each(var i in results.enrich()) {
var recipient = <recipient _key = "@id" id = {i.@id} />;
var functionIndex = 0;
for each(var label in labels) {
if(parseInt(i[keys[functionIndex++]])== 1){
nms.subscription.Subscribe(label, recipient,false);
}else{
nms.subscription.Unsubscribe(label, recipient);
}
});
}
对象(这个可能不行)
var labelsAndKeys = {
uosFunStuff:"@fun",
uosUniversityNews:"@news",
uosEvents:"@events",
uosStudentLife:"@student"
};
for each(var i in results.enrich()) {
var recipient = <recipient _key = "@id" id = {i.@id} />;
for each(var label in labels) {
if(parseInt(i[labelsAndKeys[labels])== 1){
nms.subscription.Subscribe(label, recipient,false);
}else{
nms.subscription.Unsubscribe(label, recipient);
}
});
}