把activities列表存起来,防止返回时页面变空

This commit is contained in:
guange 2016-04-06 15:19:18 +08:00
parent 606599047d
commit 70c2c9cc9f
1 changed files with 49 additions and 35 deletions

View File

@ -1,19 +1,19 @@
var app = angular.module('wechat', ['ngRoute','ngCookies']); var app = angular.module('wechat', ['ngRoute','ngCookies']);
var apiUrl = 'http://wechat.trustie.net/api/v1/'; var apiUrl = 'http://wechat.trustie.net/api/v1/';
var debug = false; //调试标志,如果在本地请置为true var debug = true; //调试标志,如果在本地请置为true
app.factory('auth', function($http,$routeParams, $cookies){ app.factory('auth', function($http,$routeParams, $cookies, $q){
var _openid = ''; var _openid = '';
if(debug===true){ if(debug===true){
_openid = "oCnvgvz8R7QheXE-R9Kkr39j8Ndg"; _openid = "oCnvgvz8R7QheXE-R9Kkr39j8Ndg";
} }
var getOpenId = function(cb) { var getOpenId = function() {
var deferred = $q.defer();
if (typeof _openid !== 'undefined' && _openid.length > 0) { if (typeof _openid !== 'undefined' && _openid.length > 0) {
cb(_openid); deferred.resolve(_openid);
return; } else {
}
var code = $routeParams.code; var code = $routeParams.code;
$http({ $http({
url: '/wechat/get_open_id', url: '/wechat/get_open_id',
@ -30,12 +30,12 @@ app.factory('auth', function($http,$routeParams, $cookies){
_openid = $cookies.get('openid'); _openid = $cookies.get('openid');
} }
} }
deferred.resolve(_openid);
cb(_openid);
}, function errorCallback(response) { }, function errorCallback(response) {
cb(null); deferred.reject(response);
}); });
}
return deferred.promise;
}; };
var openid = function(){ var openid = function(){
return _openid; return _openid;
@ -43,14 +43,27 @@ app.factory('auth', function($http,$routeParams, $cookies){
return {getOpenId: getOpenId, openid: openid}; return {getOpenId: getOpenId, openid: openid};
}); });
app.controller('ActivityController',function($scope, $http, auth){ app.factory('rms', function(){
var _saveStorage = {};
var save = function(key, value){
_saveStorage[key] = value;
};
var get = function(key){
return _saveStorage[key];
}
return {save: save, get: get};
});
app.controller('ActivityController',function($scope, $http, auth, rms){
$scope.repaceUrl = function(url){ $scope.repaceUrl = function(url){
return "http://www.trustie.net/" + url; return "http://www.trustie.net/" + url;
} }
console.log("ActivityController load"); console.log("ActivityController load");
$scope.activities = []; $scope.activities = rms.get("activities") || [];
$scope.page = 1; $scope.page = 1;
var loadActData = function(page){ var loadActData = function(page){
@ -61,17 +74,18 @@ app.controller('ActivityController',function($scope, $http, auth){
data: {openid: auth.openid(), page: page}, data: {openid: auth.openid(), page: page},
}).then(function successCallback(response) { }).then(function successCallback(response) {
$scope.activities = $scope.activities.concat(response.data.data); $scope.activities = $scope.activities.concat(response.data.data);
rms.save('activities', $scope.activities);
}, function errorCallback(response) { }, function errorCallback(response) {
}); });
} }
auth.getOpenId().then(
auth.getOpenId(function(oid){ function successCallback(response){
if(!oid){
alert("获取openid出错");
} else {
loadActData($scope.page); loadActData($scope.page);
}, function errorCallback(response) {
alert("获取openid出错:"+response);
} }
}); );
$scope.loadActData = loadActData; $scope.loadActData = loadActData;