Lightswitch HTML全局JS文件传递变量 [英] Lightswitch HTML global JS file to pass variable
问题描述
我知道这在C#中是如何工作的,但是在javascript中却不是很多,所以我希望它是相似的.
我可以使用Javascript创建带有变量(var defaultValue = "1234"
)的master.js,我可以在与该项目相关的所有其他javascript文件中引用该变量吗?
因此就Lightswitch HTML而言,每个屏幕都具有一个js文件的能力,并且在屏幕上我希望能够检索此defaultValue.
- 可以做到吗?
- 如果是,如何在当前屏幕上获取此值?
到目前为止,我已经创建了一个main.js文件,并添加了以下功能:
function getDefaultValue(value) {
var value = "1234";
return value;
}
并在default.htm文件中声明了js文件:
<script type="text/javascript" src="Scripts/main.js"></script>
我知道这是我使用其他JavaScript文件的方式,例如blob.js,lsWires.js等...
在screen.js中使用此方法不起作用,因此这些阶段之一正在导致错误...
window.alert(main.getDefaultValue(value));
理想情况下,我想使用此默认值来设置值,即var test = main.getDefaultValue(value)
当然可以,并且您在default.htm中使用的脚本声明似乎正确.
但是,当您描述的方法创建一个全局getDefaultValue函数(添加到全局窗口对象上下文中)时,您将不会像在c#中那样指定主命名空间"前缀.
您可以在LightSwitch屏幕中使用以下方法,而不是使用main.getDefaultValue调用函数:
myapp.BrowseProducts.created = function (screen) {
window.alert(window.getDefaultValue("123")); // This will display 1234
// As window is a global object, its window prefix can be omitted e.g.
alert(getDefaultValue("123")); // This will display 1234
};
或者,如果您想在main.js中定义一个全局defaultValue变量(可能是您要实现的方法),则main.js文件中将包含以下代码:
var defaultValue = "5678";
然后,您将在LightSwitch屏幕中按以下方式访问它:
myapp.BrowseProducts.created = function (screen) {
alert(defaultValue); // This will display 5678
defaultValue = "Hello World";
alert(defaultValue); // This will now display Hello World
};
此外,如果您想在主命名空间"中组织函数/属性,则可以在main.js文件中使用以下类型的方法:-
var main = (function (ns) {
ns.getDefaultValue = function (value) {
var value = "1234";
return value;
};
ns.defaultValue = "5678";
return ns;
})(main || {});
然后在LightSwitch屏幕中将这些调用如下:-
myapp.BrowseProducts.created = function (screen) {
alert(main.getDefaultValue("123")); // This will display 1234
alert(main.defaultValue); // This will display 5678
main.defaultValue = "Hello World";
alert(main.defaultValue); // This will now display Hello World
};
以下帖子介绍了这种方法:-
I know how this works in C#, however not so much in javascript so I am hoping it is similar.
With Javascript can I create say a master.js, with a variable (var defaultValue = "1234"
), which I can reference in all other javascript files associated with the project?
so in terms of Lightswitch HTML, each screen has the ability to have a js file, and on the screen I want to be able to retrieve this defaultValue.
- Can this be done?
- If yes, how can I get this value onto the current screen?
so far I have created a main.js file, added this function:
function getDefaultValue(value) {
var value = "1234";
return value;
}
and declared the js file in the default.htm file:
<script type="text/javascript" src="Scripts/main.js"></script>
I know this is how i am using other JavaScript files like blob.js, lsWires.js etc...
using this method in by screen.js doesn't work so one of these stages is causing an error...
window.alert(main.getDefaultValue(value));
ideally i would like to use this defaultvalue for setting a value, i.e. var test = main.getDefaultValue(value)
This is certainly possible, and the script declaration you've used in your default.htm appears correct.
However, as the approach you've described creates a global getDefaultValue function (added to the global window object context) you wouldn't specify a main 'namespace' prefix like you would in c#.
Instead, rather than calling the function using main.getDefaultValue, you'd use the the following approach within your LightSwitch screens:
myapp.BrowseProducts.created = function (screen) {
window.alert(window.getDefaultValue("123")); // This will display 1234
// As window is a global object, its window prefix can be omitted e.g.
alert(getDefaultValue("123")); // This will display 1234
};
Or, if you want to define a global defaultValue variable in your main.js (probably the approach you're looking to implement) you would have the following code in your main.js file:
var defaultValue = "5678";
Then you'd access it as follows in your LightSwitch screens:
myapp.BrowseProducts.created = function (screen) {
alert(defaultValue); // This will display 5678
defaultValue = "Hello World";
alert(defaultValue); // This will now display Hello World
};
Also, if you'd like to organise your functions/properties in a main 'namespace', you could use the following type of approach in your main.js file: -
var main = (function (ns) {
ns.getDefaultValue = function (value) {
var value = "1234";
return value;
};
ns.defaultValue = "5678";
return ns;
})(main || {});
These would then be called as follows in your LightSwitch screens: -
myapp.BrowseProducts.created = function (screen) {
alert(main.getDefaultValue("123")); // This will display 1234
alert(main.defaultValue); // This will display 5678
main.defaultValue = "Hello World";
alert(main.defaultValue); // This will now display Hello World
};
This type of approach is covered in the following posts: -
How to span javascript namespace across multiple files?
JavaScript Module Pattern: In-Depth
这篇关于Lightswitch HTML全局JS文件传递变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!