Created
June 5, 2010 06:48
-
-
Save rightjs/426377 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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