I think John hits the nail on the head with his comment. The reason
why I think he's right is the "view" code on the chain.js GitHub page
is just regular HTML and the "controller" code is the data-binding
logic (although he doesn't refer to "view" and "controller" in the
docs).
http://wiki.github.com/raid-ox/chain.js/
It looks like my ideal templating language as well. :) Am I reading
too much into chain.js?
Performance:
Seems like if the chain.js implementation could be changed to use
innerHTML this wouldn't be a problem any more. You could ask "raid-
ox" about that.
Also, Crockford admits innerHTML is faster, but if you're seeing 2x
orders of magnitude, I'd have to wonder whether, in your benchmark
test, you're adding each child to the main document on creation (which
generates a render after every append) or if you're building a subtree
and adding it all at once (which only generates a render at the end).
"If you say I'll put a node into the tree, and then put its child on
the tree and so on, and build it from the top down, that's really
slow, because each time you add one of those nodes you may pay for
another re-flow. Whereas if instead you build the thing independently
from the bottom up, and then plug the whole thing in when you're
finished, it's one re-flow, so that'll go a lot faster."
"... all of the browsers implement innerHTML even though no standard
requires it. It raises a question for the developer: which one should
you use? If you're making new content to put into a page, should you
build each of the nodes individually and paste them all together and
plug them in, or should you build up a big text and use innerHTML to
do that?
My advice is, do the one that favors clean code and easy maintenance
for your application. This is, in most cases, something that should
not be decided in terms of performance. Performance should only
influence you in extreme cases. In ordinary cases, if performance is
not going to be undermining the user experience, choose the one that
makes the most sense. If performance is an issue then you want to use
innerHTML, because browsers are really good at that. Even though it
appears that the browser's doing a lot more work, it goes a lot
faster."
-Crockford Episode 4
There's some subtlety in there which leaves room for you being right
and wrong at the same time. Right in that innerHTML could be 2x
orders of magnitude faster, but wrong in the sense that it may not
matter.
I'm not sure if I want to belabor this point any more because it seems
like quibbling over implementation details. Performance questions
always have an objective answer. Just test it and find out what's
best for you.
I also think "how you want to do OOP in JS" is an implementation issue
as well. I imagine if I really wanted to use Crockford-style
functional inheritance in JavascriptMVC, I could, even if everyone
else thought it was a bad idea. Framework flexibility is a good
thing.
You don't have to respond to every criticism to defend JMVC's honor.
Sometimes we can just agree to disagree.
The main issue is view template languages. And I think you're right
that we have very different application life-cycles in mind. My use
case is a lot smaller than yours, so the optimal solution is probably
different. If I wanted a full application in the browser, with
multiple differently structured partials that render in and disappear
out of the page or full page reloads replaced with JS-initiated full-
page DOM updates, the page would be a lot heavier and would require a
different organization. I think a separate templating language might
be appropriate there.
But if, for example, I have a table with some data in it and a refresh
button, and all I want to happen on refresh it to fetch some data and
repopulate the rows of that table without changing the structure, then
calling out to a separate template language feels like too much when
compared to my "ideal templating system". Is there no room in JMVC
for locally optimized solutions? Or should I build my simple app as
if it could someday become a full-blown Flash-killer RIA that only
ever touches my app server for JSON strings?
Personally, I think my adoption would be easier if there were a way
for me to use bits and pieces of the framework for a single page in my
Rails site and ramp up to the full framework gradually over time.
That's part of the warm and fuzzy feeling I'm looking for in a JS MVC
framework. Chain.js gives me that feeling because it's small enough
that I can imagine playing with it in a single page and seeing how it
works without having to rearchitect my site. I have AJAX calls, I
have roughly annotated divs that get JQuery selected and populated
with the results of that call (usually HTML snippets into innerHTML).
Rails supports JSON APIs well. What if I replace this HTML injection
with a JSON data-binding? That's the inspiration.
ps. There are a lot of things I like about JMVC as well. I like
ActiveRecord-style (if you name your functions appropriately) models
that wrap simple data services. I like controller naming
conventions. I like directory tree conventions. And I like
frameworks that are opinionated.
On Sep 10, 2:59 am, Justin Meyer <
justinbme...@gmail.com> wrote:
> John,
> Yeah, that would be pretty sweet. Chain.js is not that. Maybe something
> for 3.1.
>
> Ed,
>
> Correct me if I'm wrong because otherwise I'm having
> trouble understanding what you mean by "organizing templates with the
> server" and fetching "hidden templates" that represent separate HTML
> partials (or full pages, if you're trying to avoid page loads?).
>
> Ok, I think we have rather different application life-cycles. I care more
> about performance than anything else. I always try to get the page to load
> with initial content. But also, my pages are applications. They last a
> long time and add new content dynamically. I don't know what might happen
> with my page. I could send the page down with a bunch of hidden templates.
> To do this, I would have to make changes in the server code every time
> JavaScript needed a new template. This is in contrast to EJS and such which
> allows you to create a template in say 'foo.ejs' and render it like:
>
> $("#new_bar").html('foo.ejs',{ ... });
>
> That way the JS team can work completely independent of the server. I just
> need a public folder.
>
> Justin Meyer
>
> Jupiter Consulting
> \Development\Training\Support
>
847-924-6039
>
justinbme...@gmail.com
>
>
>
> On Fri, Sep 10, 2010 at 1:46 AM, John Hardy <
jhlag...@gmail.com> wrote:
> > Yes assuming that I am understanding you correctly, a "template" is a
> > complete HTML page which has been mocked up by a designer. In theory it
> > shouldn't even need to be completely empty. In fact it would be better if it
> > was was mocked up with phoney data so that everyone could see what the
> > populated page looks.
>
> > The mockup page would annotated with classes ids and data attributes which
> > map to the view logic. But the designer should be able at any time to take
> > the mockup back and work on it without disrupting the work of the
> > programmer. That is a real separation of concerns.
>
> > Iteration and conditional logic would not need to be explicitly defined. If
> > a div representing a row was mapped to an object it would populate the row
> > using class names as keys. If it was mapped to an array of objects, the div
> > would be repeated and mapped to each array item.
>
> > Not sure if thats what chain.js is about but there's my ideal of a
> > templating system. ;-)
>
> > No logic, no special syntax. Just pure HTML that a designer could create
> > and maintain.
>
> ...
>
> read more »