favorite.js
3.32 KB
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
define(['app'], function (app) {
'use strict';
app.factory('$favorite', function ($http, $rootScope, $window) {
var DATA = [];
return {
loadFromRemote: function (params,successHandle,errorHandle) {
/*** load from localstorage for test */
if ($window.localStorage.listFavorite){
DATA = JSON.parse($window.localStorage.listFavorite);
}else{
DATA = [];
}
return;
/*** end test */
// return format: { status:0|1 , data:{} }
var httpObj = $http({
url: $rootScope.API_URL+'/apiv1/favorite',
method: 'GET',
params: params || []
});
if (typeof successHandle == 'undefined'){
successHandle = function(response){
if (response.status==1){
DATA = response.data;
}
}
}
httpObj.success(successHandle);
if (typeof errorHandle != 'undefined'){
httpObj.error(errorHandle);
}
},
addToFavorite: function(params,successHandle,errorHandle) {
/*** load from localstorage for test */
DATA.push(params.data);
$window.localStorage.listFavorite = JSON.stringify(DATA);
return;
/*** end test */
var httpObj = $http({
url: $rootScope.API_URL+'/apiv1/favorite',
method: 'POST',
params: params || []
});
if (typeof successHandle == 'undefined'){
successHandle = function(response){
if (response.status==1){
DATA = response.data;
}
}
}
httpObj.success(successHandle);
if (typeof errorHandle != 'undefined'){
httpObj.error(errorHandle);
}
},
removeFromFavorite: function(params,successHandle,errorHandle) {
/*** load from localstorage for test */
DATA.splice(params.idx,1);
$window.localStorage.listFavorite = JSON.stringify(DATA);
return;
/*** end test */
var httpObj = $http({
url: $rootScope.API_URL+'/apiv1/favorite',
method: 'POST',
params: params || []
});
if (typeof successHandle == 'undefined'){
successHandle = function(response){
if (response.status==1){
DATA = response.data;
}
}
}
httpObj.success(successHandle);
if (typeof errorHandle != 'undefined'){
httpObj.error(errorHandle);
}
},
getAll: function(){
return DATA;
}
};
});
});