在XTemplate中加载关联的模型数据

时间:2013-10-02 15:20:15

标签: model sencha-touch sencha-touch-2 associations

过去两天我搜索了很多,但无法找到解决方案。这是我的问题的描述:

我有一个Sencha Touch应用程序,其中一个简单的列表作为视图,2个模型(CartItem和Article)和2个相应的商店(CartItems和Articles)。

CartItem模型与文章模型有“hasOne”关联。

列表视图应显示所有CartItems以及相关文章的描述。

不应该那么难吗?一般的协会都有效,我通过以下方式获得了文章描述:

> Ext.getStore('cartItemStore').getAt(0).getArticle().get('description')
"Example article 1"

但是我无法在列表中显示此说明。

我已经尝试过使用

  1) <tpl for="Article">{description}</tpl>
     -> doesn't display anything. Neither data nor an error.
  2) {Article.description}
     in combination with:
     http://appointsolutions.com/2012/07/using-model-associations-in-sencha-touch-2-and-ext-js-4/
     -> f***ed up my whole storage-system :/

那么我可以在模板中显示关联数据的任何想法吗?如果有人能找到解决方案,我会非常感激!这是一个最小的代码示例:

Main.js

Ext.define('assoTempl.view.Main', {
    extend: 'Ext.dataview.List',
    xtype: 'main',
    config: {
        store: 'cartItemStore',
        scrollable: 'vertical',
        itemTpl: '<span>{cart_pos} <tpl for="Article">{description}</tpl></span>'
    }
});

CartItem.js (CartItem Model)

Ext.define('assoTempl.model.CartItem', {
    extend: 'Ext.data.Model',

    config: {
        fields: [
            { name: 'cart_pos'},
            { name: 'article_id'}
        ],
        proxy: {
            type: 'memory'
        },
        hasOne: {
            model: 'assoTempl.model.Article',
            name: 'Article',
            foreignKey: 'ANr',
            foreignStore: 'ArticleStore'
        }
    }
});

Article.js (文章模型)

Ext.define('assoTempl.model.Article', {
    extend: 'Ext.data.Model',

    config: {
        fields: [
            { name: 'ANr', type: 'int' },
            { name: 'description', type: 'String' }
        ],
        proxy: {
            type: 'memory'
        }
    }
});

CartItems.js (CartItem商店)

Ext.define("assoTempl.store.CartItems", {
    extend: "Ext.data.Store",
    requires: ['Ext.data.proxy.Sql'],
    config: {
        storeId: 'cartItemStore',
        model: "assoTempl.model.CartItem",
        data: [
            {cart_pos:0, article_id:0},
            {cart_pos:1, article_id:1}
        ],
        autoLoad: true
    }
});

Articles.js (文章商店)

Ext.define("assoTempl.store.Articles", {
    extend: "Ext.data.Store",
    requires: ['Ext.data.proxy.Sql'],
    config: {
        storeId: 'articleStore',
        model: "assoTempl.model.Article",
        data: [
            {ANr:0, description:'Example article 1'},
            {ANr:1, description:'I am article 2'}
        ],
        autoLoad: true
    }
});

1 个答案:

答案 0 :(得分:0)

哦,上帝,抱歉打扰你。没有人说我必须执行

cartItem.setArticle(myArticle);

之前我可以访问该关联。 -.-

相关问题