按特定id过滤Ext.data.Store会返回多个结果

时间:2011-03-04 14:42:31

标签: json extjs model sencha-touch

我正在尝试按节目ID过滤我的试镜列表,但是当节目ID为“1”时,所有试镜都会返回“1x”的节目ID。以下是我的代码。

试演模型:

    Ext.regModel("Audition", {

    fields: [
        {name: "id",        type: "integer"},
        {name: "show_id",   type: "integer"},
        {name: "date",      type: "string"},
        {name: "details",   type: "string"}
    ],

    belongsTo: 'Show',

    proxy: {
        type: 'ajax',
        url : 'app/data/auditions.json',
        reader: {
            type: 'json',
            root: 'auditions',
            id  : 'id'
        }
    },

});

app.stores.auditions = new Ext.data.Store({
    autoLoad: true,
    model: 'Audition',
    sorters: 'id'
});

显示模型:

app.models.Show = Ext.regModel("app.models.Show", {

    fields: [
        {name: "id",            type: "integer"},
        {name: "title",         type: "string"},
        {name: "description",   type: "string"},
        {name: "opening_night", type: "string"},
        {name: "schedule",      type: "string"},
        {name: "producer",      type: "string"},
        {name: "director",      type: "string"},
        {name: "status",        type: "string"},
        {name: "poster",        type: "string"},
        {name: "year",          type: "string"}
    ],

    hasMany: [  {model: 'Audition', name: 'auditions'}, 
                {model: 'Role', name: 'roles'} ],

    proxy: {
        type: 'ajax',
        url : 'app/data/shows.json',
        reader: {
            type: 'json',
            root: 'shows',
            id  : 'id'
        }
    },

});

app.stores.shows = new Ext.data.Store({
    autoLoad: true,
    model: "app.models.Show",
    sorters: 'title',
    getGroupString : function(record) {
        return record.get('title')[0];
    }
});

以下是每个模型的json数据样本

试镜:

{
    "auditions": [
        {   
            "id"            : "1", 
            "show_id"       : "1",
            "date"          : "March 1, 2011",
            "details"       : "Details go here."
        },
        {   
            "id"            : "2", 
            "show_id"       : "2",
            "date"          : "March 2, 2011",
            "details"       : "Details go here."
        },
        {   
            "id"            : "3", 
            "show_id"       : "12",
            "date"          : "March 3, 2011",
            "details"       : "Details go here."
        }
    ]
}

节目:

{
    "shows": [
        {   
            "id"            : 1, 
            "title"         : "The Diary of Anne Frank", 
            "status"        : "Coming Soon", 
            "description"   : "Show description goes here.",
            "opening_night" : "2010-10-08", 
            "schedule"      : "October 8-24, 2010", 
            "producer"      : "Hello World", 
            "director"      : "Hello World",
            "year"          : "2010",
            "poster"        : "thediaryofannefrank.jpg"
        },
        {   
            "id"            : "2", 
            "title"         : "It’s a Wonderful Life", 
            "status"        : "Coming Soon", 
            "description"   : "Show description goes here.", 
            "opening_night" : "2010-11-26", 
            "schedule"      : "November 26 - December 19, 2010", 
            "producer"      : "Hello World", 
            "director"      : "Hello World",
            "year"          : "2010",
            "poster"        : "itsawonderfullife.jpg"
        }
    ]
}

在每个节目的视图模板中,我通过show_id

过滤试听的数据存储
app.stores.auditions.filter('show_id', show.id);

this.showAuditionsList = new Ext.List({
  itemTpl: '{date}',
  store: app.stores.auditions
});

所以使用上面的代码,如果我在“安妮·弗兰克日记”的节目页面上,我希望看到节目的所有试镜 - 代码将返回试听1和3 - 即使试镜3有show_id 12。

在我的测试中,我发现过滤器将保留所有试镜,其show_id均为'1'和'1X'(以1开头的任何内容)。是否有更好的方法来过滤商店或更好的方法,查询商店以返回所有试镜?

谢谢!

2 个答案:

答案 0 :(得分:6)

你还试过吗

app.stores.auditions.filter({
    property: 'show_id',
    value: show.id,
    exactMatch: true
});

app.stores.auditions.filterBy(function(record, id) {
    return store.id = id;
}, this);

或者,如果您只想获得该特定记录;

var record = app.stores.auditions.getAt(app.stores.auditions.findExact('store_id', store.id));

答案 1 :(得分:0)

我发现要传递一个对象来过滤它必须实例化一个新的过滤器,如下所示:

store.filter(new Ext.util.Filter({property:"foo", value:"bar", exactMatch:true});