Skip to content

Instantly share code, notes, and snippets.

@rightjs
Created June 5, 2010 06:48
Show Gist options
  • Save rightjs/426377 to your computer and use it in GitHub Desktop.
Save rightjs/426377 to your computer and use it in GitHub Desktop.
var Nested = new Class({
initialize: function(element){
this.element = element;
this.template = eval(this.element.get('data-template'));
var click_behavior = Event.delegate({
'.add_nested': function(e){
e.stop();
$(e.originalTarget.get('rel'))
.insert(this.getTemplate(), 'bottom')
.lastChild.show('fade');
}.bind(this),
'.remove_nested': function(e){
e.stop();
var item = e.originalTarget.parent('.item');
var link = item.select('.delete');
var options = {};
if (!link.empty()) {
link.first('input').value = 1;
} else {
options.onFinish = item.remove.bind(item);
}
item.hide('fade', options);
}
});
this.element.on('click', click_behavior);
},
getTemplate: function() {
return this._replace_ids(this.template);
},
_replace_ids: function (s){
var new_id = new Date().getTime();
return s.replace(/NEW_RECORD/g, new_id);
}
});
document.on('ready',function(){
var nested_objects = $$('.nested').map(function(element){
return new Nested(element);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment