Skip to content

Instantly share code, notes, and snippets.

@diverted247
Created March 21, 2014 16:48
Show Gist options
  • Select an option

  • Save diverted247/9690479 to your computer and use it in GitHub Desktop.

Select an option

Save diverted247/9690479 to your computer and use it in GitHub Desktop.
TypeScript + VueJS - Module Path Attempt
/// <reference path='../../../tools/vue/vue.0.10.0RC.d.ts'/>
var Router:any;
module app{
export module sorting{
export var ALL = 'all';
export var ACTIVE = 'active';
export var COMPLETED = 'completed';
export function all( todo ){
return true;
}
export function active( todo ){
return !todo.completed;
}
export function completed( todo ){
return todo.completed;
}
}
export module storage{
export var STORAGE_KEY = 'todos-vuejs';
export var todos = null;
export function fetch(){
if( !todos ){
todos = JSON.parse( localStorage.getItem( STORAGE_KEY ) || '[]' );
}
return todos;
}
export function save(){
localStorage.setItem( STORAGE_KEY , JSON.stringify( todos ) );
}
}
export module data{
export var todos= [];
export var newTodo = '';
export var editedTodo = null;
export var filter = 'all';
}
export module directives{
export function todo_focus( value ){
if( !value ){
return;
}
var el = app.vue.$el;
setTimeout( function (){
el.focus();
} , 0 );
}
}
export module filters{
export function filterTodos( todos ){
return todos.filter( app.sorting[ app.vue.filter ] );
}
}
export module computed{
export function remaining(){
return app.vue.todos.filter( app.sorting.active ).length;
}
export var allDone = {
$get: function(){
return app.vue.remaining === 0;
},
$set: function( value ){
app.vue.todos.forEach( function( todo ){
todo.completed = value;
});
app.storage.save();
}
}
}
export module methods{
export function addTodo(){
console.log( 'addTodo')
var value = app.vue.newTodo && app.vue.newTodo.trim();
if( !value ){
return;
}
app.vue.todos.push( { title: value, completed: false } );
app.vue.newTodo = '';
app.storage.save();
}
export function removeTodo( todo ){
app.vue.todos.$remove( todo.$data );
app.storage.save();
}
export function toggleTodo( todo ){
app.storage.save();
}
export function editTodo( todo ){
app.vue.beforeEditCache = todo.title;
app.vue.editedTodo = todo;
}
export function doneEdit( todo ){
if( !app.vue.editedTodo ){
return;
}
app.vue.editedTodo = null;
todo.title = todo.title.trim();
if( !todo.title ){
app.vue.removeTodo( todo );
}
app.storage.save();
}
export function cancelEdit( todo ):void{
app.vue.editedTodo = null;
todo.title = app.vue.beforeEditCache;
}
export function removeCompleted():void{
app.vue.todos = app.vue.todos.filter( app.sorting.active );
app.storage.save();
}
}
export var vue:VueAppModel;
export var router:any;
export function init(){
vue = new Vue( app );
vue.todos = app.storage.fetch();
router = new Router();
router.on( app.sorting.ALL , function (){ vue.filter = app.sorting.ALL; } );
router.on( app.sorting.ACTIVE , function (){ vue.filter = app.sorting.ACTIVE; } );
router.on( app.sorting.COMPLETED , function (){ vue.filter = app.sorting.COMPLETED; } );
router.init();
}
}
interface VueAppModel{
todos:any[];
newTodo:string;
editedTodo:any;
filter:string;
addTodo():void;
remaining:number;
removeCompleted():void;
cancelEdit( todo:any ):void;
removeTodo( todo:any ):void;
toggleTodo( todo:any ):void;
beforeEditCache:string;
}
app.init()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment