RactiveJS Adding Subcomponent On The Fly
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/
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!
Related Questions
- → How to update data attribute on Ajax complete
- → October CMS - Radio Button Ajax Click Twice in a Row Causes Content to disappear
- → Octobercms Component Unique id (Twig & Javascript)
- → Passing a JS var from AJAX response to Twig
- → Laravel {!! Form::open() !!} doesn't work within AngularJS
- → DropzoneJS & Laravel - Output form validation errors
- → Import statement and Babel
- → Uncaught TypeError: Cannot read property '__SECRET_DOM_DO_NOT_USE_OR_YOU_WILL_BE_FIRED' of undefined
- → React-router: Passing props to children
- → ListView.DataSource looping data for React Native
- → Can't test submit handler in React component
- → React + Flux - How to avoid global variable
- → Webpack, React & Babel, not rendering DOM