2013-05-29 14 views
1

我有一個看起來是這樣的(實際下面的代碼)一個簡單的數據模型:煎茶觸摸2:協會數據綁定到現有的商店

model Game: 
    fields: id, team_1_id, team_2_id 

model GameScore: 
    fields: id, game_id, team_1_score, team_2_score, is_final, submission_date 

model SpiritScore: 
    fields: id, game_id, team_1_score, team_2_score 

我想要什麼似乎很簡單。我已經有批量加載遊戲和GameScores的代碼。我手上有一個'遊戲'實例,可以調用gameScores()。我有一家商店,但它是空的。通過將商店放入模型的hasMany定義中,我擁有可動態加載它的代碼。但是我真正想要的是將Game.gameScores()調用綁定到我現有的GameScores商店的一些方法。即使它在下面使用了一個普通過濾器,它也給了我一個可以在視圖中綁定和使用的記錄。 (重要提示:數據不是嵌套形式。)

這導致我的第二個問題。遊戲:GameScores是1:很多,但我只展示最近的一個(來自實時比分報道)。這裏的一般方法是什麼?我也可以從game_id手動構建一個過濾器,但是我只能將1條記錄綁定到一個視圖,所以我沒有看到如何將其他信息放入視圖中,缺少正確的hasMany關係。有另一種方法嗎?

任何和所有的建議,包括告訴我RTFM(帶有相關手冊的鏈接)將不勝感激!我一直在爲這個(無償方面的項目)在上週的頭髮。

乾杯!

b

Ext.define('TouchMill.model.Game', { 
    extend: 'Ext.data.Model', 

    config: { 
     fields: [ 'id', 'team_1_id', 'team_2_id' ], 
     hasMany: { 
      model: 'TouchMill.model.GameScore', 
      name: 'gameScores', 
     }, 
    }, 
}); 

Ext.define('TouchMill.model.GameScore', { 
    extend: 'Ext.data.Model', 

    config: { 
     fields: [ 'id', 'game_id', 'team_1_score', 'team_2_score', 'is_final', 'submission_date', ], 
    }, 
    // belongsTo necessary? Don't think so unless I want parent func? 
}); 

Ext.define('TouchMill.model.SpiritScore', { 
    extend: 'Ext.data.Model', 

    config: { 
     fields: [ 'id', 'game_id', 'team_1_score', 'team_2_score', ], 
    }, 
}, 

回答

1

我從來沒有用過觸摸,所以我在談論這裏的ext4(4.2準確地說)......而且,你的模型定義似乎有點破,我(是,使用觸摸?)。但無論如何,你會得到一般想法。如果我的代碼無法聯繫,請使用Ext4。

另外,我知道你一次加載所有的分數。如果不是這樣,我的解決方案將需要適應...

所以,我的一般推理如下:如果你已經加載所有的分數在內存中,那麼爲什麼不使用內存代理,使用評分商店的數據作爲爲該關聯生成的商店的數據源?我嘗試過,而且令我驚訝的是,它沒有發生故障。

爲了理解這一點,您需要知道代理是一個獨立的數據源,也就是說代理可以在多個商店之間共享而沒有問題。另一方面,商店預計將被綁定到單個視圖或任務。例如,如果將同一個商店綁定到兩個不同的網格,則過濾第一個網格也會影響第二個網格。

雖然大多數代理不「包含」他們的數據,但內存代理也可以。下面是Ext.data.proxy.Memory#read方法的相關摘錄:

resultSet = operation.resultSet = me.getReader().read(me.data) 

因此,足夠的理論,這裏的概念證明(在此fiddle測試):

// I instantiate this proxy myself in order to have a reference available 
var masterScoreProxy = Ext.create('Ext.data.proxy.Memory'); 

Ext.define('TouchMill.model.GameScore', { 
    extend: 'Ext.data.Model', 
    fields: [ 'id', 'game_id', 'team_1_score', 'team_2_score', 'is_final', 'submission_date' ], 
    // I've used a remote server to ensure this all works even asynchronously 
    proxy: { 
     // configure your own 
    } 
}); 

Ext.define('TouchMill.model.Game', { 
    extend: 'Ext.data.Model' 
    ,fields: [ 'id', 'team_1_id', 'team_2_id' ] 
    ,hasMany: { 
     model: 'TouchMill.model.GameScore' 
     ,name: 'gameScores' 
     // required in order to avoid Ext autogenerating it as 'touchmill.model.game_id' 
     ,foreignKey: 'game_id' 
     // needed if we don't want to have to call gameRecord.gameScores().load() 
     ,autoLoad: true 

     // first part of the magic: make the generated store use my own proxy 
     ,storeConfig: { 
      proxy: masterScoreProxy 
     } 
    } 
}); 

// Just mocking a store with two games 
var gameStore = Ext.create('Ext.data.Store', { 
    model: 'TouchMill.model.Game' 
    ,data: [{id: 1}, {id: 2}] 
    ,proxy: 'memory' 
}); 

// Creating the "master" score store (that will use the model's proxy) 
var scoreStore = Ext.create('Ext.data.Store', { 
    model: 'TouchMill.model.GameScore' 

    // second part's in there 
    ,listeners: { 
     load: function(store, records, success) { 
      if (success) { 
       // 1. replace the data of the generated association stores' proxy 
       // (I must say I'm quite surprised that I didn't had to extract the data of 
       // every records, nor to configure a reader and all for my shared proxy... 
       // But hey, that works!) 
       masterScoreProxy.data = records; 

       // 2. update already generated stores 
       // Alternatively, you could call gameRecord.gameScores().load() individually 
       // before each usage of gameRecord.gameStores() 
       gameStore.each(function(record) { 
        var childStore = record.gameScoresStore; 
        if (childStore) { 
         childStore.load(); 
        } 
       }); 
      } 
     } 
    } 
}); 

// test first load 
scoreStore.load({ 
    callback: function(records, operation, success) { 
     if (success) { 
      // and here's to prove it 
      gameStore.each(function(record) { 
       record.gameScores().each(function(score) { 
        console.log('Game ' + record.id + ': ' + JSON.stringify(score.data, undefined, 2)); 
       }); 
      }); 

      testRefreshedData(); 
     } 
    } 
}); 

function testRefreshedData() { 
    // test refreshing 
    scoreStore.load({ 
     callback: function(records, operation, success) { 
      if (success) { 
       console.log('--- Scores have changed ---'); 
       gameStore.each(function(record) { 
        record.gameScores().each(function(score) { 
         console.log('Game ' + record.id + ': ' + JSON.stringify(score.data, undefined, 2)); 
        }); 
       }); 
      } 
     } 
    }); 
} 

關於你提到的其他問題......

如果你的Game:Score爲1:n,那麼Game:MostRecentScore的比例爲1:1,所以我會嘗試使用它。

至於視圖,應該總是有一種方法 - 即使是黑客 - 訪問嵌套在記錄中的數據。方法將取決於你在這裏看到什麼...看,例如這question

+0

哇。這是一個美妙的開始!謝謝!我會稍後嘗試一起黑客攻擊。其中一個區別是遊戲實際上是動態加載的 - 但只需要不經常刷新,因此同時刷新相關分數非常方便。此外,這個緩存概念也適用於其他事情。 – ben