【问题标题】:Appcelerator / Titanium Alloy - how to manipulate a string before it gets added to the viewAppcelerator / Titanium Alloy - 如何在将字符串添加到视图之前对其进行操作
【发布时间】:2023-03-16 23:49:02
【问题描述】:

我有一个钛合金视图,它输出一个TableView,每个视图都有一个图像缩略图。这是通过将 URL 传递到 ImageView 元素的 image 属性来实现的。因为它是一个由 Alloy 集合填充的 Alloy 视图,所以它为我处理数据循环:

<TableView id="brandsList" dataCollection="brands">
    <TableViewRow brandID="{brand_id}">
        <View class="vgroup">
            <ImageView height="45" width="80" id="image" image="{image}" />
            <Label id="name" text="{name}" />   
        </View>             
    </TableViewRow>
</TableView>

不过,我想在 到达上面的视图之前稍微更改该 URL 字符串。特别是我需要在 URL 中间添加一些值来改变图像质量和大小。如何捕获此字符串字符串值并进行更改?

【问题讨论】:

    标签: javascript titanium appcelerator titanium-alloy


    【解决方案1】:

    从这段代码的外观看来,您正在执行数据绑定。您可以在数据显示在视图中之前对其进行转换

    http://docs.appcelerator.com/titanium/latest/#!/guide/Alloy_Data_Binding

    <TableView id="brandsList" dataCollection="brands"  dataTransform="transformFunction">
        <TableViewRow brandID="{brand_id}">
            <View class="vgroup">
                <ImageView height="45" width="80" id="image" image="{image}" />
                <Label id="name" text="{name}" />   
            </View>             
        </TableViewRow>
    </TableView>
    

    然后在代码中

    function transformFunction(model) {
        // Need to convert the model to a JSON object
        var transform = model.toJSON();
        transform.image = /* do someting to image url string */;
        return transform;
    }
    

    【讨论】:

    • 您能否解释一下为什么我们需要将模型转换为 JSON 对象?
    • 这就是转换的工作方式,您传递的是要在 UI 中呈现的 JSON 对象,而不是主干模型
    猜你喜欢
    • 1970-01-01
    • 2013-08-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多