###Magnum JavaScript
###MAG = Modular Application Glue
####Simple dependency injection module service factory
Include the script into your page:
<script type="text/javascript" src="//rawgit.com/magnumjs/mag.js/master/dist/mag.full-0.2.min.js"></script>
Factory, Service, Control
they are inheritable and reusable
var githubUser = mag.module('githubUserApp');
They do maintain state, they are instantiated with a 'new'
githubUser.factory('GithubUser', function() {
var GithubUser = function(data) {
$.extend(this, {
id: null,
collection: [],
status: 'NEW',
isNew: function() {
return (this.status == 'NEW' || this.id == null);
}
});
$.extend(this, data);
};
return GithubUser;
});
They can be injected with factories. Use the [] style arguments when minifying
githubUser.service('GithubUserService', function(GithubUser) {
this.getById = function(userId) {
return $.get('github.json?id=' + userId).then(
function(response) {
return new GithubUser(response);
});
};
});
They can be injected with services or factories. They relay data to the view via the 'Scope' first argument
githubUser.control('gitUserInfo', function(Scope, GithubUserService) {
GithubUserService.getById('magnumjs').done(function(data) {
Scope.id = data.id;
});
});
[Try it out] (http://jsbin.com/hopokibi/edit)