2 * Copyright 2013 Samsung Electronics Co., Ltd
4 * Licensed under the Flora License, Version 1.1 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://floralicense.org/license/
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 /*global tizen, window, Alarm, app */
23 var Model = function Model() {
27 (function () { // strict mode wrapper
32 * Initialization function
33 * @param {Object} app object
35 init: function Model_init(app) {
36 var exercises = window.localStorage.getItem('exercises');
39 this.exercises = exercises ? JSON.parse(exercises) : [];
40 this.alarmHelper = new Alarm();
44 * Adds new exercise and save it in local storage and set new alarm
45 * @param {Object} exercise new object
46 * @return {Object|undefined} exercise object
48 add: function Model_saveAlarm(exercise) {
49 var alarmId = this.alarmHelper.add(exercise);
52 exercise.id = alarmId;
54 // add to session storage
55 this.exercises.push(exercise);
57 // add to localStorage
64 * Remove exercise with a given id
65 * @param {String} exerciseId
66 * @return {Boolean} result of remove
68 remove: function Model_remove(exerciseId) {
69 // find exercise to remove
70 var exercise = this.find('id', exerciseId)[0], index;
71 // if exercise remove connected alarm
73 if (this.alarmHelper.remove(exercise)) {
74 // if alarm removed update session store
75 index = this.exercises.indexOf(exercise);
76 this.exercises.splice(index, 1);
87 * Finds list of exercises matching values with given attribute name
88 * @param {String} attr name
89 * @param {*} value of attr
90 * @return {Array} list of exercises
92 find: function Model_find(attr, value) {
93 var result = this.exercises.filter(
95 return el[attr] === value.toString();
102 * Save exercises in local storage
104 updateStorage: function () {
106 window.localStorage.setItem(
108 JSON.stringify(this.exercises)
114 'Not enough memory to save data.' +
115 ' Please remove unnecessary files.'
122 * Returns array of all currently stored exercises
123 * @return {Array} list of exercises
125 getAll: function Model_getAll() {
126 return this.exercises;