blazor inclide code blocks glitching #{} errors then {} scope errors - scope

When I don't use # and just use {}, the scope breaks.
When I use #{} for the inline code, the scope works, but it errors because of the #
Any suggestions? Sure looks like a glitch to me. Go Blazor!
Here's a third screenshot where the brackets aren't breaking scope.

Nothing to do with Blazor.
The var ticket is scoped by the { } block. The if(ticket == null) part is outside that block.
If you had posted the code as text and not as pictures I would have fixed it here.

it actually worked with no brackets. The brackets were messing with the scope, when I thought they were required for in line code.

Related

is there a better way of initializing correctly scoped variables inside if statements

I'm curious to know if there is there a prettier way of correctly scoping objects that are initialised inside of if statements
say you have the following script:
if (x){
create object_A
}
print(object_A)
the above will fail because object A is out of scope for the print so the obvious thing to do is initialise a null variable before the if statement like this:
variable_to_reference_object_A = null
if (x){
variable_to_reference_object_A = create object_A
}
print(variable_to_reference_object_A)
This just always makes me feel like a silly billy because I'm basically saying is:
variable_to_reference_object_A = i haven't a clue because i haven't created it yet
very curious to see what you guys have to say about this, as id love to know if I'm completely doing things wrong (and possibly why it seems to show up in almost all languages). TIA

Powershell V2 vs V4 scoping differences

I am having a hard time with the literature on this. I am hoping someone can explain the difference here so that I can better understand the flow of my scripts.
function select-bin {
$objForm = New-Object System.Windows.Forms.Form
$objForm.Text = "Select a Bin"
$objForm.Size = New-Object System.Drawing.Size(300,200)
$objForm.StartPosition = "CenterScreen"
$x = #()
# Create $OKButton and $objListBox ... removed code as not relevant.
$OKButton.Add_Click({
$x+=$objListBox.SelectedItems
$objForm.Close()
})
$objForm.ShowDialog()
if ($x) {
return $x
}
else {
return $null
}
}
In the code sample above, it works great in Powershell V2, however in V4 the add_click section doesn't work. It successfully closes the form (created in the functions scope) but fails to update $x.
So I guess here are my questions.
In V2, was the add_click section considered in the same scope as the function? (only way I see it having been able to update $x)
What is the proper way to have an event like this alter data? I feel like declaring $x in the global scope is a bit much seeing as I only need it in the function.
In V4 what scope is add_click running in? It is clearly different from what it was in V2, but is it running in the global? is it relative to the $OKButton or the function? I am assuming its a child of either the global or the function but I truly do not know.
Any clarity that anyone could offer would be greatly appreciated. I have a lot of updating to do before my company moves to V4, seeing as I have not been following best practices for scoping (my bad).
In V2, a ScriptBlock, when converted to a delegate, would run dot sourced in whatever scope happened to be the current scope.
Often, this was the scope that created the script block, so things worked naturally. In some cases though, the scope it ran in had nothing to do with the scope it was created in.
In V4, these script blocks run in their own scope - a new scope that is the child of the current scope, just as they were a function and you called the function normally (not dot sourcing.)
I think your best bet is to use one of the following (in roughly best to worst):
$script:x
$x = Get-Variable -Scope 1 -Name x
$global:x

Passing variable from jade to ng-init not working

I'm trying to pass an object from jade to ng-init in angular
This: doesn't work:
ng-init='tables=!{JSON.stringify(tables)}'
This: expands but,
ng-init='tables=#{JSON.stringify(tables)}'
the output is unescaped and filled with "s
ng-init="tables={"12":{"id":....
and the view isn't updated in either of the cases. This article implies that first one should work, but like I said, it doesn't even expand,
ng-init='tables=!{JSON.stringify(tables)}'
in source code shows up exactly the same in the HTML source
ng-init='tables=!{JSON.stringify(tables)}'
Actually, the #{...} approach seems to work fine.
It is probably the way console.log prints attributes' values that confused you.
ng-init="tables=#{JSON.stringify(tables)}"
Take a look at this short demo.
In what use-case you want to pass data directly from Jade to angular? I think you could to this job in controller like this :
$scope.init = function () {
// init stuff
}
...and in your view :
ng-init = init()

Nodejs and implicit global variables?

I'm using some external libraries intended to be used in a browser and they set global variables implicitly like a='a' (without the var).
It seems like when I require certain scripts that do this, sometimes the variable will be accessible outside its scope just like in a browser, but for other scripts the global variable is not accessible outside its own script.
Anyone know how nodejs handles implicit global variables, and why I'm seeing somewhat random behavior? I found surprisingly little on the internet.
I can go into the scripts. write something like
if(typeof exports !== 'undefined' && this.exports !== exports){
var GLOBAL=global;
}
else{
var GLOBAL=window;
}
and then change all implicit references to GLOBAL.reference but these scripts are not my own and every time I want to get the latest version of them I would have to do this over again, which is clearly not desirable.
Using module.exports would be cleaner because then I don't have change all the references, but just add a section of the top of every file that exports the globals, but my original question about how node handles implicit globals is still relevant
I am not sure if this answer will help you, since it is hard to diagnose what is going on with your code, but maybe, some of this reasonings can help you diagnose the actual problem in your code.
The behavior in node is actually similar to that of the browser. If you would declare a variable without the var keyword the variable will be accesible through the global object.
//module foo.js
a = 'Obi-wan';
//module bar.js
require('./foo');
console.log(global.a); //yields Obi-wan
console.log(a); //yields Obi-wan
It is not clear why you say this behavior is not consistent in your code, but if you think about it, the use of global variables is precisely subject to this kind of problems since they are global and everyone could overwrite them at any time, causing as a result this unexpected conditions.
There is one aspect in which node is different from the browser though and that could be affecting the behavior that you see.
In the browser, if you do something like this directly in a JavaScript file:
console.log(this==window); //yields true
But if you do the same thing in a Node.js module:
console.log(this==global); //yields false
Basically, in the outer scope of a Node.js module the this reference points to the current module.exports object.
console.log(this==exports); //yield true
So, chances are that if you are putting data in the global scope (window) in the browser through the use of this, you may end up with a module scope in Node.js instead.
Interestingly, the code inside a function in Node.js behaves pretty much as in the browser, in terms of the use of the global scope.
(function what(){
console.log(this==global); //yields true
})();
This does not directly answer your question but it provides a solution since I don't think it is possible.
I love regexp. They are so powerful:
js = js.replace(/^(\t|\s{4})?(var\s)?(\w+)\s=/gm, function () {
if (arguments[1] || arguments[2]) return (arguments[1] || '') + (arguments[2] || '') + arguments[3] + ' =';
return 'exports.' + arguments[3] + ' =';
});*
JSFiddle here
How does it work? I will retrace my work:
/(\w+)\s=/g will take any var, return 'exports.' + arguments[1] + ' ='; will turn them into an export. Not very good.
/(var\s)?(\w+)\s=/g will take any var, but in the callback we examined first group (var\s). Is it undefined? Then we should export it, else nothing should happen. But what about scopes?
/^(\t|\s{4})?(var\s)?(\w+)\s=/gm now we use indent to determine the scope :)
You should be able to run this regex on your file. Be careful, you need it properly indented, and be aware that I might have forgotten some things.
Ah, the problem was that the global variable that was being declared globally in the browser wasn't being declared via a='a', but with var a='a'. In a browser if the var keyword is used not inside a function it will still declare a global variable. It only declares a local variable if the var keyword is inside a function. Node.js doesn't behave this way, and all var declarations are considered local.
Its ashame node.js does this, it makes it less compatible with browser scripts, for no real reason. (other than allowing people not to have to wrap all their scripts in a function).

Remove Single Metaclass Method

I've been starting to learn Groovy and am currently looking at the metaclass functionality. I have seen the examples of adding a new method, and removing all methods, but nothing about removing a single method. For example:
String.metaClass.foo = {delegate.toUpperCase()}
String.metaClass.bar = {delegate.toLowerCase()}
with the obvious side-effects. Now I have seen that you can say
String.metaClass = null
To remove all of the methods. I would expect one could say something along the lines of
String.metaClass.foo = null
to remove String.foo(), but have String.bar() remain, however this statement does not seem to have any effect. Is there a way to say method foo() should no longer be defined, without effecting bar() or any other added methods?
If you search this webpage for "remove method" it says that you should be able to remove a method using the exact syntax you've proposed above. But I tested it, and you're right, it doesn't seem to work.
A workaround is to assign a closure that throws MissingMethodException, which is what happens by default when you call a method that doesn't exist, e.g.
// Add method
String.metaClass.foo = {delegate.toUpperCase()}
// Remove method
def removeMethod = {throw new MissingMethodException()}
String.metaClass.foo = removeMethod
Admittedly, this is not the most pleasing solution.
As a followup, I posted a bug report here:
https://issues.apache.org/jira/browse/GROOVY-4189
And the documentation has been changed now
See the bug report for the reason this was never implemented
Don's answer is the best way around this

Resources