javascript - JS Function declarations should not be placed in block -


i have wrapped 95% of 'parentctrl' (controller) within 'if' statement, prevents functions triggering if user isn't signed in , authorized. since doing keep getting following jshint error!

error
jshint 104:5 function declarations should not placed in block?

enter image description here

if remove 'if' statement error goes away!

advice on how fix helpful!

if statement

if($scope.user){} 

controller js

var fittingcontrollers = angular.module('fittingcontrollers',[]); // 1. parent controller fittingcontrollers.controller('parentctrl', ['$scope', 'auth', '$localstorage', '$filter', '$timeout', 'notification', '$sce', '$state', function($scope, auth, $localstorage, $filter, $timeout, notification, $sce, $state) {   //// setup authentication   $scope.auth = auth;   $scope.user = $scope.auth.$getauth();   //// facebook login   $scope.facebooklogin = function() {     var scope = {scope: 'email,user_birthday'};     $scope.auth.$authwithoauthpopup('facebook', scope).then(function(auth){       $state.go('main.statistics');     })['catch'](function (error){       console.log(error);     });   };   //// authentication functions   if($scope.user){     //// facebook provider     if($scope.user.provider === 'facebook'){       //// facebook data       $scope.uid = $scope.user.uid;       $scope.displayname = $scope.user.facebook.displayname;       $scope.firstname = $scope.user.facebook.cacheduserprofile.first_name;       $scope.lastname = $scope.user.facebook.cacheduserprofile.last_name;       $scope.dob = $scope.user.facebook.cacheduserprofile.birthday;       $scope.emailaddress = $scope.user.facebook.email;       $scope.profileimageurl = $scope.user.facebook.profileimageurl;     }     //// password provider     else if($scope.user.provider === 'password'){         //// password data tbc     }     //// provider results     var uid = $scope.uid;     var displayname = $scope.displayname;     var firstname = $scope.firstname;     var lastname = $scope.lastname;     var dob = $scope.dob;     var emailaddress = $scope.emailaddress;     var profileimageurl = $scope.profileimageurl;     //// aditional data (test)     var weddingdate = '21/09/2017';     var bodytype = 'triangle';     //// localstorage     $scope.$storage = $localstorage;     //// $localstorage.$reset();     //// storage data     if($scope.$storage.app === undefined) {       $scope.$storage.app = ({         //// user account         'account': [{           //// user information           'userinfo': [             {'id':uid,'username':displayname,'age':dob,'emailaddress':emailaddress,'telephonenumber':'','weddingdate':weddingdate,'bodytype':bodytype}           ]         },{           //// user bodytype           'bodyinfo': [             {'height':524,'chest':32,'waist':30,'hips':32,'thighs':20,'bodytype':''}           ]         },{           //// user love list           'lovelist': [             {'heart': [],'brokenheart': []}           ]         }],         //// app settings         'settings': [{           'intromsg': [             {'lovelist': false},             {'showstyle': false},             {'secondopionion': false}           ]         },{           'introbtn': [             {'lovelist': false},             {'showstyle': false},             {'secondopionion': false}           ]         }],         //// app data         'courtyard': [{           //// dress collection           'dresses': [             {'id':'1','name':'ritva westenius 1','designer':'ritva westenius 2','description':'pellentesque ultricies, sem vel pellentesque tempus, quam massa posuere mauris, gravida semper massa ante non risus. ut scelerisque hendrerit arcu, ac scelerisque mi maximus in. sed non bibendum sapien, ut blandit purus. aenean scelerisque mauris. pellentesque augue augue, feugiat et dignissim sit amet, rhoncus sit amet metus.','bodytype': 'triangle','imageurl': '../img/dress1-body-small@2x.jpg'},             {'id':'2','name':'stephanie allin 1','designer':'stephanie allin 2','description':'vivamus mattis posuere eleifend. etiam vitae mauris neque. sed @ quam quis felis elementum mattis eu id diam.','bodytype':'triangle','imageurl':'../img/dress1-body-medium@2x.jpg'},             {'id':'3','name':'allure bridals 1','designer':'allure bridals 2','description':'phasellus lorem urna, blandit in vestibulum vel, consectetur nec ipsum. pellentesque tellus ipsum, tincidunt eget arcu euismod, vestibulum molestie dui.','bodytype':'round','imageurl': '../img/dress1-body-large@2x.jpg'},             {'id':'10','name':'pronovias barcelona 1','designer':'pronovias barcelona 2','description': 'vestibulum libero odio, suscipit vitae aliquam in, maximus eu purus. morbi luctus elit nec elit euismod iaculis. ut mi nisi, dignissim quis libero id, sagittis rhoncus leo. pellentesque placerat mi non nunc tincidunt vestibulum. suspendisse in ornare tortor.','bodytype':'triangle','imageurl':'../img/dress1-body-large@2x.jpg'},             {'id':'11','name':'chanticleer 1','designer':'chanticleer 2','description':'nulla ante mi, commodo vitae diam sed, aliquam consectetur ex. in maximus nisl urna, non aliquet leo finibus et. donec rhoncus ipsum eget elementum accumsan. nullam id tellus in magna molestie facilisis quis et tellus. nunc nec feugiat ipsum.','bodytype':'triangle','imageurl':'../img/dress1-body-small@2x.jpg'},             {'id':'7','name':'alan hannah 1','designer':'alan hannah 2','description':'etiam non sagittis nisl. suspendisse potenti. quisque sed dolor vitae sem molestie interdum. praesent leo lectus, cursus ac bibendum quis, accumsan in metus. phasellus sagittis, lectus eget iaculis molestie, magna tortor fringilla neque, sit amet ultrices nunc erat sed augue.','bodytype':'triangle','imageurl':'../img/dress1-body-large@2x.jpg'}           ]         }]       });     } else {       $scope.$storage.app = $scope.$storage.app;     }     //// notification function     function notification(){       $timeout(function(){         notification.bethan(           {message:'hi '+firstname+'! you’re not sure do? hover menu in top right , click',positiony:'bottom',positionx:'left',delay:10000,templateurl:"views/notification.html"}         );       }, 3000);       $timeout(function() {         notification.rhiannon(           {message:'this dress perfect triangle body shape yours!',positiony:'bottom',positionx:'left',delay:null,templateurl:"views/notification.html"}         );       }, 6000);     }     //// alert function     function alertmessage(id) {       //// message id       var messageid = id;       //// alert collection       var messages = {         "results": [           {'id':'1','icon':'','button':true,'title':'welcome','subtitle':'hi '+firstname+', welcome virtual fitting room','content':'here able browse through selection of our dresses<br/>and shortlist them future refrence. find best<br/>dress you, please let know bit yourself'},           {'id':'2','icon':'','button':false,'title':'raiding rails','subtitle':'','content':'from measurments have supplied, see have triangle body shape.<br/>please wait while 5 best dresses shape.'},           {'id':'3','icon':'','button':false,'title':'my love list','subtitle':'','content':'any dresses mark &#9829; store<br/> love list can review @ any<br/> point clicking heart icon.'},           {'id':'4','icon':'','button':false,'title':'show me style','subtitle':'','content':'feel free through rest of our<br/> collection , &#9829; like.'},           {'id':'5','icon':'','button':false,'title':'second optionions','subtitle':'','content':'it can tough choose, why not ask<br/> ones know best help<br/>by voting favourites.'}         ]       };       var messageresult = $filter('filter')(messages.results, {id: messageid})[0];       var message = '<h2>'+messageresult.title+'</h2><h3>'+messageresult.subtitle+'</h3><p>'+messageresult.content+'</p>';       $scope.message = $sce.trustashtml(message);       $scope.showmessage = {active: true};       //// close alert       $scope.hidemessage = function() {         $scope.showmessage.active = !$scope.showmessage.active;         notification();       };       $scope.hidebutton = {active: true};       if (messageresult.button === true) {         $scope.hidebutton.active = !$scope.hidebutton.active;       }     }     alertmessage(1);     //// child alert     $scope.$on('eventname', function(event, args) {       var getbodytype = $scope.$storage.app.account[0].userinfo[0].bodytype;       var getid = $filter('filter')($scope.$storage.app.courtyard[0].dresses,{bodytype: getbodytype})[0].id;       $scope.id = args.id;       alertmessage($scope.id);       $timeout(function() {         if ($scope.id === 2) {           $state.go('main.rail', {id: getid});         } else if ($scope.id === 3) {}       }, 3000);       $timeout(function() {         $scope.showmessage = {active: false};       }, 6000);     });     //// menu function     function menu(){       $scope.hidden = false;       //// menu collection       $scope.items = [         {name: 'help',icon: 'svg/help.svg',direction: 'left',backgroundcolor: 'rgb(239, 149, 73)'},         {name: 'book',icon: 'svg/book.svg',direction: 'left',backgroundcolor: 'rgb(95, 205, 155)'},         {name: 'contact',icon: 'svg/contact.svg',direction: 'left',backgroundcolor:'rgb(32, 133, 188)'},         {name: 'share',icon: 'svg/share.svg',direction: 'left',backgroundcolor: 'rgb(102, 181, 215)'},         {name: 'exit',icon: 'svg/exit.svg',direction: 'left',backgroundcolor: 'rgb(255, 206, 97)'}       ];     }     menu();     //// menu triggers     $scope.menubutton = function(button) {       if (button === 'help') {         alert('help');       } else if (button === 'book') {         alert('book');       } else if (button === 'contact') {         alert('contact');       } else if (button === 'share') {         alert('share');       } else if (button === 'exit') {         //// remove notifications         notification.clearall();         //// unauthenticate         $scope.auth.$unauth();         //// state change         $timeout(function() {           //// remove notifications (again)           notification.clearall();           //// state change           $state.go('authentication');         }, 3000);       }     };     //// app buttons (love list, show me style (collection) & second opinion)     $scope.$watch(function() {       return angular.tojson([$scope.$storage.app.settings[1].introbtn[0]]);     }, function() {       if ($scope.$storage.app.settings[1].introbtn[0].lovelist === true) {         $scope.lovelistbtn = true;       }       if ($scope.$storage.app.settings[1].introbtn[0].showstyle === true) {         $scope.showstylebtn = true;       }       if ($scope.$storage.app.settings[1].introbtn[0].secondopionion === true) {         $scope.secondopionionbtn = true;       }     });     //// view navigator (pager)     $scope.pager = function(url) {$state.go(url);};   }   //// authentication end }]); 

take function definition outside if block

function notification()  function alertmessage(id)  function menu() 

javascript not have block scoping, has function scope. so, not matter if write function definition inside if block or not, available throughout enclosing function block.

thus jshint throws error, might give ambiguous meaning reader function available within if block

because of hoisting not matter if put function declaration after call/use within same function block.


Comments

Popular posts from this blog

javascript - Chart.js (Radar Chart) different scaleLineColor for each scaleLine -

apache - Error with PHP mail(): Multiple or malformed newlines found in additional_header -

java - Android – MapFragment overlay button shadow, just like MyLocation button -