【问题标题】:Titanium Alloy Filter Function - How do I access the view that I'm iterating over钛合金过滤器功能 - 如何访问我正在迭代的视图
【发布时间】:2014-02-14 13:53:07
【问题描述】:

我在 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 组件的多个版本,所以我希望过滤器函数能够访问它所应用的每个单独的组件,就像我可以在内部使用collection 一样过滤功能。所以如果我能做这样的事情那就太好了:

function recentlyAddedFilter(collection) {
    theComponent.width = "200dp"; // change property on component that calls this function
    return collection.where({title:'A title'});
}

有没有办法可以做到这一点?

【问题讨论】:

    标签: javascript titanium appcelerator titanium-alloy


    【解决方案1】:

    改为使用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。

    【讨论】:

      猜你喜欢
      • 2013-10-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多