简体   繁体   English

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

[英]Build new instance of component from parent component and inject it into DOM? - Ember

I am building a simple web app that, when a user pushes a button, will generate a new instance of a component and add it to the page. 我正在构建一个简单的Web应用程序,当用户按下按钮时,该应用程序将生成组件的新实例并将其添加到页面中。

However, I'm not sure how to generate a new instance of a component from the parent component's .js file.. 但是,我不确定如何从父组件的.js文件中生成组件的新实例。

The template: 模板:

{{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>

The route: 路线:

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 I was not using pushObject when inserting the new object into the array. 解答将新对象插入数组时,我没有使用pushObject

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

Change the componentNames property in your controller. 在您的控制器中更改componentNames属性。 Should render a new component when you add to the componentNames array. 当您添加到componentNames数组时,应该呈现一个新组件。

edit- swap component for controller, and it answers your question. 编辑控制器的交换组件,它回答了您的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM