从父组件构建组件的新实例并将其注入DOM? - 灰烬

时间:2016-06-19 13:42:55

标签: javascript ember.js

我正在构建一个简单的Web应用程序,当用户按下按钮时,它将生成组件的新实例并将其添加到页面中。

但是,我不确定如何从父组件的.js文件生成组件的新实例..

模板:

{{yield}}

<div class="feed-tracks-container">
  {{#each model.tracks as |track|}}
    {{#feed-card track=track}}{{/feed-card}}
  {{/each}}
</div>

<div class="feed-controls-container">
  <div class="feed-control-container" style="width:25%" {{action (route-action "undoLastAction" track)}}>
    <img src="images/undo-btn.png"/>
  </div>
  <div id="skip-btn" class="feed-control-container" style="width:25%" {{action (route-action "skipTrack" track)}}>
    <img src="images/skip-btn.png"/>
  </div>
  <div id="share-btn" class="feed-control-container" style="width:25%" {{action (route-action "shareTrack" track)}}>
    <img src="images/share-btn.png"/>
  </div>
  <div class="feed-control-container" style="width:25%" {{action (route-action "togglePreview" track)}}>
    <img src="images/preview-inactive-btn.png"/>
  </div>
</div>

路线:

import Ember from 'ember';

export default Ember.Route.extend({
model: function() {
     return {
       tracks: [
        {
          id: 1,
          track_name: "Karate",
          artist_name: "R3hab",
          track_length: 230,
          share_count: 10864,
          background_img: "https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcR3oh8ADMDG3hVZ1I_9IJumqLXJNS2-TBPrxpUdRL8J_71H5I0kyw",
          waveform_img: "http://i.imgur.com/kP6KCJl.png"
        },
        {
          id: 2,
          track_name: "Care",
          artist_name: "R3hab",
          track_length: 230,
          share_count: 1322,
          background_img: "https://encrypted-tbn2.gstatic.com/images?q=tbn:ANd9GcS5OsXpnek1SMJzol7hYnRt9QjNEHwPf1InRAKwoWocZWvMzPqo0w",
          waveform_img: "http://i.imgur.com/8o0AdWk.png"
        },
        {
          id: 3,
          track_name: "Soundwave",
          artist_name: "R3hab",
          track_length: 230,
          share_count: 14554,
          background_img: "http://www.music-bazaar.com/album-images/vol17/800/800095/2652999-big/Soundwave-Extended-Mix-Single-cover.jpg",
          waveform_img: "http://i.imgur.com/8o0AdWk.png"
        }
      ] 
    }
  },
  actions: {
    skipTrack: function() {
      var new_track = {
        id: 6,
        track_name: "I'm new!",
        artist_name: "R3hab",
        track_length: 230,
        share_count: 14554,
        background_img: "http://www.music-bazaar.com/album-images/vol17/800/800095/2652999-big/Soundwave-Extended-Mix-Single-cover.jpg",
        waveform_img: "http://i.imgur.com/8o0AdWk.png"
      }
      var tracks = this.modelFor('feed').tracks;
      tracks.shift();
      tracks.pushObject(new_track);
    },
    shareTrack: function() {
      console.log("sharing");
    }
  },
});

ANSWER 将新对象插入数组时,我没有使用pushObject

1 个答案:

答案 0 :(得分:0)

//template
{{#each componentNames in componentName}}
  {{component componentName data=data}}
{{/each}}

更改控制器中的componentNames属性。在添加到componentNames数组时应该呈现一个新组件。

为控制器编辑交换组件,它会回答你的问题。