Commit d8731b8d authored by Sindre Sorhus's avatar Sindre Sorhus

jQuery: Extract store() method to Utils

parent 1f2793f1
...@@ -7,13 +7,21 @@ jQuery(function( $ ) { ...@@ -7,13 +7,21 @@ jQuery(function( $ ) {
uuid: function(a,b){for(b=a='';a++<36;b+=a*51&52?(a^15?8^Math.random()*(a^20?16:4):4).toString(16):'-');return b}, uuid: function(a,b){for(b=a='';a++<36;b+=a*51&52?(a^15?8^Math.random()*(a^20?16:4):4).toString(16):'-');return b},
pluralize: function( count, word ) { pluralize: function( count, word ) {
return count === 1 ? word : word + 's'; return count === 1 ? word : word + 's';
},
store: function( namespace, data ) {
if ( arguments.length > 1 ) {
return localStorage.setItem( namespace, JSON.stringify( data ) );
} else {
var store = localStorage.getItem( namespace );
return ( store && JSON.parse( store ) ) || [];
}
} }
}; };
var App = { var App = {
init: function() { init: function() {
this.ENTER_KEY = 13; this.ENTER_KEY = 13;
this.todos = this.store(); this.todos = Utils.store('todos-jquery');
this.cacheElements(); this.cacheElements();
this.bindEvents(); this.bindEvents();
this.render(); this.render();
...@@ -30,14 +38,6 @@ jQuery(function( $ ) { ...@@ -30,14 +38,6 @@ jQuery(function( $ ) {
this.$count = $('#todo-count'); this.$count = $('#todo-count');
this.$clearBtn = $('#clear-completed'); this.$clearBtn = $('#clear-completed');
}, },
store: function( data ) {
if ( arguments.length ) {
return localStorage.setItem( 'todo-jquery', JSON.stringify( data ) );
} else {
var store = localStorage.getItem('todo-jquery');
return ( store && JSON.parse( store ) ) || [];
}
},
bindEvents: function() { bindEvents: function() {
var list = this.$todoList; var list = this.$todoList;
this.$newTodo.on( 'keyup', this.create ); this.$newTodo.on( 'keyup', this.create );
...@@ -54,7 +54,7 @@ jQuery(function( $ ) { ...@@ -54,7 +54,7 @@ jQuery(function( $ ) {
this.$main.toggle( !!this.todos.length ); this.$main.toggle( !!this.todos.length );
this.$toggleAll.prop( 'checked', !this.activeTodoCount() ); this.$toggleAll.prop( 'checked', !this.activeTodoCount() );
this.renderFooter(); this.renderFooter();
this.store( this.todos ); Utils.store( 'todos-jquery', this.todos );
}, },
renderFooter: function() { renderFooter: function() {
var todoCount = this.todos.length, var todoCount = this.todos.length,
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment