Mathieu
Mathieu

Reputation: 1082

sencha touch add variables to templates/List/ etc

Looking around I found no solution for Sencha Touch (while I managed to get this working with ExtJS) so I am turning to you.

New to Sencha touch I learned how to correctly bind a stor to a List component.

My problem is that I need to work on a particular record before rending it to template.

My record coordinate is like this :

2.356095,48.879154,0.000000

With ExtJS I created a function that split it and render a link to GMaps.

My Questions :

Can I access my record as in extJS (record.data.coordinates) ?

How can I add new variables to use in itemTpl ?

MyAPP = new Ext.Application({
    name: 'ListDemo',
    launch: function() {

        MyAPP.listPanel = new Ext.List({
            id: 'indexlist',
            store: MyAPP.ListStore,
            itemTpl: '<div>{name}<br>{coordinates}</div>'

        }); // end of MyAPP.listPanel

        function renderMap(value, p, record) {
            var split = record.data.coordinates.split(',');
            var lat = split[0];
            var lon = split[1];
            return Ext.String.format(
                '<b><a href="http://maps.google.com/maps?q={2}+{1}+({3})" target="_blank">Google Map</a>',
                value,
                lat,
                lon,
                record.data.Adresse
            );
        }
    }
});

Thanks for your help,

Julius.

Upvotes: 4

Views: 6478

Answers (2)

ilija139
ilija139

Reputation: 2974

You can have inline javascript code in your template that will split the coordinates variable. Here is example from the docs:

var tpl = new Ext.XTemplate(
    '<p>Name: {name}</p>',
    '<p>Company: {[values.company.toUpperCase() + ", " + values.title]}</p>',
    '<p>Kids: ',
    '<tpl for="kids">',
        '<div class="{[xindex % 2 === 0 ? "even" : "odd"]}">',
        '{name}',
        '</div>',
    '</tpl></p>'
 );

Notice how the values.company.toUpperCase() is handled. So to get to your variable you can do values.coordinates.

Another solution is to have a template member function. Here is another example from the sencha docs.

var tpl = new Ext.XTemplate(
    '<p>Name: {name}</p>',
    '<p>Kids: ',
    '<tpl for="kids">',
        '<tpl if="this.isGirl(name)">',
            '<p>Girl: {name} - {age}</p>',
        '</tpl>',
         // use opposite if statement to simulate 'else' processing:
        '<tpl if="this.isGirl(name) == false">',
            '<p>Boy: {name} - {age}</p>',
        '</tpl>',
        '<tpl if="this.isBaby(age)">',
            '<p>{name} is a baby!</p>',
        '</tpl>',
    '</tpl></p>',
    {
        // XTemplate configuration:
        compiled: true,
        // member functions:
        isGirl: function(name){
           return name == 'Sara Grace';
        },
        isBaby: function(age){
           return age < 1;
        }
    }
);

Check the docs here http://docs.sencha.com/touch/1-1/#!/api/Ext.XTemplate if you need more help.

Define the tpl object before this code.

 MyAPP.listPanel = new Ext.List({
        id: 'indexlist',
        store: MyAPP.ListStore,
        itemTpl: tpl  // use the tpl object like this

    }); 

Upvotes: 4

Swar
Swar

Reputation: 5503

Because you mentioned you know how to make it work in ExtJS, I assume that you know the functionality of Ext.XTemplate. So, for itemTpl just use XTemplate. Something like this:

var tpl = new Ext.XTemplate('<div>{name}<br>{coordinates}</div>', {
               // XTemplate methods here
          });

And use this tpl in itemTPl:

MyAPP.listPanel = new Ext.List({
        id: 'indexlist',
        store: MyAPP.ListStore,
        itemTpl: tpl
    });

Upvotes: 0

Related Questions