I want to use node global variable in Spooky.js.so how I access a global variable into SpookyJS. please help me into this?
Use .bind() functionality. Example:
var globalVar = "foo";
var spooky = new Spooky({...}, function (err) { ... }.bind({globalVar: globalVar}));
Related
I'm very new in kotlin and wanted to solve following problem with a do while:
I want to create a hash and want to check if there is the same hash stored in a key-value store as a key.
In java I would make it with a String variable which I declared outside the while. But that will only work with a var in Kotlin and I learned that it is common practise to avoid var.
My code looks as following (with var...)
var hash = ""
do {
hash = createHash(longUrl)
val optional = shortUrlRepository.findById(hash)
} while(optional.isPresent)
What would you say is the best way to solve this?
thank you a lot!
Maybe something like this?
val hash = generateSequence { createHash(longUrl) }
.first { !shortUrlRepository.findById(it).isPresent }
... and of course, you can always localize var and pass it outside as val.
val someVal = run {
var someVar: String = ""
// do super logic with var
someVar
}
...
Is there a function to convert objects that have be converted into vuejs's reactive system back into a normal object?
I think you can try:
const normalObject = JSON.parse(JSON.stringify(objectWithReactivity)).
Even try a copy using ecmascript
object_reactive
let object = {...object_reactive}
or maybe just javascript
var obj = { a: 1 };
var copy = Object.assign({}, obj);
I tried to rewrite my Application.cfc and other .cfc files in my system with cfscript. There is few things that I'm not sure how they work in cfscript. I'm wondering about defining variables inside of the functions. For example onRequestStart() function looks like this:
function onRequestStart(required string thePage) returntype="boolean" output="false" {
var request.appCode = "MyApp";
var request.appName = "Single Page Application";
var page = listLast(arguments.thePage,"/");
var onApplicationStart();
if(!listFindNoCase("Home.cfm,Auth.cfc",page)){
if(structKeyExists(SESSION, "loggedin") AND SESSION.loggedin EQ false){
location(url="https://www.myapp.org", addToken="false");
}
}
return true;
}
Do I need to use var word in situations where I'm defining request/session variables? If I do what is the best practice, use var word or use local.variablename? Is local and variables same in cfscript?
var is used only for local variables. That means variables which are/should not be accessible outside the function definition. Session & Request are accessible to each session & request respectively. Putting them on var scope will give terrible results.
You can use either var or local, both have 'local' scope. Variables is the page scope and any variable defined in the Variables scope will be accessible to all functions in the CFC.
function onRequestStart(required string thePage) returntype="boolean" output="false" {
request.appCode = "MyApp";
request.appName = "Single Page Application";
var page = listLast(arguments.thePage,"/");
//this is a function call and not variable declaration.
onApplicationStart();
if(!listFindNoCase("Home.cfm,Auth.cfc",page)){
if(structKeyExists(SESSION, "loggedin") AND SESSION.loggedin EQ false){
location(url="https://www.myapp.org", addToken="false");
}
}
return true;
}
var is not equal to the <cfset> tag, i.e. you can't do a simple search & replace when switching to CFScript syntax.
var is only used for local variable definitions. This means, setting structure and array items, like request, session and other scope variables should not be prefixed by var.
Also, function calls must be written without preceding var.
local and var both refer to the local scope. Though note, as mentioned above, if you want to define variables via local.something the var keyword is also not needed.
variables, in contrary to local, refers to the page scope, which is accessible from everywhere within the component and any included pages.
For more info on the different scopes, you should read the Adobe docs.
Is there a way to set the context of the expression in Dynamic Expresso library, so that we can do something like the following:
interpreter.Eval("FirstName", new Parameter("person", new { FirstName="Homer", LastName="Simpson"}));
rather than
interpreter.Eval("person.FirstName", new Parameter("person", new { FirstName="Homer", LastName="Simpson"}));
Maybe we could have a another option that would say that the first parameter is to be used as the context for the expression.
I guess there could also be another version of Parse and Eval methods that simply takes the expression text and a simple object value that will serve as the expression context.
Other than that and the lack of support for dynamic types, I am really liking this library. I had worked on something similar, but had not added support for extension methods and generic method calls.
Thanks for the great library,
Neal
There isn't a built-in solution but you can simulate it in many ways:
Option 1: Inject an expression
var workingContext = new { FirstName = "homer" };
var workingContextExpression = Expression.Constant(workingContext);
var firstNameExpression = Expression.Property(workingContextExpression, "FirstName");
var interpreter = new Interpreter();
interpreter.SetExpression("FirstName", firstNameExpression);
Assert.AreEqual(workingContext.FirstName, interpreter.Eval("FirstName"));
Basically I inject an expression using SetExpression method. The injected expression is the property that you want to be available.
Option 2: Use this/me/it variable
You can inject a variable that will contain your working object. I usually call it this (or me or it depending on the application).
var workingContext = new { FirstName = "homer" };
var interpreter = new Interpreter();
interpreter.SetVariable("this", workingContext);
Assert.AreEqual(workingContext.FirstName, interpreter.Eval("this.FirstName"));
Option 3: A combination of the previous solutions
var workingContext = new { FirstName = "homer" };
var interpreter = new Interpreter();
interpreter.SetVariable("this", workingContext);
var firstNameExpression = interpreter.Parse("this.FirstName").LambdaExpression.Body;
interpreter.SetExpression("FirstName", firstNameExpression);
Assert.AreEqual(workingContext.FirstName, interpreter.Eval("FirstName"));
Equal to the first solution but I generate the expression using the parser itself.
Consider that all solutions assume that you must have an Interpreter instance for each context.
Disclaimer: I'm the author of Dynamic Expresso library.
Starting with DynamicExpresso v2.13.0, it's possible to define a variable named "this", that will be used for implicit resolution:
var target = new Interpreter();
target.SetVariable("this", new { FirstName="Homer", LastName="Simpson"});
// 'this' variable is used implicitly
Assert.AreEqual("Homer", target.Eval("FirstName"));
// 'this' variable can also be used explicitly
Assert.AreEqual("Homer", target.Eval("this.FirstName"));
I have the following script that is calling a text file:
/* first create a new instance of the LoadVars object */
myVariables = new LoadVars();
myVariables.load("myFile.txt");
myVariables.onLoad = function(getreading):String{
var ODOMETER2:String=myVariables.ACADEMICWATER;
return ODOMETER2;
trace (ODOMETER2);
}
trace(getreading());
The text file contains the following:
ACADEMICWATER=3002&elec=89
I am able to import the value of 3002 into the function and I can trace it. However, I Should be able to trace it outside the function using trace(getreading()); as shown on the last line. This only returns an "UNDEFINED" value. I am stumped.
You are declaring an anonymous function (see AS3 Syntax and language / Functions) which can't be referenced by name. getreading is declared in your code as an untyped parameter of this function.
If you want to trace the result of this function, then you should declare a named function like this:
function getReading(): String {
var ODOMETER2:String=myVariables.ACADEMICWATER;
return ODOMETER2;
}
myVariables.onLoad = getReading;
trace(getReading());
getreading is not the name of the function in this case, but the name of a parameter to the anonymous function that is run on the onLoad event of the myVariables object.
Place the variable ODOMETER2 outside the function and set it's value inside the anonymous function. Then you will be able to access it outside the function as well.
/* first create a new instance of the LoadVars object */
var ODOMETER2:String;
myVariables = new LoadVars();
myVariables.load("myFile.txt");
myVariables.onLoad = function(){
ODOMETER2=myVariables.ACADEMICWATER;
}
trace(ODOMETER2);
LoadVars.onLoad is an event handler. It is called by LoadVars as soon as it finishes with the asynchronous load operation. It takes a boolean argument, indicating success or failure of the operation. It does not return anything.
LoadVars.onLoad documentation
In that function, you typically act upon the data you received, like storing and processing it. Here's a very simple example showing some basic use cases:
var ODOMETER2:String;
var myVariables = new LoadVars();
myVariables.load("myFile.txt");
myVariables.onLoad = function(success) {
trace(success);
ODOMETER2 = myVariables.ACADEMICWATER;
processResults();
}
function processResults() {
trace(ODOMETER2);
trace(myVariables.ACADEMICWATER);
}
// traces:
// true
// 3002
// 3002