我正在嘗試使用AngularJS和Ionic Framework創建一個投票系統。我正在使用ng-repeat循環來自JSON數組的一系列用戶帖子。每個帖子都有一個upvote和一個downvote按鈕。選擇一個後,我禁用該帖子上的按鈕。我目前遇到的問題是無論用戶重新打開該應用程序,該按鈕都將被禁用。我的目標是防止多投票。目前,我正在使用此方法在點擊後禁用按鈕:AngularJS投票系統 - 防止多投票
<button class="button button-small button-light" ng-click="upThread({{thread.id}})" ng-disabled="upDisabled[thread.id]">
<i class="icon ion-thumbsup"></i>
</button>
<button class="button button-small button-light" ng-click="downThread({{thread.id}})" ng-disabled="downDisabled[thread.id]">
<i class="icon ion-thumbsdown"></i>
</button>
$scope.upDisabled = {};
$scope.downDisabled = {};
$scope.upThread = function(id) {
...
$scope.upDisabled[id] = true;
$scope.downDisabled[id] = false;
}
$scope.downThread = function(id) {
...
$scope.downDisabled[id] = true;
$scope.upDisabled[id] = false;
}`
而且這對於禁用按鈕非常有用。現在,問題是,我要求緩存控制器爲false,否則我的新帖子在添加時不會在視圖中更新。這意味着,當重新加載應用程序或在路線/視圖之間切換時,這些按鈕會再次啓用,這不是我想要的。另外,將應用程序重新打開後,將緩存設置爲true並不會使按鈕保持禁用狀態。 我曾嘗試使用localstorage來存儲$ scope.upDisabled和$ scope.downDisabled,但它遺失了JSON格式。我試過遠程存儲並收到相同的結果。例如,$ scope.upDisabled和$ scope.downDisabled存儲數據,像這樣:
{ 「2」:真, 「3」:假 「4」:真}
和等等。但是因爲存儲在本地存儲或數據庫中需要使用JSON.stringify,所以可以猜測這些引號會導致問題,並且最終還會嵌套JSON。所以,來自localstorage的數據不會返回正確的格式。 我已知的問題/選項是雙重的。
- 是否有更好的解決方案來持續禁用這些 按鈕?
- 我該如何正確格式化從 localstorage出來的JSON?
需要注意的一點:由於這是一個使用Ionic的移動/混合應用程序,我沒有使用Cookie,但我確實創建了一個類似的令牌系統。
編輯:感謝那些回覆和你的建議。我目前正在使用MySQL數據庫來跟蹤投票(稍後可能會切換到SQLite)@aorfevre:我已經查看過SQLite插件,並且可能會使用它。我終於得到了一切我想要的東西。
$scope.upDisabled = {};
$scope.downDisabled = {};
$scope.upCheck = {};
$scope.downCheck = {};
...Votes Loading function up here
...Thread loading Function up here
.finally(function(){
angular.forEach($scope.threads,function(value,index){
angular.forEach($scope.upCheck,function(value2,index){
if(value.id == value2.threadID)
{
$scope.upDisabled[value.id] = true;
$scope.downDisabled[value.id] = false;
}
})
})
angular.forEach($scope.threads,function(value,index){
angular.forEach($scope.downCheck,function(value2,index){
if(value.id == value2.threadID)
{
$scope.downDisabled[value.id] = true;
$scope.upDisabled[value.id] = false;
}
})
})
}
$scope.loadVotes();
$scope.loadThreads();
至於服務器端(PHP與Laravel框架):
public function upvoteThread($id, $token)
{
$thread = Thread::find($id);
$score = $thread->score;
$score = $score + 1;
$thread->score = $score;
$thread->save();
$voted = ThreadVote::where('threadID','=',$id)->where('token','=',$token)->get();
if($voted->isEmpty())
{
$upVote = new ThreadVote;
$upVote->threadID = $id;
$upVote->token = $token;
$upVote->upVote = 'true';
$upVote->downVote = 'false';
$upVote->save();
}
else
{
DB::table('thread_votes')
->where('threadID', '=', $id)
->where('token','=',$token)
->update(array('upVote' => 'true', 'downVote' => 'false')
);
}
return Response::json(array('status'=>1))->setCallback(Input::get('callback'));
}
public function getUpVotes($token)
{
$votes = ThreadVote::where('token','=',$token)
->where('upVote','=','true')
->select('threadID','upVote')
->get();
return Response::json($votes)->setCallback(Input::get('callback'));
}
...Similar downVote Function...
所以因爲它的立場。當按下按鈕時,它將用戶的令牌,線程ID和投票保存到數據庫。加載視圖時,基於令牌和threadID的信息將作爲與Thread數組關聯的JSON數組加載,並且如果threadID和thread.id匹配,則將其作爲「threadID」推送到向上/向下的作用域中:布爾(「1」:例如)。
您需要存儲用戶所做的投票。這可以通過cookie,本地存儲或服務器上的數據庫來完成。 – krillgar
需要存儲在數據庫中 - 你不能依靠餅乾/本地存儲 –
@PetrAveryanov:\t數據庫如何比localStorage更好? – dandavis