简体   繁体   English

在Windows 8应用程序中将变量从一个Javascript文件传送到另一个Javascript文件?

[英]Carrying a variable from one Javascript file to another Javascript file in a Windows 8 App?

Currently pushes hard coded '/video/vid1.MP4' into the list and this URL is diplayed for all items. 当前将硬编码的“ /video/vid1.MP4”推入列表,并且该URL对所有项目均不显示。 I want to display the variable I have defined in my Javascript2 under 'content'. 我想在“内容”下显示我在Javascript2中定义的变量。

HTML: HTML:

<video id=vid src='#' data-win-bind="src: videoUrl" />

JavaScript1: JavaScript1:

        for (var i = 0; i <= this._items.length; i++) {
            list.push({ videoUrl: '/video/vid1.MP4' });
        }

This is used to generate data in different views! 这用于生成不同视图中的数据! I want to call the variable stored in content into the javascript below under videoUrl. 我想将存储在内容中的变量调用到videoUrl下的以下javascript中。

JavaScript2: JavaScript2:

var video = "/video/vid.mp4";    
     var sampleItems = [
                    { group: sampleGroups[0], content: video},
                    { group: sampleGroups[0],content: video},
                    { group: sampleGroups[0], content: video}
     ];

Essentially I want to use'content' rather then a hard coded url. 本质上,我想使用“内容”,而不是硬编码的网址。 I want to carry information from one JavaScript file to a different one. 我想将信息从一个JavaScript文件传送到另一个文件。

eg. 例如。 list.push({ videoUrl: content }); list.push({videoUrl:content});

Are you attempting to push each item from sampleItems array into a new array called list? 您是否正在尝试将每个项目从sampleItems数组推入名为list的新数组中?

if so: 如果是这样的话:

for (var i = 0; i <= this._items.length; i++) {
    list.push({ videoUrl: this._items[i].content });
}

however if this._items is not the same thing as sampleItems you can access the items inside sampleItems like you would any array. 但是,如果this._itemssampleItems ,则可以像访问任何数组一样访问sampleItems内部的项目。 sampleItems[0] gives you the first object in the array. sampleItems[0]为您提供数组中的第一个对象。 In this case it is: 在这种情况下是:

{ group: sampleGroups[0], content: "/video/vid.mp4"}

This object has two properties 'group' and 'content' which can be accessed like so: 该对象具有两个属性“ group”和“ content”,可以像这样访问:

sampleItems[0].content

EDIT: 编辑:

You also need to fix your for loop it so doesn't attempt to access the array at an index larger than its length. 您还需要修复它的for循环,以便不要尝试以大于其长度的索引访问数组。

for (var i = 0; i < this._items.length; i++) {

If I understand this correctly, you're new to JavaScript and aren't familiar with out variable scoping works. 如果我正确理解这一点,那么您是JavaScript的新手,也不熟悉变量范围界定的工作。

By default, anything you define outside of a function scope is added to the global namespace, and available to JavaScript from any file in your project (so long as it's running in the same script context - so not in an iframe/webview or a WebWorker). 默认情况下,您在函数范围之外定义的所有内容都会添加到全局名称空间,并且可从项目中的任何文件中的JavaScript使用(只要它在同一脚本上下文中运行-而不是在iframe / webview或WebWorker中运行) )。

However, the best (and most common) practice is to create a file scope for each JS file using the module pattern. 但是,最佳(也是最常见)的做法是使用模块模式为每个JS文件创建文件范围。 The WinJS templates do this, so you may have followed this pattern without knowing it. WinJS模板会执行此操作,因此您可能不了解它就遵循了这种模式。 If you did, your file probably looks like: 如果这样做,您的文件可能看起来像:

(function () {
// All your code
})();

The result is that all variables and functions in the "all your code" section are scoped to this file. 结果是“所有代码”部分中的所有变量和函数都作用于该文件。 This is A Good Thing. 这是一件好事。

When you want to expose something outside of this file scope, you need to attach it to the global scope somewhere so that it can be accessed by code running elsewhere. 当您要在此文件作用域之外公开某些内容时,需要将其附加到全局作用域的某个位置,以便其他地方运行的代码可以访问它。 The simplest way to do this is to attach it to the global "window" object. 最简单的方法是将其附加到全局“窗口”对象。 So, you could write: 因此,您可以编写:

window.myVideoList = list;

Then myVideoList will be available everywhere (with or without the explicit "window." in front). 然后,myVideoList将随处可用(无论前面有没有显式的“窗口”。)。

A better pattern for this kind of thing, though, is to define namespaces. 但是,针对这种情况的更好模式是定义名称空间。 These are really just objects off of window which in turn have more objects (or more namespace objects) attached. 这些实际上只是窗口之外的对象,这些对象又附加了更多对象(或更多命名空间对象)。 Since it looks like you're using the WinJS library, you can use the WinJS.Namespace facility for this. 由于看起来您正在使用WinJS库,因此可以使用WinJS.Namespace工具。 For example, at the end of your first file (but inside the enclosing function scope), you can write: 例如,在第一个文件的末尾(但在封闭函数范围内),您可以编写:

WinJS.Namespace.define("DataModel", {
    videoList: list,
});

Then in your other file you write: 然后在另一个文件中编写:

{ content: DataModel.videoList[0] } or whatever.

Or, alternatively, you could expose a "getVideoList" function that builds and returns it, or whatever. 或者,您也可以公开一个“ getVideoList”函数来生成并返回它,或者其他。

Hope that helps! 希望有帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在另一个 javascript 文件中引用变量 - Referencing variable from one javascript file in another 将变量从一个javascript文件传递到另一个javascript文件 - pass a variable from one javascript file to another javascript file 将变量从一个 javascript 获取到另一个 php 文件中的另一个 javascript - Get variable from one javascript to another javascript in another php file 有没有办法在javascript中将变量或函数从一个文件获取到另一个文件? - Is there a way to get a variable or function from one file to another file in javascript? 从一个php文件导航到另一个承载数据的数组 - Navigate from one php file to another carrying array of data 如何将变量从一个 javascript 文件传递到另一个文件? - How do I pass a variable from one javascript file to another? 如何将一个JavaScript文件中的一个变量调用到另一个JavaScript文件中? - How to call one variable in a JavaScript file to another JavaScript file? 如何将一个JavaScript文件中的变量访问/获取到另一个JavaScript文件中 - how to access/get the variable in one javascript file to another javascript file JavaScript修改一个文件中的变量并在另一个文件中读取它 - JavaScript modifying a variable in one file and reading it in another 从另一个JavaScript文件中获取javaScript文件中的变量 - Fetch Variable in a javaScript file from another JavaScript File
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM