summaryrefslogtreecommitdiffstats
path: root/third_party/webkit/PerformanceTests/Speedometer/resources/todomvc/architecture-examples/emberjs-debug/source/app/services/repo.js
blob: cffa3858fd77bbba5c435ea5c9c8898ad1773b8a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import Ember from 'ember';
import localStorageMemory from './memory';

export default Ember.Service.extend({
    lastId: 0,
    data: null,
    findAll() {
        return this.get('data') ||
            this.set('data', JSON.parse(window.localStorageMemory.getItem('todos') || '[]'));
    },

    add(attrs) {
        let todo = Object.assign({ id: this.incrementProperty('lastId') }, attrs);
        this.get('data').pushObject(todo);
        this.persist();
        return todo;
    },

    delete(todo) {
        this.get('data').removeObject(todo);
        this.persist();
    },

    persist() {
        window.localStorageMemory.setItem('todos', JSON.stringify(this.get('data')));
    }
});