钛合金过滤器功能 - 如何访问我正在迭代的视图

时间:2014-02-14 13:53:08

标签: javascript titanium appcelerator titanium-alloy

我在Titanium Alloy中有一个视图,它循环显示集合中的视频列表,如下所示:

<View dataCollection="videos" dataFilter="recentlyAddedFilter">
    <ImageView image="{img}" width="200">
          <Label text="{title}"></Label>  
    </ImageView>
</View>

我有一个名为recentlyAddedFilter的过滤函数,在我的控制器中定义如下:

function recentlyAddedFilter(collection) {
    return collection.where({title:'A title'});
}

我要创建这个view组件的多个版本,所以我希望过滤器函数能够访问它所应用的每个组件,就像我可以使用{{1在filter函数里面。如果我能做到这样的话会很棒:

collection

我有办法做到这一点吗?

1 个答案:

答案 0 :(得分:2)

而是使用dataTransform函数将属​​性添加到用于构造视图的JSON,然后使用添加的自定义属性,像任何其他属性一样调用它。例如:

<View dataCollection="videos" dataFilter="recentlyAddedFilter" 
                              dataTransform="recentlyAddedTransform">
    <!-- Pass the customWidth attribute that was added in the transform function -->
    <ImageView image="{img}" width="{customWidth}">
          <Label text="{title}"></Label>  
    </ImageView>
</View>

所以你的控制器看起来像这样:

// Takes a collection
function recentlyAddedFilter(collection) {
    return collection.where({title:'A title'});
}

// Takes a model
function recentlyAddedTransform(model) {
    // Make sure to convert the model to JSON
    var transform = model.toJSON();
    // Add a custom width attribute to the model
    transform.customWidth = "200dp"; 
    return transform;
}

请记住,在dataTransform函数中,您正在使用单个模型,但是您将返回JSON。