Javscript dependencies in composite view
Posted: Tue Aug 12, 2008 9:04 am
How do you ya'll deal with Javascript and CSS dependencies in composite views?
I've recently started incorporating Yahoo's YUI library into my applications. (I really like their library, BTW, it fits very well with my style of applications.) Now I'm starting to try to figure out how best to deal with all the dependencies their various components require.
Various YUI components usually require a few other javascript files and a few CSS files. Have ya'll run into wanting your view partials to add styles and javascript to the surrounding template?
For my part, my problem stems from using composition to assemble the view partials inside a Template. A typical controller for me looks like:
The taskTable block looks like:
Where I'm reusing the same Block (view partial), but with different data. In order to inject any extra, required javascript into the template it looks like I either have to:
* Do it in the controller
* or somehow find some way to allow the Blocks to reference the template
I'm not crazy about doing stuff like
That seems like the controller would need to know about any JavaScript requirements of the view ahead of time. Not very fun for adding fancy AJAX stuff to the view down the road. But I can't seem to wrap my head around a way to reference the Template object from inside a Block.
I've recently started incorporating Yahoo's YUI library into my applications. (I really like their library, BTW, it fits very well with my style of applications.) Now I'm starting to try to figure out how best to deal with all the dependencies their various components require.
Various YUI components usually require a few other javascript files and a few CSS files. Have ya'll run into wanting your view partials to add styles and javascript to the surrounding template?
For my part, my problem stems from using composition to assemble the view partials inside a Template. A typical controller for me looks like:
Code: Select all
$template = new Template();
$template->title = 'Dashboard';
$pastDue = new TaskList(array('status'=>'Past Due'));
$template->blocks[] = new Block('tasks/taskTable.inc',array('taskList'=>$pastDue,'title'=>'Past Due'));
$range = array('start'=>time(),'end'=>strtotime('+30 day'));
$thisMonth = new TaskList(array('dueDate'=>$range));
$template->blocks[] = new Block('tasks/taskTable.inc',array('taskList'=>$thisMonth,'title'=>'Next 30 Days'));
echo $template->render();
Code: Select all
<div class="interfaceBox">
<?php echo $title; ?>
<table>
<tr>
<th>Task</th>
<th>Status</th>
<th>Due</th>
</tr>
<?php
foreach($this->taskList as $task)
{
$name = View::escape($task->getName());
echo "
<tr>
<td><a href=\"{$task->getURL()}\">$name</a></td>
<td>{$task->getStatus()}</td>
<td>{$task->getDueDate('n/j/Y')}</td>
</tr>
";
}
?>
</table>
</div>
* Do it in the controller
* or somehow find some way to allow the Blocks to reference the template
I'm not crazy about doing stuff like
Code: Select all
$template = new Template();
$template->appendScript('/js/yui/build/utilities/utilities.js');
$pastDue = new TaskList(array('status'=>'Past Due'));
$template->blocks[] = new Block('tasks/taskTable.inc',array('taskList'=>$pastDue,'title'=>'Past Due'));
echo $template->render();