Skip to content

Instantly share code, notes, and snippets.

@thcipriani
Created February 4, 2014 22:11
Show Gist options
  • Save thcipriani/8813378 to your computer and use it in GitHub Desktop.
Save thcipriani/8813378 to your computer and use it in GitHub Desktop.
Small MC
$.observable = function(model) {
model.Vent = $({});
var handleEvent = function(vent, args) {
var argArr = Array.prototype.slice.call(args);
model.Vent[vent].apply(model.Vent, argArr);
return model;
};
model.trigger = function() { return handleEvent('trigger', arguments); };
model.on = function() { return handleEvent('on', arguments); };
model.off = function() { return handleEvent('off', arguments); };
model.one = function() { return handleEvent('one', arguments); };
return model;
};
/* Models */
var Orders = function() {
var self = this;
self.new = function(data) {
self.trigger( "new", [data]);
};
self.get = function() {
$.ajax({
method: 'GET',
dataType: 'json',
url: [whatever]
}).done(self.new);
};
$.observable(self);
};
/* Controller */
$(function() {
var orders = new Orders;
orders.get();
/* Listen for events on the model */
orders.on('new', function(e, data) {
console.log("data from the model GET", data);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment