如何从sencha touch 2中的xml获取媒体内容属性值

时间:2012-12-15 09:39:56

标签: xml sencha-touch-2 sencha-architect

我想获得媒体:内容属性 - > “url”值,在sencha touch 2中的快照中突出显示。enter image description here

  

我的Sencha型号代码

Ext.define('MyApp.model.Feed', {
extend: 'Ext.data.Model',

config: {
    fields: [
        {
            name: 'title'
        },
        {
            name: 'link'
        },
        {
            name: 'pubDate'
        },
        {
            name: 'channel'
        },
        {
            name: 'item'
        },
        {
            name: 'content'
        },
        {
            name: 'description'
        },
        {
            name: 'category'
        }
    ],
    hasMany: {
        associationKey: 'content',
        model: 'MyApp.model.Feed',
        autoLoad: true
    }
}

});

  

Sencha STORE XMLStore

Ext.define('MyApp.store.MyXmlStore', {
extend: 'Ext.data.Store',

requires: [
    'MyApp.model.Feed'
],

config: {
    autoLoad: true,
    model: 'MyApp.model.Feed',
    storeId: 'MyXmlStore',
    proxy: {
        type: 'ajax',
        url: 'http://www.goodnews.pk/feed/',
        reader: {
            type: 'xml',
            rootProperty: 'channel',
            record: 'item'
        }
    }
}

});

  

MyList查看代码

Ext.define('MyApp.view.ListView', {
extend: 'Ext.Panel',

config: {
    id: 'ListView',
    layout: {
        type: 'card'
    },
    items: [
        {
            xtype: 'list',
            itemTpl: [
                '<div style="width:auto; float:left;"><img src="{pubDate}" width="50" height="50" /></div>',
                '<div style="width:70%; padding: 5px;">',
                '    <span><h4>{description}</h4></span>',
                '    <span>{category}</span>',
                '    <span>{content}</span>',
                '</div>'
            ],
            store: 'MyXmlStore'
        }
    ]
}

});

xml链接为www.goodnews.pk/feed/

1 个答案:

答案 0 :(得分:1)

我解决了媒体中的url链接:来自sencha架构师xml的内容解析。我在模型中只更改了一些设置,并添加了一个通过此函数转换字段的函数。

以上代码相同。只有My Sencha Model代码才有变化。代码如下。

  

我的Sencha型号代码

Ext.define('MyApp.model.Feed', {
extend: 'Ext.data.Model',

config: {
    fields: [
        {
            name: 'title'
        },
        {
            name: 'link'
        },
        {
            name: 'pubDate'
        },
        {
            name: 'channel'
        },
        {
            name: 'item'
        },
        {
            name: 'content'
        },
        {
            convert: function(v, rec) {
                var nodes = rec.raw.querySelectorAll('description');
                var arrayItem = [];
                var l = nodes.length;

                var desc = "";

                for( var i = 0; i < l; i++){
                    var node = nodes[i];

                    // now push the description in array with substring
                    desc = nodes[i].textContent;
                    console.log(desc.substring(0,100));

                    //arrayItem.push(nodes[i].textContent);
                    //console.log(nodes[i].textContent);

                    desc = nodes[i].textContent;
                    arrayItem.push(desc.substring(0,100)+'[...]');
                }

                return arrayItem;
            },
            name: 'description'
        },
        {
            name: 'category'
        },
        {
            convert: function(v, rec) {
                console.log(''+rec.raw);
                var nodes = rec.raw.querySelectorAll('content');
                console.log('length is '+nodes.length);
                var arrayItem = [];
                var l = nodes.length;
                var flag = true;


                for( var i = 0; i < l; i++){
                    var node = nodes[i];

                    //this will get the image Link
                    var imageLink = nodes[i].getAttribute('url');




                    //console.log(nodes[i].textContent);

                    if(i%2!==0)
                    {

                        // now push the image Link in array that contains all the images link
                        arrayItem.push(imageLink);
                        console.log(nodes[i].getAttribute('url'));
                    }

                }

                return arrayItem;
            },
            name: 'imageLink'
        }
    ],
    hasMany: {
        associationKey: 'content',
        model: 'MyApp.model.Feed',
        autoLoad: true
    }
}

});

我也分享我的sencha架构快照。 +控制台日志 Sencha Architect enter image description here