我正在用appcelerator-studio构建一个应用程序。
应用程序以编程方式创建一个包含以下代码的表:
controller.js
var rowData = [];
var header = Alloy.createController('result_row_header',{
examination:Titanium.Locale.getString(lang+"examination"),
start_date:Titanium.Locale.getString(lang+"start_date"),
end_date:Titanium.Locale.getString(lang+"end_date")
});
var viewHeader = header.getView();
//DISEGNO LA TESTATA
rowData.push(viewHeader);
$.table.setData(rowData);
function set_fields(lang) {
header.changeLanguage(lang);
}
result_row_header.js
var args = arguments[0] || {};
$.examination.text = args.examination;
$.start_date.text = args.start_date;
$.end_date.text = args.end_date;
function changeLanguage(lang){
$.examination.text=Titanium.Locale.getString(lang+"examination");
}
现在我想从controller.js调用changeLanguage方法但是如果我尝试执行方法set_fileds(来自controller.js)我有一个错误。
答案 0 :(得分:1)
您的代码稍有变化:
<强> result_row_header.js 强>
var args = arguments[0] || {};
$.examination.text = args.examination;
$.start_date.text = args.start_date;
$.end_date.text = args.end_date;
// this is the code to access function of a controller from any other
$.changeLanguage = function (lang){
$.examination.text=Titanium.Locale.getString(lang+"examination");
};
其余代码保持不变。
答案 1 :(得分:0)
让我们说这是你的view.js文件:
$.yourMethod = function() {
return 'first';
};
$.view.yourMethod = function() {
return 'second';
};
这是你的view.xml:
<Alloy>
<View id="view">
<Label text="test view"/>
</View>
</Alloy>
你这样称呼它:
var view = Alloy.createController("view",{});
view.yourMethod(); //will return first
win.add(view.getView());
但是如果你在创建方法中获得了视图,你可以这样称呼它:
var view = Alloy.createController("view",{}).getView();
view.yourMethod(); //will return second
win.add(view);
Titanium Alloy Controller Constructor doesn't exposer functions