Created
February 4, 2014 22:11
-
-
Save thcipriani/8813378 to your computer and use it in GitHub Desktop.
Small MC
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
$.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