Extjs - newbie, Switch case function not working - extjs

In this following code I am getting the desired output
fn: function(btn) {
switch(btn){
case 'yes':
Ext.Msg.prompt('Milton', 'Where is it?');
break;
case 'no':
Ext.Msg.alert('Milton',
'Im going to burn the building down!');
break;
case 'cancel':
Ext.Msg.wait('Saving tables to disk...','File Copy');
break;
}
}
This Works just fine. Now i am trying to do a function call in the switch 'yes' but i don't get any output in the screen.
This is the code i am using.
case 'yes':
Ext.Msg.prompt('Milton', 'Where is it?', function(btn,txt)
{
if (txt.toLowerCase() == 'the office') {
Ext.get('my_id').dom.innerHTML = 'Dull Work';
}else{
Ext.get('my_id').dom.innerHTML = txt;
}
Ext.DomHelper.applyStyles('my_id',{
background: 'transparent
url(images/stapler.png) 50% 50% no-repeat'
});
});
break;
Using this code inside the swich case 'yes' , i am getting a blank screen. Even the dialog box has dissapeared. Please help.

It is a very basic javascript error. A string cannot extend over the end of the line. This will work.
case 'yes':
Ext.Msg.prompt('Milton', 'Where is it?', function(btn,txt){
if (txt.toLowerCase() == 'the office') {
Ext.get('my_id').dom.innerHTML = 'Dull Work';
}else{
Ext.get('my_id').dom.innerHTML = txt;
}
Ext.DomHelper.applyStyles('my_id',{
background: 'transparent url(images/stapler.png) 50% 50% no-repeat'
});
});
break;
In the console I got the error SyntaxError: Unexpected token ILLEGAL

Related

BulkDelete Discord.js (How to add interger parsing)

How to bulkDelete in discord.js. Please give me a program for it...
bot.on('message', message => {
let args = message.content.substring(PREFIX.length).split(" ");
switch (args[0]) {
case 'Ping':
message.channel.send('Pong!');
break;
case 'clear':
if (!args[1]) return message.reply('Error please mention second arg');
message.delete.bulkDelete(args[1]);
break;
}
})
I've tried the above code and found " TypeError: message.delete.bulkDelete is not a function "
as error.
It's quite simple, you replace:
message.delete.bulkDelete(args[1]);
with:
message.channel.bulkDelete(Number.parseInt(args[1]));

Compare values in an array using if conditions in a for loop

so I'm utilizing the Ocrad.js library in an ionic project to be able to read text from images. In the app, the user can add 'items' (as in words) to an array and then I would like to check whether or not these items (words) are present in the text from the image. For instance: if the image has a sentence: 'I like football' and the user added the word 'football' to the list, on the press of a button, the app would check whether or not 'football' exists in 'I like football' and would say so to the user. In an else occasion, it would also say so.
So far I came up with this situation:
for(let item of this.itemList) {
if(text.indexOf(item.name)>=0){
alert('word found');
} else {
alert('word not found');
}
}
The idea was to loop through the list of items(words) that the user had added to the array and give them the appropriate response. While it is working if I add just one word, like the football example I mentioned above, if I add more words to the list, the loop will obviously give me the 2 alerts. So if add the word 'soccer' to the list and therefore have an array with 'football' and 'soccer', I would get the 2 alerts, which makes sense because I wasn't stopping the loop. So I tried a switch case, which did not work for some reason (I don't really know why, but it kept giving me the two alerts). This is what my switch looked like:
for(let item of this.itemList){
switch(true){
case (text.indexOf(item.name)>=0): {
alert('word found');
break;
}
case (text.indexOf(item.name) <= 0):{
alert('word not found');
break;
}
default: {
alert('please add a word to the list');
break;
}
}
}
So after playing around and researching, I could not really find something that helped me very well. The idea again is if the image text says 'I like football' and I add 3 items to the array: 'soccer', basketball', 'football', the answer would be 'word found' only, whereas if the word is not there, I would get 'word not found' only once! I believe I might be doing something really stupid that I can't see with my noobs' eyes haha. I hope my question made sense. Can anyone help me with this? Cheers guys!
Edit - This is the function I am actually using with the camera feature:
onAnalyze() {
let matchFound = false;
let loader = this.loadingCtrl.create({
content: 'Please wait...'
});
loader.present();
(<any>window).OCRAD(document.getElementById('image'), text => {
loader.dismissAll();
alert(text);
console.log(text);
for(const item of this.itemList)
{
if(text.indexOf(item) >=0){
matchFound = true;
break;
}
}
if(found){
alert('word found!');
} else{
alert('word not found!');
}
}
);
}
In the first example you provided, the for-loop continues to evaluate the input string, even though you've already found a match (which I believe is what you want, to see only one alert for any number of successful matches). The below example introduces a boolean matchFound, which is set to true on the first match. If a match is found, we break out of the for-loop, terminating its execution (and preventing the N number of alerts):
const itemList = ['football', 'nachos', 'sports'];
const textFromImage = 'I like football and nachos';
isTextPresent(itemList, textFromImage);
function isTextPresent(itemList: string[], textFromImage: string) {
let matchFound = false;
for (const item of this.itemList) {
if (textFromImage.indexOf(item) >= 0) {
matchFound = true;
break;
}
}
if (matchFound) {
alert('word found');
} else {
alert('word not found');
}
}
FWIW, Array.prototype.some() could also be used here as well. On the first match from the textFromImage, it terminates (much like the for-loop that breaks in the first example):
const itemList = ['football', 'nachos', 'sports'];
const textFromImage = 'I like football and nachos';
isTextPresent(itemList, textFromImage);
function isTextPresent(itemList: string[], textFromImage: string) {
const matchFound = itemList.some((item: string) => {
return textFromImage.indexOf(item) >= 0;
});
if (matchFound) {
alert('word found');
} else {
alert('word not found');
}
}
You should use an nested for loop.
let words = ['soccer', basketball', 'football']
let foundedWords = []
for(let item of this.itemList) {
for(let word in words){
if(text.indexOf(word)>=0){
foundedWords.push(word)
alert('word found');
} else {
alert('word not found');
}
}
}
And finally you will get a list of founded words.
I think this should be fine for you.

Show and close dynamically created dialog in angularjs

I have scenarios which are created dynamically by clicking Add Scenario button. Initially 1 scenario will be there and then user can add 4 scenarios. Maximum 5 scenarios can be added. Each scenario has a calculator icon and by clicking it, it will show a calculator popup.
If user clicks calculator icon on a particular scenario, it should show only particular scenario's calculator popup; and if user clicks close, it should close the popup.
I'm passing scenarios.Id in showCalculatorPopup(scenarios.Id) and CloseCalculatorPopup(scenarios.Id) and with scenarios.Id i'm trying to open and close particular scenario's popup.
My issue is if I click a calculator icon in scenario 1, popup is being opened in all scenarios. I tried using eval(), window[] to no avail.
Updated the code.
Instead of Switch Case i'm using
vm.showCalculatorPopup = function (ScenarioId) {
vm['calculatorPopup' + ScenarioId] = true;
}
Added break;. Even after adding break it's not working properly. When i open and close calculator for 1st time in scenario 1 then its working fine.
But when i add scenario 2 and click calculator icon, its shows the popup and when i click close in scenario 1 popup it close the scenario 2 popup.
Can't use index because button is inside the Licenceplates and its inside the Sections and its inside the Scenarios.
<div data-ng-app="ang" data-ng-controller="InputController as input">
<div data-ng-repeat="scenarios in input.model.Scenarios"> <!-- Scenario Id = 1, 2, 3, 4, 5 -->
<div data-ng-repeat="sections in scenarios.Sections">
<div data-ng-repeat="licensePlates in sections.LicensePlates">
<button data-ng-click="input.showCalculatorPopup(scenarios.Id)">Calculate</button>
<div data-ng-show="input.calculatorPopup{{scenarios.Id}}">
Dialog Box
<button data-ng-click="input.CloseCalculatorPopup(scenarios.Id)"></button>
</div>
</div>
</div>
</div>
</div>
Latest Angular Controller
vm.showCalculatorPopup = function (ScenarioId) {
vm['calculatorPopup' + ScenarioId] = true;
}
vm.showCalculatorPopup = function (ScenarioId) {
vm['calculatorPopup' + ScenarioId] = false;
}
Previous Angular Controller
vm.showCalculatorPopup = function (ScenarioId) {
vm.eval('calculatorPopup' + ScenarioId) = true;
// or
vm.window['calculatorPopup' + ScenarioId] = true;
vm.calculatorPopup1 = true; // working
// or
switch (ScenarioId) {
case 1:
vm.calculatorPopup1 = true;
break;
case 2:
vm.calculatorPopup2 = true;
break;
case 3:
vm.calculatorPopup3 = true;
break;
case 4:
vm.calculatorPopup4 = true;
break;
case 5:
vm.calculatorPopup5 = true;
break;
}
}
vm.CloseCalculatorPopup = function (ScenarioId) {
switch (ScenarioId) {
case 1:
vm.calculatorPopup1 = false;
break;
//case 3 ,4
case 5:
vm.calculatorPopup2 = false;
break;
}
}
Your switch cases are never breaking, so you are executing every later case.
switch (ScenarioId) {
case 1:
vm.calculatorPopup1 = true;
break;
case 2:
vm.calculatorPopup2 = true;
break;
case 3:
vm.calculatorPopup3 = true;
break;
case 4:
vm.calculatorPopup4 = true;
break;
case 5:
vm.calculatorPopup5 = true;
break;
}
You have the same problem with the other swtich. More info on this.
you could pass $index instead of scenarios.Id. finally you need to add a break statement in your switch statements
switch (ScenarioId) {
case 1:
vm.calculatorPopup1 = true;
break;
case 2:
vm.calculatorPopup2 = true;
break;
case 3:
vm.calculatorPopup3 = true;
break;
case 4:
vm.calculatorPopup4 = true;
break;
case 5:
vm.calculatorPopup5 = true;
break;
}
}
vm.CloseCalculatorPopup = function (ScenarioId) {
switch (ScenarioId) {
case 1:
vm.calculatorPopup1 = false;
break;
case 2:
vm.calculatorPopup2 = false;
break;
}
}
break stops the evaluation of your switch statement.

AngularJS: When applying custom filter to data, i get an empty output

I'm trying to apply a filter to some data that I'm pulling out of Firebase.
My HTML look like this:
<span ng-bind-html="game.rating | ratings"></span>
Note: If I remove the "| ratings" it does work, and output the original text from firebase.
But once I apply this filter, I don't get any output, it's just empty. :(
I'm totally new to both Angular, javascript and firebase. So any advice is very appreciated
'use strict';
MyApp.filter('ratings', function() {
return function(rating) {
switch(rating) {
case 1:
return "1star";
case 2:
return "2stars";
case 3:
return "3stars";
case 4:
return "4stars";
}
}
})
Have any of you seen this before?
Maybe the output is not an int? Parse it. And use a default, its good practice.
MyApp.filter('ratings', function() {
return function(rating) {
switch(parseInt(rating)) {
case 1:
return "1star";
case 2:
return "2stars";
case 3:
return "3stars";
case 4:
return "4stars";
default:
return "Not set";
}
}
})

Using switch on $_GET with if

So I'm trying basically to make it like: ?p=blabla&dep=blabla
switch($_GET['p'])
{
case 'home':
include("template/index.html");
break;
case null:
include("template/index.html");
break;
case 'roster':
include("template/roster.html");
break;
case 'about':
include("template/about.html");
break;
case 'members':
include("members/index.php");
break;
}
if(($_GET['p'] == 'about') && ($_GET['dep'] == 'hospital'))
{
include("template/hospital.html");
}
And it still includes both about.html and hospital.html when I do blablabla?p=about&dep=hospital
How can I fix this?
Just put the if statement inside the switch case.
case 'about':
if ($_GET['dep'] == 'hospital')
include("template/hospital.html");
else
include("template/about.html");
break;
This is exactly what you ask for.
First you have your switch statement. It sees there is 'about' in $_GET['p'] so it will include that script.
Afterwards you have your if and this also evaluates to true, thus it's included.
To change this:
add another if in your 'about' case.
case 'about':
if ($_GET['dep'] == 'hospital') break;
include("template/about.html");
break;
Your switch is rocessed before the line that looks for dep=hospital, so it will include about.html before it even looks for the department.
If you want to display just hospital.html, but only if p=about move the test into the case.
switch($_GET['p'])
{
case 'home':
include("template/index.html");
break;
case null:
include("template/index.html");
break;
case 'roster':
include("template/roster.html");
break;
case 'about':
if(($_GET['dep'] == 'hospital')) {
include("template/hospital.html");
} else {
include("template/about.html");
}
break;
case 'members':
include("members/index.php");
break;
}

Resources