forked from tastejs/todomvc
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Update for spec complience * Polymer 0.5 -> 1 changes
- Loading branch information
Showing
5 changed files
with
257 additions
and
138 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,77 +1,85 @@ | ||
<link rel="import" href="../bower_components/polymer/polymer.html"> | ||
|
||
<polymer-element name="td-model" attributes="filter items storageId"> | ||
<dom-module id="td-model"> | ||
<template> | ||
<iron-localstorage name="todos-polymer" value="{{items}}" on-iron-localstorage-load-empty="initializeDefaultTodos"></iron-localstorage> | ||
</template> | ||
<script> | ||
Polymer('td-model', { | ||
filtered: null, | ||
completedCount: 0, | ||
activeCount: 0, | ||
allCompleted: false, | ||
ready: function() { | ||
this.asyncMethod(function() { | ||
this.items = this.storage.value || []; | ||
}); | ||
}, | ||
filterChanged: function() { | ||
this.asyncMethod(function() { | ||
this.filterItems(); | ||
}); | ||
}, | ||
itemsChanged: function() { | ||
this.completedCount = this.items.filter(this.filters.completed).length; | ||
this.activeCount = this.items.length - this.completedCount; | ||
this.allCompleted = this.completedCount && !this.activeCount; | ||
this.filterItems(); | ||
if (this.storage) { | ||
this.storage.value = this.items; | ||
this.storage.save(); | ||
} | ||
}, | ||
storageIdChanged: function() { | ||
this.storage = document.querySelector('#' + this.storageId); | ||
if (this.storage) { | ||
this.items = this.storage.value; | ||
} | ||
}, | ||
filterItems: function() { | ||
var fn = this.filters[this.filter]; | ||
this.filtered = fn ? this.items.filter(fn) : this.items; | ||
}, | ||
newItem: function(title) { | ||
title = String(title).trim(); | ||
if (title) { | ||
var item = { | ||
(function() { | ||
'use strict'; | ||
|
||
Polymer({ | ||
is: 'td-model', | ||
properties: { | ||
items: { | ||
type: Array, | ||
notify: true | ||
}, | ||
filter: String | ||
}, | ||
initializeDefaultTodos: function() { | ||
this.items = []; | ||
}, | ||
newItem: function(title) { | ||
title = String(title).trim(); | ||
|
||
if (!title) { | ||
return; | ||
} | ||
|
||
this.push('items', { | ||
title: title, | ||
completed: false | ||
}; | ||
this.items.push(item); | ||
this.itemsChanged(); | ||
} | ||
}, | ||
destroyItem: function(item) { | ||
var i = this.items.indexOf(item); | ||
if (i >= 0) { | ||
this.items.splice(i, 1); | ||
} | ||
this.itemsChanged(); | ||
}, | ||
clearItems: function (){ | ||
this.items = this.items.filter(this.filters.active); | ||
}, | ||
setItemsCompleted: function(completed) { | ||
this.items.forEach(function(item) { | ||
item.completed = completed; | ||
}); | ||
this.itemsChanged(); | ||
}, | ||
filters: { | ||
active: function(item) { | ||
return !item.completed; | ||
}); | ||
}, | ||
getCompletedCount: function(items) { | ||
return items === null ? 0 : items.filter(this.filters.completed).length | ||
}, | ||
getActiveCount: function(items) { | ||
return items.length - this.getCompletedCount(items); | ||
}, | ||
areAllCompleted: function(items) { | ||
return this.getCompletedCount(items) && !this.getActiveCount(items) ? true : false; | ||
}, | ||
matchesFilter: function(item, filter) { | ||
var fn = this.filters[filter]; | ||
return this.filtered = fn ? fn(item) : true; | ||
}, | ||
destroyItem: function(item) { | ||
var i = this.items.indexOf(item); | ||
|
||
i !== -1 && this.splice('items', i, 1); | ||
}, | ||
clearCompletedItems: function (){ | ||
this.items = this.items.filter(this.filters.active); | ||
}, | ||
setItemsCompleted: function(completed) { | ||
// Since we are mutating elements in an array here | ||
// and we want everyone to know about it we must go through | ||
// the polymer internal `splice` api. The fix that comes to my mind here | ||
// would be to use a hash or set to represent this structure so that the mutations | ||
// would happening on an object with real key value pairs instead of an object | ||
// nested inside of an array | ||
// Polymer array mutation docs: https://www.polymer-project.org/1.0/docs/devguide/properties.html#array-mutation | ||
this.items.forEach(function(item, i) { | ||
if (this.filter) { | ||
if (this.filters[this.filter](item)) { | ||
this.splice('items', i, 1, {title: item.title, completed: completed}); | ||
} | ||
} else { | ||
this.splice('items', i, 1, {title: item.title, completed: completed}); | ||
} | ||
}, this); | ||
}, | ||
completed: function(item) { | ||
return item.completed; | ||
filters: { | ||
active: function(item) { | ||
return !item.completed; | ||
}, | ||
completed: function(item) { | ||
return item.completed; | ||
} | ||
} | ||
} | ||
}); | ||
}); | ||
})(); | ||
</script> | ||
</polymer-element> | ||
</dom-module> |
Oops, something went wrong.