1. #1
    Sencha User
    Join Date
    Apr 2013
    Posts
    7
    Vote Rating
    0
    benomatic42 is on a distinguished road

      0  

    Default Unanswered: hasMany relationship: static (broken) vs. dynamic loading

    Unanswered: hasMany relationship: static (broken) vs. dynamic loading


    I have 2 models with a 1:many relationship (Game:GameScores -- game scores can be part-way through a game). I have the relationship defined in the Game model. When I call <game>.gameScores(), I always get an empty array. The 'GameScores' store is already populated with the data that I want. How do I root the gameScores() store in my 'basis' store? The code is below, and also available as a gist: https://gist.github.com/benfleis/91a3345cb626367fef84#file-hasmany-js

    I could also load the scores dynamically, but performance wise, it would be better if I can bulk pre-load and simply refer to those that already exist in the main store. But I would love to see both solutions if they can both be implemented.

    The code below works live and refers to a live server, btw. But please be nice to it.

    Code:
    Ext.Loader.setConfig({ enabled: true, });
     
    Ext.application({
        name: 'TouchMill',
     
        models: [ 'Game', 'GameScore', ],
        stores: [ 'Games', 'GameScores', ],
        views: [ 'Games', 'GameView', 'GamesList', ],
        controllers: [ 'Games', ],
     
        launch: function() {
            this.getController('Games').loadInitialGames();
            Ext.create('TouchMill.view.Main');
        },
    });
     
    // ----------------------------------------------------------------------------
     
    Ext.define('TouchMill.model.Game', {
        extend: 'Ext.data.Model',
     
        config: {
            fields: [
                { name: 'id', },                        //": 74181,
                { name: 'team_1_id', },                 //": 19098,
                { name: 'team_2_id', },                 //": 19083,
            ],
     
            hasMany: {
                model: 'TouchMill.model.GameScore',
                name: 'gameScores',
                autoLoad: true,
            },
        },
    });
     
    Ext.define('TouchMill.model.GameScore', {
        extend: 'Ext.data.Model',
     
        config: {
            fields: [
                { name: 'game_id', },                   //": 28582,
                { name: 'team_1_score', },              //": 8,
                { name: 'team_2_score', },              //": 9,
                { name: 'is_final', },                  //": true,
                { name: 'id', },                        //": 27903,
            ],
        },
    });
     
    // ----------------------------------------------------------------------------
     
     
    Ext.define('TouchMill.store.Games', {
        extend: 'Ext.data.Store',
     
        config: {
            model: 'TouchMill.model.Game',
            proxy: {
                type: 'rest',
                url: 'http://api.playwithlv.com/v1/games/',
                reader: { type: 'json', rootProperty: 'objects', },
            },
        },
     
        loadByTeamId: function(team_id, opts) {
            console.log('Games.loadByTeamId(' + team_id + ')');
            opts = opts || {};
            opts.params = { team_ids: [team_id] };
            this.load(opts);
        },
    });
     
     
    Ext.define('TouchMill.store.GameScores', {
        extend: 'Ext.data.Store',
     
        config: {
            model: 'TouchMill.model.GameScore',
     
            proxy: {
                type: 'rest',
                url: 'http://api.playwithlv.com/v1/game_scores/',
                reader: { type: 'json', rootProperty: 'objects', },
            },
        },
     
        loadByGameId: function(game_id, opts) {
            opts = opts || {};
            opts.params = { game_id: game_id };
            this.load(opts);
        },
    });
     
    // ----------------------------------------------------------------------------
     
    Ext.define('TouchMill.controller.Games', {
        extend: 'Ext.app.Controller',
     
        requires: [ 'TouchMill.view.GameView', 'TouchMill.view.GamesList', 'TouchMill.view.Games', ],
     
        config: {
            refs: { games: 'games', gamesList: 'gamesList', gameView: 'gameView', },
            control: {
                gamesList: { itemtap: 'onGameSelect', },
            },
        },
     
        loadInitialGames: function() {
            Ext.getStore('Games').load({
                params: { tournament_id: '18093', team_ids: '[19083]', },
                scope: this,
                callback: function() {
                    Ext.create('TouchMill.view.Games');
                }
            });
        },
     
        onGameSelect: function(list, idx, item, rec, evt) {
            var gameView = this.getGameView();
            if (!gameView)
                gameView = Ext.create('TouchMill.view.GameView');
     
            gameView.setRecord(rec);
            //gameView.setMasked(true);
            Ext.getStore('GameScores').load({
                params: { game_id: rec.data.id, },
                scope: this,
                callback: function() {
                    console.log(rec.gameScores());
                    //gameView.setMasked(false);
                }
            });
            this.getGames().push(gameView);
        },
    });
     
    // ----------------------------------------------------------------------------
     
    Ext.define('TouchMill.view.Main', {
        extend: 'Ext.tab.Panel',
        xtype: 'main',
     
        config: {
            fullscreen: true,
            tabBar: { layout: { pack: 'center', align: 'center', }, },
            tabBarPosition: 'top',
            items: [ { xtype: 'games', }, ],
        }
    });
     
    Ext.define('TouchMill.view.Games', {
        extend: 'Ext.navigation.View',
        xtype: 'games',
        requires: [ 'TouchMill.view.GamesList', 'TouchMill.view.GameView', ],
     
        config: {
            iconCls: 'calendar2',
            items: [
                { xtype: 'gamesList', },
            ],
        },
    });
     
    Ext.define('TouchMill.view.GamesList', {
        extend: 'Ext.List',
        xtype: 'gamesList',
     
        config: {
            title: 'My Games',
            store: 'Games',
            itemTpl: '{team_1_id} vs. {team_2_id}<br/><small>{start_time} @ {game_site_id}</small>',
        },
    });
     
    Ext.define('TouchMill.view.GameView', {
        extend: 'Ext.Container',
        xtype: 'gameView',
     
        config: {
            title: 'Game Summary',
            record: null,
            tpl: '<h1>id: {id}</h1><h1>{team_1_id} vs. {team_2_id}</h1>{gameScores()}',
        },
    });

  2. #2
    Sencha User
    Join Date
    Sep 2010
    Posts
    27
    Vote Rating
    2
    dusoft is on a distinguished road

      0  

    Default I have the same problem

    I have the same problem


    I have the same problem, but your solution doesn't work for me...
    See my post..
    http://www.sencha.com/forum/showthre...ny-association.
    Can you help me?

Thread Participants: 1

Tags for this Thread

Turkiyenin en sevilen filmlerinin yer aldigi xnxx internet sitemiz olan ve porn sex tarzi bir site olan mobil porno izle sitemiz gercekten dillere destan bir durumda herkesin sevdigi bir site olarak tarihe gececege benziyor. Sitenin en belirgin ozelliklerinden birisi de Turkiyede gercekten kaliteli ve muntazam, duzenli porno izle siteleri olmamasidir. Bu yuzden iste. Ayrica en net goruntu kalitesine sahip adresinde yayinlanmaktadir. Mesela diğer sitelerimizden bahsedecek olursak, en iyi hd porno video arşivine sahip bir siteyiz. "The Best anal porn videos and slut anus, big asses movies set..."