Skip to content

Instantly share code, notes, and snippets.

@fredericksilva
Forked from PatrickJS/ng1.TodoService.es5.js
Last active August 29, 2015 14:23
Show Gist options
  • Save fredericksilva/11b2dd77729055998599 to your computer and use it in GitHub Desktop.
Save fredericksilva/11b2dd77729055998599 to your computer and use it in GitHub Desktop.
var angular = require('angular');
// 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
var moduleName = 'app.services.todoservice';
angular.module(moduleName, [])
.value('todoState', _todoState)
.service('TodoService', TodoService)
exports.name = moduleName;
import angular from 'angular';
// Using TypeScript we can define our state interface
interface ITodo {
value: string;
created_at: Date;
completed?: boolean;
}
interface ITodoState {
todos: Array<ITodo>
}
// We can also make a TodoStore
var _todoState:ITodoState = {
todos: [
{ value:'finish example', created_at: new Date() },
{ value:'add tests', created_at: new Date() }
]
};
// Our Todo Service
export class TodoService {
private _state: ITodoState; // we shouldn't access .state directly
static $inject = ['todoState'];
constructor(state) {
this._state = state;
}
get(type) {
return (type) ? this._state[type] : this._state;
}
add(todo) {
this._state.todos.push({
value: todo,
created_at: new Date()
});
}
remove(index) {
this._state.todos.splice(index, 1);
}
}//TodoService
var moduleName = 'app.services.todoservice';
angular.module(moduleName, [])
.value('todoState', _todoState)
.service('TodoService', TodoService)
export default moduleName;
var di = require('angular/di');
// 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
di.annotate(TodoService, new di.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
exports.todoInjectables = [
di.bind('todoState').toValue(_todoState),
bind(TodoService).toClass(TodoService),
// We only have this to mimic Angular 1's di that is limited only to string tokens. Otherwise we would use `TodoService` class as the token
di.bind('TodoService').toAlias(TodoService)
];
import {bind, Inject} from 'angular2/di';
// Using TypeScript we can define our state interface
interface ITodo {
value: string;
created_at: Date;
completed?: boolean;
}
interface ITodoState {
todos: Array<ITodo>
}
// We can also make a TodoStore
var _todoState:ITodoState = {
todos: [
{ value:'finish example', created_at: new Date() },
{ value:'add tests', created_at: new Date() }
]
};
// Our Todo Service
export class TodoService {
private _state: ITodoState; // we shouldn't access .state directly
constructor(@Inject('todoState') state) {
this._state = state;
}
get(type) {
return (type) ? this._state[type] : this._state;
}
add(todo) {
this._state.todos.push({
value: todo,
created_at: new Date()
});
}
remove(index) {
this._state.todos.splice(index, 1);
}
}//TodoService
export const todoInjectables = [
bind('todoState').toValue(_todoState),
bind(TodoService).toClass(TodoService),
// We only have this to mimic Angular 1's di that is limited only to string tokens. Otherwise we would use `TodoService` class as the token
bind('TodoService').toAlias(TodoService)
];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment