Skip to content

Instantly share code, notes, and snippets.

@lega911
Last active October 23, 2015 17:19
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 lega911/bce33922049929aa6300 to your computer and use it in GitHub Desktop.
Save lega911/bce33922049929aa6300 to your computer and use it in GitHub Desktop.
// service = {};
function controller(scope) {
service.todo.get();
// or $todo = service.todo
// or require('todoService', function($todo) {}
};
service.todo = (function() {
var todos = [
{ value:'finish example', created_at: new Date() },
{ value:'add tests', created_at: new Date() }
];
return {
get: function() {
return todos;
},
add: function(todo) {
todos.push({
value: todo,
created_at: new Date()
})
},
remove: function(index) {
todos.splice(index, 1);
}
}
})();
// <script src="angular.min.js"></script>
(function(name, factory) {
// our basic IO module system that stores every module on modules with the "file" namespace
// please use something like browserify rather than rolling your own like this
window.modules = window.modules || {};
window.require = window.require || function require(name) { return window.modules[name] || window[name]; };
var exports = {}; factory(exports, window.require);
window.modules[name] = exports;
}('TodoService', function(exports, require) {
var angular = require('require');
// We can also make a TodoStore
var _todoState = {
todos: [
{ value:'finish example', created_at: new Date() },
{ value:'add tests', created_at: new Date() }
]
};
// Our Todo Service
TodoService.$inject = ['todoState'];
exports['TodoService'] = function TodoService(state) {
this.state = state;
}
TodoService.prototype.get = function get(type) {
return (type) ? this.state[type] : this.state;
}
TodoService.prototype.add = function add(todo) {
this.state.todos.push({
value: todo,
created_at: new Date()
});
}
TodoService.prototype.remove = function remove(index) {
this.state.todos.splice(index, 1);
}
// TodoService
// Dependency injection
var moduleName = 'app.services.todoservice';
exports['ngModule'] = angular.
module(moduleName, []).
value('todoState', _todoState).
service('TodoService', TodoService)
exports['name'] = moduleName;
}));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment