繁体   English   中英

sencha touch将变量添加到模板/ List / etc.

[英]sencha touch add variables to templates/List/ etc

环顾四周,我找不到Sencha Touch的解决方案(虽然我设法让这个与ExtJS合作)所以我转向你。

Sencha touch的新手我学会了如何正确地将stor绑定到List组件。

我的问题是我需要在将其转换为模板之前处理特定记录。

我的记录coordinate是这样的:

2.356095,48.879154,0.000000

使用ExtJS,我创建了一个将其拆分并呈现GMaps链接的函数。

我的问题:

我可以像在extJS( record.data.coordinates )中那样访问我的记录吗?

如何添加要在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
            );
        }
    }
});

谢谢你的帮助,

朱利叶斯。

您可以在模板中使用内联javascript代码来分割坐标变量。 以下是文档中的示例:

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>'
 );

注意如何处理values.company.toUpperCase() 因此,要获得变量,您可以执行values.coordinates

另一种解决方案是具有模板成员功能。 这是sencha文档的另一个例子。

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;
        }
    }
);

如果您需要更多帮助,请查看此处的文档http://docs.sencha.com/touch/1-1/#!/api/Ext.XTemplate

在此代码之前定义tpl对象。

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

    }); 

因为你提到你知道如何使它在ExtJS中工作,我假设你知道Ext.XTemplate的功能。 因此,对于itemTpl,只需使用XTemplate。 像这样的东西:

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

并在itemTPl中使用此tpl:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM