public
Created

Some real world examples

  • Download Gist
anAjaxPolicyExample.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
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);
}
});
});
}
};
});
backboneViewExample.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
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;
});
jquerypluginexample.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
extend('jquery', function() {
return function($) {
$.fn.myplugin = function () {/*...*/}
return $;
}
});
 
define("fooView", ["jquery"], function ($) {
return {
init: function () {
$("#view").myplugin({/*..*/});
},
destory: function () {
$("#view").empty();
}
}
});

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.