S
S
Svyatoslav Khusamov2016-03-15 10:00:55
ExtJS/Sencha
Svyatoslav Khusamov, 2016-03-15 10:00:55

How to create links between Ext.data.Store stores in memory?

I have created two repositories. Specified a one-to-many relationship (hasMany: "Detail"). Indicated that they are connected through the "pid" field of the "detailStore" ({ name: "pid", reference: "Master" }).
The question is, where to register the connection of two existing repositories?
Here is the code (sandbox https://fiddle.sencha.com/#fiddle/1735):

var proxy = {
            type: "memory",
            reader: {
                type: "json"
            }
        };
        
        Ext.define("Master", {
            extend: "Ext.data.Model",
            fields: ["id", "title"],
            hasMany: "Detail"
        });
        
        Ext.define("Detail", {
            extend: "Ext.data.Model",
            fields: ["id", { name: "pid", reference: "Master" }, "title"]
        });
        
        var masterStore = Ext.create("Ext.data.Store", {
            proxy: proxy,
            model: "Master",
            data: [
                { id: 1, title: "Группа 1" },
                { id: 2, title: "Группа 2" },
                { id: 3, title: "Группа 3" }
            ]
        });
        
        var detailStore = Ext.create("Ext.data.Store", {
            proxy: proxy,
            model: "Detail",
            data: [
                { id: 1, pid: 1, title: "Запись 1" },
                { id: 2, pid: 1, title: "Запись 2" },
                { id: 3, pid: 1, title: "Запись 3" },
                { id: 4, pid: 2, title: "Запись 4" },
                { id: 5, pid: 2, title: "Запись 5" },
                { id: 6, pid: 2, title: "Запись 6" }
            ]
        });
        
        console.log(masterStore.getAt(1).details().getCount()); // Выдает нуль, хотя записей там три на каждую группу.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Svyatoslav Khusamov, 2016-03-16
@khusamov

Hooray, I got an answer on the official site
https://www.sencha.com/forum/showthread.php?309480...
Here is the sandbox https://fiddle.sencha.com/#fiddle/177f
And here is the code:

var proxy = {
            type: "memory",
            reader: {
                type: "json"
            }
        };

        var session = Ext.create("Ext.data.Session");

        Ext.define("Master", {
            extend: "Ext.data.Model",
            fields: ["id", "title"]
        });

        Ext.define("Detail", {
            extend: "Ext.data.Model",
            fields: ["id", {
                name: "pid",
                reference: {
                    type: "Master",
                    role: "Master"
                }
            }, "title"]
        });

        var masterStore = Ext.create("Ext.data.Store", {
            proxy: proxy,
            model: "Master",
            session: session,
            data: [{
                id: 1,
                title: "Группа 1"
            }, {
                id: 2,
                title: "Группа 2"
            }, {
                id: 3,
                title: "Группа 3"
            }]
        });

        var detailStore = Ext.create("Ext.data.Store", {
            proxy: proxy,
            model: "Detail",
            session: session,
            data: [{
                id: 1,
                pid: 1,
                title: "Запись 1"
            }, {
                id: 2,
                pid: 1,
                title: "Запись 2"
            }, {
                id: 3,
                pid: 1,
                title: "Запись 3"
            }, {
                id: 4,
                pid: 2,
                title: "Запись 4"
            }, {
                id: 5,
                pid: 2,
                title: "Запись 5"
            }, {
                id: 6,
                pid: 2,
                title: "Запись 6"
            }]
        });

        console.log(masterStore.getAt(1).details().getCount());

It turns out that in the link you need to register role: "Master"

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question