Commit a514894f authored by Aaron Boushley's avatar Aaron Boushley

Added localStorage persistence for ember.js.

This is probably not the most graceful implementation, but it should get the job done for the time being.
parent fa6a2630
......@@ -171,10 +171,6 @@ body {
padding: 20px 0 30px 0;
line-height: 1;
}
#stats-area{
display:none;
}
#create-todo {
position: relative;
}
......
Todos = Ember.Application.create();
Todos.Todo = Ember.Object.extend({
id: null,
title: null,
isDone: false
isDone: false,
todoChanged: function () {
Todos.TodoStore.update(this);
}.observes('title', 'isDone')
});
Todos.todosController = Ember.ArrayProxy.create({
......@@ -12,9 +16,20 @@ Todos.todosController = Ember.ArrayProxy.create({
var todo = Todos.Todo.create({ title: title }),
stats = document.getElementById('stats-area');
this.pushObject(todo);
Todos.TodoStore.create(todo);
(stats.style.display=='block')? stats.style.display = 'inline' : stats.style.display = 'block';
},
pushObject: function (item, ignoreStorage) {
if (!ignoreStorage)
Todos.TodoStore.create(item);
return this._super(item);
},
removeObject: function (item) {
Todos.TodoStore.remove(item);
return this._super(item);
},
clearCompletedTodos: function() {
......@@ -65,3 +80,83 @@ Todos.CreateTodoView = Ember.TextField.extend({
}
});
Todos.TodoStore = (function () {
// Generate four random hex digits.
var S4 = function () {
return (((1+Math.random())*0x10000)|0).toString(16).substring(1);
};
// Generate a pseudo-GUID by concatenating random hexadecimal.
var guid = function () {
return (S4()+S4()+"-"+S4()+"-"+S4()+"-"+S4()+"-"+S4()+S4()+S4());
};
// Our Store is represented by a single JS object in *localStorage*. Create it
// with a meaningful name, like the name you'd give a table.
var Store = function(name) {
this.name = name;
var store = localStorage.getItem(this.name);
this.data = (store && JSON.parse(store)) || {};
// Save the current state of the **Store** to *localStorage*.
this.save = function() {
localStorage.setItem(this.name, JSON.stringify(this.data));
};
// Add a model, giving it a (hopefully)-unique GUID, if it doesn't already
// have an id of it's own.
this.create = function (model) {
if (!model.get('id')) model.set('id', guid());
return this.update(model);
};
// Update a model by replacing its copy in `this.data`.
this.update = function(model) {
this.data[model.get('id')] = model.getProperties('id', 'title', 'isDone');
;
this.save();
return model;
};
// Retrieve a model from `this.data` by id.
this.find = function(model) {
return Todos.Todo.create(this.data[model.get('id')]);
};
// Return the array of all models currently in storage.
this.findAll = function() {
var result = [];
for (var key in this.data)
{
var todo = Todos.Todo.create(this.data[key]);
result.push(todo);
}
return result;
};
// Delete a model from `this.data`, returning it.
this.remove = function(model) {
delete this.data[model.get('id')];
this.save();
return model;
};
};
return new Store('todos-emberjs');
})();
(function () {
var items = Todos.TodoStore.findAll();
if (items.length < 1)
{
var todo = Todos.Todo.create({'title': 'First Task'});
Todos.TodoStore.create(todo);
items = [todo];
}
Todos.todosController.arrayContentWillChange(0, 0, items.length);
Todos.todosController.set('[]', items);
Todos.todosController.arrayContentDidChange(0, 0, items.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