Tuesday 15 March 2011

php - Creating Page Elements Only If JSON Data is Present -



php - Creating Page Elements Only If JSON Data is Present -

when receive json info server, customize page depending on info json. problem is, if page highly customized need if statement every element needs creating in html.

the question is, there way besides checking every single variable in angularjs? illustration json data:

{ user_logged_in: 'yes', user_custom_background: 'no', user_likes_page: 'yes' }

in angularjs need if statement checking each json variable in order decide if should display on page or not, number of ifs can become exponential.

edit exponential mean page has lot of elements created depending on user info database, retrieved json, each 1 requiring own if statement.

the rending logic be, pseudo code:

if user_logged_in: 'yes' show black `div` if user_custom_background: 'yes' alter background color if user_likes_page: 'yes' display message

the number of ifs huge complex web app.

given using angularjs, should able assign json info server scope variable within controller follows:

$http({ method: 'get', url: '/server/data', }).success(function(response) { $scope.serverresponse = response; });

for testing purposes, skip async phone call , utilize object in question set serverresponse (note followed suggestion @doctus utilize booleans.):

$scope.serverresponse = { user_logged_in: true, user_custom_background: false, user_likes_page: true };

the rest involve html markup changes. based on pseudo code, assume next changes need done:

<div id="black" ng-show="serverresponse.user_logged_in">logged in</div> <div id="background" ng-style="{'background-color': ( serverresponse.user_custom_background ? '#ff0000' : '#00ff00')}">&nbsp;</div> <div id="message" ng-show="serverresponse.user_likes_page">message</div>

here's jsfiddle show in action: http://jsfiddle.net/sy0q263n/

hope helps!

php jquery mysql json angularjs

No comments:

Post a Comment