2017-01-23 6 views
0

回答のグリッドを生成する外部サーバーからのJSON応答があります。彼らが行くルートはEmberに設定されていますが、結果を追加する際にヘルパーにリンクを使用する方法を理解できず、クリックするとアプリケーションがリロードされます。Emberの投稿応答からlink-toを追加します。

export default Ember.Component.extend({ 
    actions: { 
    switchGenre(genre){ 
     $.post('https://somewhere.com/search',{'genre':genre},function (data) { 
     $('#stations').empty() 
     var activeRow = 0; 
     $('#stations').append('<div class="row" id="row'+activeRow+'">') 
     $.each(data,function(index,station){ 
      if(index%4==0 && index > 0){ 
      activeRow++; 
      $('#stations').append('<div class="row" id="row'+activeRow+'">') 
      } 
      $('#row'+activeRow).append('<a href="/station/'+station.id+'"></a>') 

     }) 
     }) 
    } 

    } 
}); 
+1

jqueryで直接DOMを操作するのではなく、レスポンスを使って '{{each} 'ブロックを書くのはなぜですか? – kumkanillam

+0

しかし、JSファイルでHandlebarsを使用するにはどうしたらいいですか? '{{各データを|ステーションのインデックスとして|}}' –

答えて

1

あなたのコンポーネント名はmy-stationsとします。 my-stations.jsファイルのinitメソッドでstations配列を定義し、my-stations.hbsファイルで繰り返します。更新する配列をswitchGenreメソッドに問い合わせると、最新の応答でコンポーネントが再描画されます。

+0

見つけにくい場合は、 'my-station.js'と' my-station.hbs'コードを準備し、それを – kumkanillam

+0

I switchGenreアクションから更新するステーション配列を取得しましたが、ビューを更新することができませんでした。 –

+0

基本的なデモンストレーションについては、[このひねり](https://ember-twiddle.com/ec4c0a88555e85718c3aa66da81b8191?openFiles=components.my-component.js%2Ctemplates.components.my-component.hbs)を確認してください。 – kumkanillam

関連する問題