javascript - Appcelerator titan - 传递js变量

标签 javascript model-view-controller scope titanium appcelerator

我是 JS 和 Appcelerator Titanium 的新手。我正在尝试在我的应用程序中实现 MVC 模型,但访问模型中的数据时遇到问题:

在 model.js 中:

var my_val;

然后在file1.js中,我修改了my_val的值:

Ti.include("model.js");
my_val=5;

然后在另一个文件file2.js中:

Ti.include("model.js");
T.API.info(my_val); // the value I always get is "undefined"

为什么 file2.js 没有检测到 file1.js 对 my_val 所做的更改?有什么办法让它发挥作用吗?

最佳答案

看看我关于这个特定问题的博客文章。

blog.clearlyinnovative.com

您希望将这两个文件包含在 app.js 中并将变量添加到您的命名空间中;这更干净,不会污染全局范围。

var myApp = {};
Ti.include("model.js");
Ti.include("file2.js");

在 model.js 中做类似的事情

var myApp.model = {};
myApp.model.my_val = 100;

在 file2.js 中做这样的事情;无需再次包含 model.js,该值已在您自己的命名空间中设置并且已明确定义

Ti.API.info(myApp.model.my_val);

关于javascript - Appcelerator titan - 传递js变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6327026/

相关文章:

javascript - Fanytree Init 上的事件节点

javascript - 从javascript中的字符串中替换特殊字符和空格

java - Knock Knock 带有服务器和 UI 的应用程序

java - 在拦截器的 Spring MVC Controller 方法上使用自定义注释

model-view-controller - 如何使用属性路由使 ASP.NET MVC 5 区域显示为站点根目录

scope - Sublime Text : scope selector operators

php - 如何使用来自其他 namespace 的对象以及如何在 PHP 中导入 namespace

javascript - D3 - 如何从 xAxis 中删除逗号分隔符?

javascript - Nodejs 从请求范围内检索正文

python - 意外范围行为的文档