Commit 4ecf41db authored by Pascal Hartig's avatar Pascal Hartig

CanJS: Replaced localstorage plugin with component

parent 44dc7cd0
{
"name": "todomvc-canjs",
"version": "0.0.0",
"dependencies": {
"jquery": "~1.9.1",
"canjs": "~1.1.4",
"canjs-localstorage": "~0.1.0"
}
}
can.Model('can.Model.LocalStorage', {
// Implement local storage handling
localStore : function (cb) {
var name = this.name,
data = JSON.parse(window.localStorage[name] || (window.localStorage[name] = '[]')),
res = cb.call(this, data);
if (res !== false) {
can.each(data, function (todo) {
delete todo.editing;
});
window.localStorage[name] = JSON.stringify(data);
}
},
findAll : function (params) {
var def = new can.Deferred();
this.localStore(function (todos) {
var instances = [],
self = this;
can.each(todos, function (todo) {
instances.push(new self(todo));
});
def.resolve({data : instances});
});
return def;
},
destroy : function (id) {
var def = new can.Deferred();
this.localStore(function (todos) {
for (var i = 0; i < todos.length; i++) {
if (todos[i].id === id) {
todos.splice(i, 1);
break;
}
}
def.resolve({});
});
return def;
},
create : function (attrs) {
var def = new can.Deferred();
this.localStore(function (todos) {
attrs.id = attrs.id || parseInt(100000 * Math.random(), 10);
todos.push(attrs);
});
def.resolve({id : attrs.id});
return def;
},
update : function (id, attrs) {
var def = new can.Deferred(), todo;
this.localStore(function (todos) {
for (var i = 0; i < todos.length; i++) {
if (todos[i].id === id) {
todo = todos[i];
break;
}
}
can.extend(todo, attrs);
});
def.resolve({});
return def;
}
}, {});
\ No newline at end of file
......@@ -17,10 +17,10 @@
<script src="components/jquery/jquery.js"></script>
<script src="components/todomvc-common/base.js"></script>
<script src="components/canjs/can.jquery.js"></script>
<script src="components/canjs-localstorage/can.localstorage.js"></script>
<script src="js/lib/can.mustache.min.js"></script>
<script src="js/lib/can.localstorage.min.js"></script>
<script src="js/models/todo.js"></script>
<script src="js/todos/todos.js"></script>
<script src="js/app.js"></script>
......
can.Model("can.Model.LocalStorage",{localStore:function(b){var c=this.storageName,a=JSON.parse(window.localStorage[c]||(window.localStorage[c]="[]"));!1!==b.call(this,a)&&(can.each(a,function(a){delete a.editing}),window.localStorage[c]=JSON.stringify(a))},findAll:function(){var b=new can.Deferred;this.localStore(function(c){var a=[],d=this;can.each(c,function(b){a.push(new d(b))});b.resolve({data:a})});return b},destroy:function(b){var c=new can.Deferred;this.localStore(function(a){for(var d=0;d<
a.length;d++)if(a[d].id===b){a.splice(d,1);break}c.resolve({})});return c},create:function(b){var c=new can.Deferred;this.localStore(function(a){b.id=b.id||parseInt(1E5*Math.random(),10);a.push(b)});c.resolve({id:b.id});return c},update:function(b,c){var a=new can.Deferred,d;this.localStore(function(a){for(var e=0;e<a.length;e++)if(a[e].id===b){d=a[e];break}can.extend(d,c)});a.resolve({});return a}},{});
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