views:

58

answers:

2

This is a rephrase of a post I made last week. I was surprised I didn't get more of a response, thinking maybe I didn't describe/title it very well.

Simplest possible way to describe it:

Is there something like spring:bind for javascript/dhtml?

The high level view of what I want:

In my app, I have a list of "subscriber" objects which I've loaded via ajax. I'd like to dynamically draw UI elements representing these objects, and register event handlers so that the backing objects are updated when the user edits the view.

The more low level thoughts

Dynamically displaying js data in the browser is trivial. It's also not a big problem to write event handlers for every type of data, but it's a bit tedious. I feel like there should be some library out there which will behave as a template system, creating HTML form elements dynamically, populating placeholders with data from my js objects, but go to the additional step of updating the backing objects when the user makes edits to the form elements. The closest example I'm aware of is a back-end technology, the Spring (java framework) binding functionality, where form elements in templates are coded according to a system where they're automatically linked to the model objects on the server.

So, does what I've described exist in the front-end world?

+1  A: 

I do not know about libraries of the kind you described, but jQuery offers the very useful data() function which allows you to associate an arbitrary piece of data with a DOM element, using a key. Adding a new element and associating data with it might look like this:

var elem = $('<div class="subscriber">...</div>').appendTo(someContainer);
elem.data('yourKey', backingObject);

Using event delegation (e.g., the live() function), you can add one event handler that is valid for all graphical representations of your subscriber objects, no matter if they already exist or not.

$('.subscriber').live('click', function(e) {
    var backingObject = $(this).data('yourKey');
    // Now call some methods on the backing object
});

Hope this helps you somehow. Cheers, Tom

Tom Bartel
Thanks Tom,I'm too lazy to write the "// Now call some methods" stuff though! I want all of that to be wired up for me automagically! Let's say "subscriber" has first name, last name, and email address. I'd like to not have to write the code that copies the value from the "email address" gui element to the backing object. Sure seems like such a technology ought to exist already.
morgancodes
I see. Form generation, including input handling routines. Maybe add this aspect to your question, I couldn't infer that. Interesting, the whole thing.
Tom Bartel
Thanks for the feedback. I've edited the question. Hopefully it's a bit clearer now.
morgancodes
A: 

This is not an answer, but a larger comment. It might help to write the API you want to use, such as:

var jsObj = {};

jsObj.bind(document.getElementById("firstName"), "onchange");
//binds the data from the firstName element to the jsObject in property "firstName" during the onchange event.

Is this what you are trying to do?

A quick google search turns up: http://api.jquery.com/bind/

It might be interesting to make a delegate to jQuery's bind, which doesn't see to hard to do.

jon077
I want something that works just like Spring binding. Create a template. Give attributes to elements in the template that link them to the data model, render the template, automatically attaching event handlers. So my template might have something like <div>#address.street#</div> in it. The template renderer would replace the ## text, and attach an event handler to the containing div which would a) make it editable on click and b) propagate changes to the text back to the back object.
morgancodes