Skip to content

Instantly share code, notes, and snippets.

@daifu
Created July 13, 2012 16:18
Show Gist options
  • Select an option

  • Save daifu/3105758 to your computer and use it in GitHub Desktop.

Select an option

Save daifu/3105758 to your computer and use it in GitHub Desktop.
Javascript Web Application BindingModelsUser
<script id="userTmpl" type="text/x-jquery-tmpl">
<li>${name}</li>
</script>
<ul id="users"> </ul>
var User = function(name) {
this.name = name;
};
User.records = [];
User.bind = function(ev, callback) {
var calls = this._callbacks || (this._callbacks = {});
(this._callbacks[ev] || (this._callbacks[ev] = [])).push(callback);
};
User.trigger = function(ev) {
var list, calls, i, l;
if (!(calls = this._callbacks)) return this;
if (!(list = this._callbacks[ev])) return this;
jQuery.each(list, function() {
this()
})
};
User.create = function(name) {
this.records.push(new this(name));
this.trigger("change")
};
jQuery(function($) {
User.bind("change", function() {
var template = $("#userTmpl").tmpl(User.records);
$("#users").empty();
$("#users").append(template);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment