Allowing sections to work outside components

This commit is contained in:
Bruno Alves de Faria 2017-03-06 08:28:13 -03:00
parent dd745e3f65
commit 2a5699a5b7
3 changed files with 41 additions and 21 deletions

View File

@ -1,8 +1,26 @@
import { Template } from 'meteor/templating';
import { Tracker } from 'meteor/tracker';
import { ReactiveVar } from 'meteor/reactive-var';
import { OHIF } from 'meteor/ohif:core';
// Get the view that contains the desired section's content and return it
const getSection = (view, sectionName) => {
let currentView = view;
while (!currentView._sectionMap || !currentView._sectionMap.get(sectionName)) {
currentView = OHIF.blaze.getParentTemplateView(currentView);
if (!currentView) return;
}
return currentView._sectionMap.get(sectionName);
};
Template.section.onCreated(() => {
const instance = Template.instance();
if (instance.data === 'dialogFooter') {
console.warn('>>>>instance', instance);
}
// Create the render function and section data as reactive objects
instance.renderFunction = new ReactiveVar(null);
instance.sectionData = new ReactiveVar(null);
@ -18,36 +36,28 @@ Template.section.onCreated(() => {
// Get the content block
const templateContentBlock = instance.view.templateContentBlock;
// Get the parent template view of this section block
let currentView = OHIF.blaze.getParentTemplateView(instance.view);
// Check if it is defining or printing the section content
if (templateContentBlock) {
// Get the parent component of this section
const component = OHIF.blaze.getParentComponent(instance.view, '_wrapperComponent');
// Stop here if this section is not inside a component
if (!component) {
return;
// Define a section map for template's view if none was yet set
if (!currentView._sectionMap) {
currentView._sectionMap = new Map();
}
// Define the content
component.templateInstance.sections[sectionName] = {
data: component.templateInstance.data,
currentView._sectionMap.set(sectionName, {
data: currentView._templateInstance.data,
renderFunction: templateContentBlock.renderFunction
};
});
} else {
// Wait for re-rendering and print the section content
Tracker.afterFlush(() => {
// Get the parent component of this section
const component = OHIF.blaze.getParentComponent(instance.view, '_wrapperComponent');
// Get the defined section's content
const section = getSection(currentView, sectionName);
// Stop here if this section is not inside a component
if (!component) {
return;
}
// Get the defined section content
const section = component.templateInstance.sections[sectionName];
// Stop here if the section content is not defined yet
// Stop here if the section content is not defined
if (!section) {
return;
}

View File

@ -65,7 +65,6 @@ Template.baseComponent.constructView = function(contentFunc, elseFunc) {
// Iterate over all wrappers and assign the component to them
wrapperInstances.forEach(wrapperInstance => {
wrapperInstance.view._wrapperComponent = instance.component;
wrapperInstance.component = instance.component;
});
});

View File

@ -40,3 +40,14 @@ OHIF.blaze.getParentComponent = (view, property='_component') => {
}
}
};
// Search for the parent template of the given view
OHIF.blaze.getParentTemplateView = view => {
let currentView = view;
while (currentView) {
currentView = currentView.originalParentView || currentView.parentView;
if (currentView.name.indexOf('Template.') > -1) {
return currentView;
}
}
};