请问一个EXT绑定数据的问题

请问一个EXT绑定数据的问题

问题描述:

数据始终无法读出来,取出来的JSON为[count:7,items:[{"Slots":null,"Id":4,"DisplayOrder":100,"ChannelName":"6ur","ChannelDesc":"utr"},{"Slots":null,"Id":5,"DisplayOrder":100,"ChannelName":"345345","ChannelDesc":"23423"},{"Slots":null,"Id":6,"DisplayOrder":100,"ChannelName":"345345","ChannelDesc":"23423"},{"Slots":null,"Id":7,"DisplayOrder":100,"ChannelName":"345345","ChannelDesc":"23423"},{"Slots":null,"Id":8,"DisplayOrder":100,"ChannelName":"345345","ChannelDesc":"23423"},{"Slots":null,"Id":9,"DisplayOrder":100,"ChannelName":"345345","ChannelDesc":"23423"},{"Slots":null,"Id":10,"DisplayOrder":1,"ChannelName":"ertert","ChannelDesc":"rgdg"}]]

Ext.define('local', {
extend: 'Ext.data.Model',
fields: ['ChannelName', 'ID', 'ChannelDesc', 'DisplayOrder', 'Slots']
});

Ext.define('Gotogether.Advertisement.ChannelList', {
extend: 'Ext.panel.Panel',
alias: 'widget.channellist',
title: '频道',
collapsible: true,
animCollapse: true,
margins: '5 0 5 5',
layout: 'fit',
initComponent: function () {
Ext.apply(this, {
items: [{
xtype: 'dataview',
trackOver: true,
store: returnStore(),
model: 'local',
itemSelector: '.feed-list-item',
overItemCls: 'feed-list-item-hover',
tpl: '

{ChannelName}
',
listeners: {
selectionchange: this.onSelectionChange,
scope: this
}
}]
});
this.callParent(arguments);
},
onSelectionChange: function (selmodel, selection) {
    var selected = selection[0],
        button = this.down('button[action=remove]');
    if (selected) {
        button.enable();
    }
    else {
        button.disable();
    }
}

});

function returnStore() {
var proxy1 = new Ext.data.HttpProxy({
type :'ajax',
method: 'get',
url: '../../handler/ChannelHandler.ashx?method=listChannel'
});
var reader1 = new Ext.data.JsonReader({
count : 'count',
root: 'items'
},
[
{ name: 'ChannelName', mapping: 'ChannelName' },
{ name: 'ID', mapping: 'ID' }
]
);
// 构建Store

var store = new Ext.data.Store({
proxy: proxy1,
reader: reader1
});
// 载入
return store;
}

[quote]
用你写的代码可以,不过我想知道我写的哪里出了问题
[/quote]
没办法复原你的执行情况,我也很难知道原因。还有你页面加载后,reader的请求是否发出呢,是不是没有设置“autoLoad”的原因啊?

报错还是怎样?

[code="java"]
[count:7,items:[{"Slots":null,"Id":4,"DisplayOrder":100,"ChannelName":"6ur","ChannelDesc":"utr"},{"Slots":null,"Id":5,"DisplayOrder":100,"ChannelName":"345345","ChannelDesc":"23423"},{"Slots":null,"Id":6,"DisplayOrder":100,"ChannelName":"345345","ChannelDesc":"23423"},{"Slots":null,"Id":7,"DisplayOrder":100,"ChannelName":"345345","ChannelDesc":"23423"},{"Slots":null,"Id":8,"DisplayOrder":100,"ChannelName":"345345","ChannelDesc":"23423"},{"Slots":null,"Id":9,"DisplayOrder":100,"ChannelName":"345345","ChannelDesc":"23423"},{"Slots":null,"Id":10,"DisplayOrder":1,"ChannelName":"ertert","ChannelDesc":"rgdg"}]]
[/code]
我怎么看着你这个json数据格式不对呢,最外边不是应该是大括号的么?

[code="java"]
var store = new Ext.data.Store({
model : 'local',
proxy: proxy1,
reader: reader1
});
[/code]
你这个store,是不是应该把上面定义的model设置进去呢!

[quote]可惜没数据[/quote]
我觉得应该还是你json格式可能有问题,你返回的json应该把“count”属性加上,再试试。

:wink:

[code="java"]
var reader1 = new Ext.data.JsonReader({
count : 'count',
root: 'items'
},
[
{ name: 'ChannelName', mapping: 'ChannelName' },
{ name: 'ID', mapping: 'ID' }
]
);
[/code]
这个写法不对吧,JsonReader的构造函数怎么会传两个参数呢?你改成:
[code="java"]
var reader1 = new Ext.data.JsonReader({
count : 'count',
root: 'items',
model: 'User'
}
);
[/code]
这样试试。

把model名字改成你的“local”,不是‘User’

[code="java"]
Ext.require(['*']);

Ext.define('Channel', {
extend: 'Ext.data.Model',
fields: ['ChannelName', 'ID', 'ChannelDesc', 'DisplayOrder', 'Slots']
});

Ext.define('Gotogether.Advertisement.ChannelList', {
extend: 'Ext.panel.Panel',
alias: 'widget.channellist',
title: '频道',
collapsible: true,
animCollapse: true,
margins: '5 0 5 5',
layout: 'fit',
initComponent: function () {
var me = this ,
store = Ext.create('Ext.data.Store', {
model: 'Channel',
proxy: {
type: 'ajax',
url : 'channels.json',
reader: {
type: 'json',
root: 'items'
}
},
autoLoad: true
});

    Ext.apply(me, {
        items: [{
            xtype: 'dataview',
            trackOver: true,
            store: store,
            model: 'Channel',
            itemSelector: '.feed-list-item',
            overItemCls: 'feed-list-item-hover',
            tpl: '<tpl for="."><div class="feed-list-item">{ChannelName}</div></tpl>',
            listeners: {
                selectionchange: me.onSelectionChange,
                scope: me
            }
        }]
    });
    me.callParent(arguments);
},

onSelectionChange: function (selmodel, selection) {
    var selected = selection[0],
        button = this.down('button[action=remove]');
    if (selected) {
        button.enable();
    }
    else {
        button.disable();
    }
}

});

Ext.onReady(function() {
Ext.create('Gotogether.Advertisement.ChannelList' , {renderTo : Ext.getBody()});
});
[/code]
我用chanels.json文件测试了一下,上面的代码是可以的。