Last active
October 23, 2015 17:19
-
-
Save lega911/bce33922049929aa6300 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// service = {}; | |
function controller(scope) { | |
service.todo.get(); | |
// or $todo = service.todo | |
// or require('todoService', function($todo) {} | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} | |
})(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// <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