Ad

RactiveJS Adding Subcomponent On The Fly

- 1 answer

I've got some problems when adding a subcomponent on the fly with Ractive.

I'm making a schedule/calendar component and by selecting an interval on a day a new event/meeting should be added. This is why i need to add the subcomponent on the fly.

Right now i do something like this:

this.on('addEvent', function () {
  new CalendarEvent({
    el: evt.node
  });
});

This adds the component in place, but it isn't recognized as a subcomponent so e.g. click events are being triggered on the "parent" component as well. That was fine(-ish) because i could hack an on-click=cancelEvents dummy listener which just returned false(thus preventing bubbling). Not a beautiful solution but it worked.

But in my subcomponent i need to know the parent components height, but this.parent is null which is a bigger problem.

I also tried placing the component with render and insert but i just can't get it to work.

Is there a sane way of adding subcomponents on the fly but still keep the parent/child relations?

Thanks in advance!

P.S

Here is a JS-fiddle demonstrating what my expectations vs actual behavior is! http://jsfiddle.net/noakcjws/1/

Ad

Answer

The only real way to create components in such a way that parent-child relationships are respected is to do it with data binding (skip to 'Run code snippet' then hit 'Full page'...):

var ractive = new Ractive({
  el: 'main',
  template: '#template',
  data: {
    days: 'monday tuesday wednesday thursday friday'.split( ' ' ).map( function ( name ) {
      return { name: name, intervals: makeIntervals() };
    })
  },
  addEvent: function ( day, interval ) {
    this.set( 'days[' + day + '].intervals[' + interval + '].event', {
      description: 'i am a calendar event'
    });
  }
});

Ractive.components.CalendarEvent = Ractive.extend({
  template: '#calendar-event'
});

function makeIntervals () {
  var intervals = [];
  for ( var i = 9; i < 17; i += 1 ) {
    intervals.push({ start: i, end: i + 1, event: null });
  }
  return intervals;
}
body, html, main {
  width: 100%;
  height: 100%;
  padding: 0;
  margin: 0;
}

.day {
  position: relative;
  float: left;
  width: 20%;
  height: 100%;
  padding: 4em 0 0 0;
  border-right: 2px solid #aaa;
  box-sizing: border-box;
}

h2 {
  position: absolute;
  top: 0;
  left: 0;
}

.interval {
  position: relative;
  width: 100%;
  height: 12.5%; /* 100/8 */
  border-bottom: 1px solid #eee;
  box-sizing: border-box;
}

time {
  position: absolute;
  top: 0.5em;
  left: 0.5em;
  color: #eee;
}

.event {
  position: absolute;
  width: 100%;
  height: 100%;
  background-color: orange;
  color: white;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/ractive/0.7.3/ractive.min.js"></script>
<main></main>

<script type='text/html' id='template'>
  {{#each days :d}}
    <div class='day'>
      <h2>{{name}}</h2>
      
      {{#each intervals :i}}
        <div class='interval' on-click='addEvent(d,i)'>
          <time>{{start}}:00</time>
          {{#if event}}
            <CalendarEvent event='{{event}}'/>
          {{/if}}
        </div>
      {{/each}}
    </div>
  {{/each}}
</script>

<script type='text/html' id='calendar-event'>
  <div class='event'>
    <p>{{event.description}}</p>
  </div>
</script>

Obviously this elides a huge amount of complexity around events that span x intervals, and scheduling conflicts, etc, but they're going to crop up whichever approach you take!

Ad
source: stackoverflow.com
Ad