Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@rauhryan
Created February 15, 2012 21:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rauhryan/1839061 to your computer and use it in GitHub Desktop.
Save rauhryan/1839061 to your computer and use it in GitHub Desktop.
Some real world examples
define("jquery", function () {
return {
post: function (url, data, callback) {
callback( {
successful: true,
error: true,
message: "yo dawg!"
});
}
};
});
define("messagebus", function () {
return {
publish : function (topic, data) {
console.log(topic, data);
}
}
});
extend("policies", function () {
return function (policies){
var policy = {
matches : function (context) {
/* always execute */
return true;
},
execute : function (data) {
console.log("data:received", data);
}
}
policies.push(policy);
return policies;
};
});
extend("policies",["messagebus"], function (bus) {
return function (policies){
var policy = {
matches : function (context) {
/* only execute when */
return context.successful;
},
execute : function (data) {
bus.publish("successful", data);
}
}
policies.push(policy);
return policies;
};
});
extend("policies",["messagebus"], function (bus) {
return function (policies){
var policy = {
matches : function (context) {
/* only execute when */
return context.error;
},
execute : function (data) {
bus.publish("failure", data);
}
}
policies.push(policy);
return policies;
};
});
define("policies", function () {
return [];
});
define("fooModel",["jquery","policies"], function ($, policies) {
return {
save: function (attributes) {
$.post("/foo", attributes,function (data) {
policies.forEach(function(policy) {
if(policy.matches(data)){
policy.execute(data);
}
});
});
}
};
});
extend("backboneView", ["jquery","underscore","backbone"], function ($, _, Backbone) {
var extendedView = Backbone.View.extend({/* ... */});
return function (bbView) {
var oldRender = bbView.prototype.render;
bbView.prototype.render = function () {
var rendered = oldRender.call(this,arguments);
var myView = new extendedView();
this.on("someevent", function () {
myView.trigger("someotherevent");
});
return rendered;
}
return bbView;
}
});
define("backboneView", ["jquery","underscore","backbone"], function ($, _, Backbone) {
var bbView = Backbone.View.extend({
render: function () {
/* do stuff */
return this;
}
});
return bbView;
});
extend('jquery', function() {
return function($) {
$.fn.myplugin = function () {/*...*/}
return $;
}
});
define("fooView", ["jquery"], function ($) {
return {
init: function () {
$("#view").myplugin({/*..*/});
},
destory: function () {
$("#view").empty();
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment