Skip to content

Instantly share code, notes, and snippets.

@james-gardner
Created October 30, 2014 22:28
Show Gist options
  • Save james-gardner/9c942e30d5f187666d47 to your computer and use it in GitHub Desktop.
Save james-gardner/9c942e30d5f187666d47 to your computer and use it in GitHub Desktop.
First attempt at mocking an API using mockjax and factories
(function (global) {
var Blueprint = function () {
};
var p = Blueprint.prototype;
p.generate = function (api) {
var obj = {}, template = this.template(api);
if(this.options.fields) {
_.extend(template, this.options.fields);
}
_.each(template, function (val, key, template) {
if(val instanceof Factory) {
obj[key] = val.build();
} else {
obj[key] = _.result(template, key);
}
}, this);
return obj;
}
global.Blueprint = Blueprint;
}(window));
(function (global) {
var ContactDefault = function (options) {
this.options = options || {};
};
var p = ContactDefault.prototype;
_.extend(p, Blueprint.prototype);
p.template = function (api) {
return {
id : function () {
return api._increment;
},
clinicId : function () {
return _.uniqueId();
}
}
}
global.ContactDefault = ContactDefault;
}(window));
(function (global) {
var Factory = function (config) {
_.bindAll(this, 'build');
this.config = config;
this.output = [];
this._increment = 0;
};
var p = Factory.prototype;
p.generate = function (quantity, worker) {
var output = [];
for(var i = 0; i < quantity; i++, this._increment++) {
output.push(worker.generate(this));
}
return output;
},
p.build = function () {
var output = []; this._increment = 0;
if(_.isArray(this.config)) {
_.each(this.config, function (item) {
output = output.concat(this.generate(item.quantity, item.worker));
}, this);
}
return output;
};
global.Factory = Factory;
}(window));
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Mock API</title>
<script type="text/javascript" src="lib/jquery-2.1.1.min.js"></script>
<script type="text/javascript" src="lib/mockjax.js"></script>
<script type="text/javascript" src="lib/underscore.js"></script>
<script type="text/javascript" src="lib/backbone.js"></script>
<script type="text/javascript" src="lib/faker.js"></script>
<script type="text/javascript" src="blueprint.js"></script>
<script type="text/javascript" src="contact.js"></script>
<script type="text/javascript" src="factory.js"></script>
<script type="text/javascript" src="router.js"></script>
<script type="text/javascript" src="server.js"></script>
</head>
<body>
</body>
</html>
(function (global) {
var MockRouter = function () {
this._routes = [];
};
var p = MockRouter.prototype;
p.addRoute = function (url, handler) {
var route = { url : url};
if(_.isFunction(handler)) {
route.handler = handler;
}
this._routes.push(route);
};
p.getRoute = function (url) {
if(!(url instanceof RegExp)) {
return _.findWhere(this._routes, { url : url });
}
}
global.Router = MockRouter;
}(window))
var router = new Router();
router.addRoute('/api/dummy', new Factory([
{ quantity : 5, worker : new ContactDefault() },
{
quantity : 5,
worker : new ContactDefault({
fields : {
pendingActions : new Factory([{ quantity: 2, worker: new ActionEnquiry() }])
}
})
}
]).build);
$.mockjax(function(settings){
var route = router.getRoute(settings.url);
if (route) {
return {
response: function(settings) {
this.responseText = JSON.stringify(route.handler())
}
};
}
return;
});
var x = new Backbone.Collection();
x.fetch({
url : '/api/dummy'
}).done(function () {
console.log(x.models);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment