user3137766
user3137766

Reputation:

Titanium collections display

I have two questions about titanium collections :

This is my controller :

var products = Alloy.Collections.products;
products.fetch({
    success:function(_m,_r){
        var size = Alloy.Globals.half,
        width = size;
        for(var i = 1; i<=_resultats.length; i++){
            width = i%2 == 0 ? size : size-1; 
            backgroundColor = i%4 == 2 || i%4 == 3 ? Alloy.CFG.colors.lightgrey : "white";
            $.item.width = width;
    }

});

This is my View :

<ScrollView dataCollection="products" dataTransform="parse_liste" layout='vertical' id="products"> 
    <View id='item'>
        <Label text='{itemName}' />
        <ImageView image='images/star.png' id='rating' />
        <ImageView image='images/star.png' id='rating' />
        <ImageView image='images/star.png' id='rating' />
        <ImageView image='images/star.png' id='rating' />
    </View>
</ScrollView>

All items is displayed well, but i have following error :

[ERROR] :  Script Error {
[ERROR] :      column = 25;
[ERROR] :      line = 269;
[ERROR] :      message = "undefined is not an object (evaluating '$.item.width = width')";
[ERROR] :      stack = "success\nsuccess\n\nonLoad\nonload";
[ERROR] :  }

Secondly how to loop over ratings images from item ratings?, can i do a kind of a loop inside alloy tags?

Thanks for your help.

Upvotes: 0

Views: 156

Answers (2)

hini
hini

Reputation: 240

First you are trying to do collection data binding with a ScrollView and I'm not sure if that's possible out of the box.

Just do it in plain javascript. If you want to keep the item object you can put it in a separate controller and pass appropriate data. Remove xml from the body of the scrollview too.

var products = Alloy.Collections.products;
products.fetch({
    success:function(_m,_r) {
            var models = _m.models;
            models.forEach(function(model){
                // In the for loop create an item controller and pass model data
                var modelData = model.toJSON();
                Ti.API.info(JSON.stringify(modelData));
                var item = Alloy.createController('item', modelData) //pass here data for each model
                $.products.add(item.getView());
            }
        }
    }
});

Upvotes: 0

rjcpereira
rjcpereira

Reputation: 953

I think that you are missing a close brace.

But besides that, you could set the width like this, there is no need to create those 2 variables (width & size)

var products = Alloy.Collections.products;
products.fetch({
    success:function(_m,_r) {
        for(var i = 1; i<=_resultats.length; i++) {
            backgroundColor = i%4 == 2 || i%4 == 3 ? Alloy.CFG.colors.lightgrey : "white";
            $.item.width = !parseInt(i%2) ? Alloy.Globals.half : Alloy.Globals.half-1;
        }
    }
});

Upvotes: 0

Related Questions