钛移动,通过自定义循环获取数组值的最佳方式
我试图找出在自定义循环内获取数组值的最佳方法。这是我的代码,我不确定这是否是执行此操作的有效方法,或者还有其他方法:
var win = Ti.UI.createWindow({ backgroundColor: '#fff', layout:'vertical' });
var data = [
{title:'Row 1',customValue:'123'},
{title:'Row 2',customValue:'345'},
{title:'Row 3',customValue:'234'},
];
for(var i = 0, l = data.length; l--; i++) {
thisObject = data[i];
var container = Titanium.UI.createView({
left: 10,
right: 10,
customValue:thisObject.customValue
});
var label = Ti.UI.createLabel({
text : thisObject.title,
width : 'auto',
height : 25
});
container.add(label);
win.add(container);
container.addEventListener('touchend', function(e) {
alert(this.customValue);
});
}
win.open();
谢谢。
I am trying to figure out the best way to gey array value inside the the custom loop. Here is my code, I am not sure if that is a valid way of doing this or there is another way of doit it:
var win = Ti.UI.createWindow({ backgroundColor: '#fff', layout:'vertical' });
var data = [
{title:'Row 1',customValue:'123'},
{title:'Row 2',customValue:'345'},
{title:'Row 3',customValue:'234'},
];
for(var i = 0, l = data.length; l--; i++) {
thisObject = data[i];
var container = Titanium.UI.createView({
left: 10,
right: 10,
customValue:thisObject.customValue
});
var label = Ti.UI.createLabel({
text : thisObject.title,
width : 'auto',
height : 25
});
container.add(label);
win.add(container);
container.addEventListener('touchend', function(e) {
alert(this.customValue);
});
}
win.open();
Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的解决方案是可以接受的,并且在概念上与我的方法相似。但是,我建议您在必要时始终为此自定义数据使用唯一的属性名称,并允许它通过使用对象来存储许多属性及其值。如果将来 Appcelerator 决定创建一个名为
customValue
的属性,您可能会对 Titanium API 感到满意并遇到不良结果。传递/存储您的自定义数据:
像这样访问您的自定义数据对象属性:
Your solution is acceptable and is similar in concept to my approach. However, I would suggest you consistently use a unique property name for this custom data where necessary, and allow it to store many properties and their values by using an object. If in the future Appcelerator decides to create a property named
customValue
you may be contenting with the Titanium API and experience undesirable results.Passing / storing your custom data:
Accessing your custom data object property like so: