Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using agility.js for page layout and composition

I am new to MVC-style javascript libraries, so pardon me if this question is too basic. I'm trying to write a Single-Page Application entirely in jQuery and agility.js. The examples given in the agility.js documentation consist entirely of adding html elements to the document root. Question: Is there a 'Best-Practices' way to assemble a page by components.

Here is a rough outline of my html app:

<html>
    <head> ... </head>
    <body>
        <header> ... </header>
        <div id=PageHolder>
            <div id=AppPane_1></div>
            <div id=AppPand_2></div>
        </div>
        <footer> ... </footer>
    </body>
</html>

Within the 'AppPane' divs will be the content of the application.

Okay, given all of this, I'm not asking what can I do, but I'm asking what should I do.

I see from the documentation and my research that I have 3 choices:

  1. create my page composition from atomic agility objects and assemble them in a jQuery document ready block. $$.document.append(Foo) works for the root element, but I could not figure out how to add Foo's children to foo.
  2. Use one (very large) agility object which lays out the basic static html from above and append controls and whatnot to that using the controller functions (which I havn't been able to get to work either)
  3. Use one root agility object and append all the children onto it using view (somehow, I havn't been able to get that to work either.)

Which of these is best, and what is the syntax involved? Thanks in advance, any guidance in assembling html components into a cogent agility app would be much appreciated.

http://jsbin.com/ojenon/1/


like image 221
Evan Plumlee Avatar asked Sep 15 '12 23:09

Evan Plumlee


1 Answers

In my opinion, the best way to organize your page modules is to save individual client-side templates in script tags in the head:

<html>
<head>
<script type="text/template" id="template1">
    <b>Some <abbr>HTML</abbr> template</b>
</script>
<script type="text/template" id="template2">
    <b>Some <abbr>HTML</abbr> template</b>
</script>
</head>
...

You could even choose to use a template language, such as jQuery.template or handlebars.js, to facilitate logic, variable interpolation, etc.

Then, in your your controller, you would load the html contents of these templates' script tags from the DOM and copy them into your destination div (#PageHolder) as appropriate.

An alternative to this technique would be storing your templates in a literal JS object in the head:

<script type="text/javascript">
var Templates = {
    template1: "<b>Some <abbr>HTML</abbr> template</b>"
    ...
}
</script>

This is just the beginning. There are many more options, such as pre-compiling your templates, subdividing your templates to avoid redundant template compilations, etc. From a structural standpoint, maintaining your templates in a dedication location will help you scale your single page app.

like image 125
mickeyreiss Avatar answered Oct 02 '22 15:10

mickeyreiss