2015-12-10 60 views
1

问题:
我想尝试有多个游戏在我的火力地堡的根源运行,它们都具有火力地堡生成的ID,
当我试图获取他们使用<firebase-collection>我总是得到一个对象数组,并且2-way绑定对于Polymer来说非常困难。
有没有办法将Firebase数据作为对象获取?使用聚合物火力地堡数据阵列的设置值

我试图添加data-as-object属性,但它没有奏效,我也得到了一个数组。
我甚至试图使用<firebase-document>元素,只需将表格
<-collection>更改为<-document>,但更新游戏数据并没有推到firebase。
我不知道这是否可能...

或者有没有一种方法来实现良好的双向绑定?当我尝试

的完整代码上修改数组中的对象下的值this.set('gameData.' + i),因为那时我遇到了错误Uncaught Error: unexpected key 2,:
我不能使用数组项目的索引,这样Github

我的代码:
数据提供:

<firebase-collection location="https://***.firebaseio.com" 
        data="{{data}}" data-as-object log> 
</firebase-collection> 
<!-- script --> 
Polymer({ 
    is: 'tmp-game-data', 
    properties: { 
     data: { 
      notify: true 
     } 
     } 
}); 

游戏元素:

<!-- map with several players(Array), --> 
<!-- an player(Object) has some properties --> 
<!-- on-map-ready, when the map is fully configured --> 
<tmp-map players="{{game.players}}" 
     on-map-ready="_mapReadyCallback"> 
</tmp-map> 
<!-- menu user for creating and --> 
<!-- joining games with selected settings --> 
<tmp-menu on-start-global-game="startGlobalGame" 
      on-join-game="joinGame"> 
</tmp-menu> 
<tmp-game-data data="{{gamesData}}"></tmp-game-data> 
<!-- script --> 
var blocks = []; //Map blocks, global access 

Polymer({ 
    is: 'tmp-game', 

    properties: { 
     gamesData: { 
      notify: true 
     }, 
     game: { 
      notify: true 
     }, 
     gameId: { 
      type: String 
     } 
    }, 

    startGlobalGame: function(e) { 
     var gameId = getRandomString(6); 
     this.set('gameId', gameId); 

     var player = {id: /*playerId*/}; 
     var players = [player]; 

     this.push('gamesData', { 
      gameId: gameId, 
      maxPlayers: e.detail.maxPlayers, 
      players: players 
     }); 

     this.selectGame(); 

     //generate map 
    }, 

    joinGame: function(e) { 
     var gameId = e.detail.gameId; 
     for (var i = 0; i < this.gamesData.length; i++) { 
      if(this.gamesData[i].gameId === gameId) { 
       var map = this.get('gamesData.' + i + '.map'); 
       //generate map with the map of the game 
      } 
     } 
     this.selectGame(); 
    }, 

    selectGame: function() { 
     for (var i = 0; i < this.gamesData.length; i++) { 
      if(this.gamesData[i].gameId === this.gameId) { 
       this.set('game', this.gamesData[i]); 
       this.linkPaths('game', 'gamesData.' + i); 
      } 
     } 
    }, 

    _mapReadyCallback: function(e) { 
     for (var i = 0; i < this.gamesData.length; i++) { 
      if(this.gamesData[i].gameId === this.gameId) { 
       //get the map as a json 
       var map = JSON.stringify(blocks); 
       this.set('gamesData.' + i + '.map', map); 
      } 
     } 
    } 
}); 
+0

我除去'','