-
-
Save fredericksilva/11b2dd77729055998599 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
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; |
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
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; |
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
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) | |
]; |
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
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