From 68426c7ccbe46c3be6ca439d6918ce0bd1a4c400 Mon Sep 17 00:00:00 2001 From: Matteo Pagliazzi Date: Sun, 13 Oct 2013 20:47:59 +0200 Subject: [PATCH] profile edits saved only on click --- public/js/controllers/userCtrl.js | 23 ++++++++++++++++++----- views/options/profile.jade | 10 +++++----- 2 files changed, 23 insertions(+), 10 deletions(-) diff --git a/public/js/controllers/userCtrl.js b/public/js/controllers/userCtrl.js index 1d01cc4888..80a0d1413c 100644 --- a/public/js/controllers/userCtrl.js +++ b/public/js/controllers/userCtrl.js @@ -9,14 +9,27 @@ habitrpg.controller("UserCtrl", ['$scope', '$location', 'User', $scope.toggleHelm = function(val){ User.log({op:'set', data:{'preferences.showHelm':val}}); } + + $scope.$watch('_editing.profile', function(value){ + if(value === true) $scope.editingProfile = angular.copy(User.user.profile); + }); + + $scope.save = function(){ + var values = {}; + _.each($scope.editingProfile, function(value, key){ + // Using toString because we need to compare two arrays (websites) + if($scope.editingProfile[key].toString() !== $scope.profile.profile[key].toString()) values['profile.' + key] = value; + }); + User.setMultiple(values); + $scope._editing.profile = false; + } + $scope.addWebsite = function(){ - if (!User.user.profile.websites) User.user.profile.websites = []; - User.user.profile.websites.push($scope._newWebsite); - User.set('profile.websites', User.user.profile.websites); + if (!$scope.editingProfile.websites) $scope.editingProfile.websites = []; + $scope.editingProfile.websites.push($scope._newWebsite); $scope._newWebsite = ''; } $scope.removeWebsite = function($index){ - User.user.profile.websites.splice($index,1); - User.set('profile.websites', User.user.profile.websites); + $scope.editingProfile.websites.splice($index,1); } }]); diff --git a/views/options/profile.jade b/views/options/profile.jade index 895e9f172c..0f50d9df16 100644 --- a/views/options/profile.jade +++ b/views/options/profile.jade @@ -42,7 +42,7 @@ // ------- Edit ------- .span4 button.btn.btn-default(ng-click='_editing.profile = true', ng-show='!_editing.profile') Edit - button.btn.btn-primary(ng-click='_editing.profile = false', ng-show='_editing.profile') Save + button.btn.btn-primary(ng-click='save()', ng-show='_editing.profile') Save div(ng-show='!_editing.profile') h4 Display Name span(ng-show='profile.profile.name') {{profile.profile.name}} @@ -68,13 +68,13 @@ // TODO use photo-upload instead: https://groups.google.com/forum/?fromgroups=#!topic/derbyjs/xMmADvxBOak .control-group.option-large label.control-label Display Name - input.option-content(type='text', placeholder='Full Name', ng-model='user.profile.name', ng-blur='set("profile.name", user.profile.name)', ) + input.option-content(type='text', placeholder='Full Name', ng-model='editingProfile.name') .control-group.option-large label.control-label Photo Url - input.option-content(type='url', ng-model='user.profile.imageUrl', placeholder='Image Url', ng-blur='set("profile.imageUrl", user.profile.imageUrl)') + input.option-content(type='url', ng-model='editingProfile.imageUrl', placeholder='Image Url') .control-group.option-large label.control-label Blurb - textarea.option-content(style='height:15em;', placeholder='Blurb', ng-model='user.profile.blurb', ng-blur='set("profile.blurb", user.profile.blurb)') + textarea.option-content(style='height:15em;', placeholder='Blurb', ng-model='editingProfile.blurb') include ../shared/formatting-help .control-group.option-large label.control-label Websites @@ -82,7 +82,7 @@ input.option-content(type='url', ng-model='_newWebsite', placeholder='Add Website') ul // would prefer if there were and index in #each, instead using data-website to search with indexOf - li(ng-repeat='website in user.profile.websites') + li(ng-repeat='website in editingProfile.websites') | {{website}} a(ng-click='removeWebsite($index)') i.icon-remove