1

我搜索并找不到答案,所以这是我的疑问,

是否可以获取对象并复制它?例如,我的视图中有此代码(.xml):

<View id="endereco1" class="" layout="horizontal" height="60dp" >   
  <Label class="input-left-label label-font-size label-text-align-right">CEP: </Label>
  <TextField class="textArea"></TextField>
</View>

然后在我的 Js 上,我想获取这个 $.endereco1,并创建和 $.endereco2,只更改 id,是否有可能,比如实例化写在视图上的对象?

先感谢您

4

2 回答 2

2

我的建议是您将该对象用作控制器,然后当您想要获取重复的控制器时,在创建方法中发送相同的参数:

var copy = Alloy.createController('view',argsUsedOnFirst).getView();

或者创建一个返回第一个对象的函数:

var copy = function(propertiesUsedOnFirst) {

    return Ti.UI.createView(propertiesUsedOnFirst);
};
于 2016-01-27T14:46:23.147 回答
-1

underscore.js 内置在钛 sdk 中。为什么不简单地使用

clone 

_.clone(object) 
Create a shallow-copied clone of the provided plain object. Any nested objects or arrays will be copied by reference, not duplicated.

_.clone({name: 'moe'});
=> {name: 'moe'};
于 2016-01-27T21:54:17.443 回答