Angular.js Server Side

go to site In the previous articles we have went through many features in angular.js.

Provide Assignments Writing Service If you observe them, you can see that in those articles we have never used server side scripting. All of them were based on client side scripting.

go to site Why we are in need of server side scripting?

http://edsonwebsite.com/?p=looking-for-business-plan-writer Every SPA framework out there relies on some type of service (like RESTful web service) to serve and process data. That’s the obvious role that servers can play with SPAs. What is not quite as obvious are the other roles that the server can play as a SPA interacts with it.

Validation:

http://mudanzaspt.com/?help-writing-an-historical-paper Talking about server side scripting , and the first thing comes to our mind is validation. As with client-side security, client-side validation is a frail solution that may not stand on its own. All validation of SPA data has to be validated on the server to ensure that the client-side code, and data haven’t been tampered with.

Service & Caching:

http://fajarsulistya.com/?p=data-analysis-coursework-help The server typically exposes a RESTful service to SPA clients to make it easy to GET, PUT, POST, or DELETE data. This can be done using Node.js, ASP.NET Web API, PHP and many other frameworks using HTTP, Web Sockets, or another technique. As mentioned earlier, this is the most obvious role played by the server when building SPAs.
Some applications consume data that does not change very frequently on the server. This type of data can often be shared across users. Servers are great at caching data so that queries to the data store are minimized. Whether we are using Node.js, ASP.NET MVC, PHP, or one of the many other server-side frameworks, we are likely accessing to caching functionality that can increase the scalability of application significantly.

View Caching:

watch In addition to data caching, the server may also be used to cache HTML views that are retrieved by a SPA. Views may be dynamically generated on the server, and in situations where data does not change often and can be shared across users, the views and data may be cached by the server to allow them to be served in a more efficient and scalable manner. Once views reach the client they can also be cached there for scenarios where a “fresh” view is not required each time a route is triggered. It also relieves load from the application, as well as make the application faster and responsive.

Dynamic Views:

http://protectedevidence.com/?p=professional-writing-and-editing-online The server can once again be used to dynamically generate the view based upon specific security requirements. The authenticated and authorized user should see contents based upon their current roles/permissions.

Security:

go to site Security is a key part of many applications. SPAs that load static HTML views from the server more than likely handle security on the client-side , as well as through the data service exposed by the server. What if HTML view can easily compromise application and data by simply changing client-side variables by a hacker? Browser dev tools make it super easy to tweak HTML, CSS, and JavaScript. Every request to the server should be validated on the server-side to ensure that the user really does have a given role/permission and can perform a specific action.

In the above we tried to explain reasons for using server side scripting.

But how can we accomplish that?

Let us view that topic in the below.

In the below we are going to attempt to make a task manager, which will have features mentioned below.

  • Adding a task
  • Deleting a task
  • Managing a task’s status

To achive this features we are going to need

  • Angular.js as front-end
  • php as backend
  • mysql as database

we are going to discuss only about angular.js and php. I leave the database on to you, so you can work with different data as you wish.

We will arrange the project files of our MVC framework into 5 different folders for better organisation.

  • js/ – Javascript library files. e.g. angular.js
  • app/ – Our custom javascript controller files for our project
  • partials/ – Small pagelets that we wish to reuse
  • ajax/ – The .php files to communicate to server (Connect, Create, Read, Update, Delete)
  • css/ – Stylesheet files

The Stylesheets:

Let us load some css for styling purpose

<link href="css/bootstrap.min.css" rel="stylesheet" type="text/css" />
<link href="css/taskman.css" rel="stylesheet" type="text/css" />

Importing javascript library:

<script type="text/javascript" src="js/angular.min.js"></script>
<script type="text/javascript" src="app/app.js"></script>

The Controller Code (app.js):

//Define an angular module for our app
var app = angular.module('myApp', []);

app.controller('tasksController', function($scope, $http) {
 getTask(); // Load all available tasks
 function getTask(){
   $http.get("ajax/getTask.php").success(function(data){
     $scope.tasks = data;
   });
 };
 $scope.addTask = function (task) {
   $http.get("ajax/addTask.php?task="+task).success(function(data){
     getTask();
     $scope.taskInput = "";
  });
 };
 $scope.deleteTask = function (task) {
   if(confirm("Are you sure to delete this line?")){
     $http.get("ajax/deleteTask.php?taskID="+task).success(function(data){
       getTask();
     });
   }
 };

$scope.toggleStatus = function(item, status, task) {
 if(status=='2'){status='0';}else{status='2';}
   $http.get("ajax/updateTask.php?taskID="+item+"&status="+status).success(function(data){
     getTask();
   });
 };

}); 

Create/Read/Update with PHP:

In the below we have the php files which accomplices the task for create,read,update and delete(CRUD) purpose

business plan powerpoint sample addTask.php

<?php
require_once '../includes/config.php'; // The mysql database connection script
  if(isset($_GET['task'])){
    $task = $_GET['task'];
    $status = "0";
    $created = time();

    $query="INSERT INTO tasks(task,status,created_at) VALUES ('$task', '$status', '$created')";
    $result = $mysqli->query($query) or die($mysqli->error.__LINE__);

    $result = $mysqli->affected_rows;

    echo $json_response = json_encode($result);
}
?>

http://sqr.com.bd/help-high-school-students-homework/ getTask.php

<?php
require_once '../includes/config.php'; // The mysql database connection script
$status = '%';
if(isset($_GET['status'])){
  $status = $_GET['status'];
}
$query="select ID, TASK, STATUS from tasks where status like '$status' order by status,id desc";
$result = $mysqli->query($query) or die($mysqli->error.__LINE__);

$arr = array();
if($result->num_rows > 0) {
  while($row = $result->fetch_assoc()) {
  $arr[] = $row;
 }
}

# JSON-encode the response
echo $json_response = json_encode($arr);
?>

algebra 2 homework practice workbook answers updateTask.php

<?php
require_once '../includes/config.php'; // The mysql database connection script
if(isset($_GET['taskID'])){
  $status = $_GET['status'];
  $taskID = $_GET['taskID'];
  $query="update tasks set status='$status' where id='$taskID'";
  $result = $mysqli->query($query) or die($mysqli->error.__LINE__);

  $result = $mysqli->affected_rows;

  $json_response = json_encode($result);
}
?>

deleteTask.php

<?php
require_once '../includes/config.php'; // The mysql database connection script
if(isset($_GET['taskID'])){
$taskID = $_GET['taskID'];

  $query="delete from tasks where id='$taskID'";
  $result = $mysqli->query($query) or die($mysqli->error.__LINE__);

  $result = $mysqli->affected_rows;

  echo $json_response = json_encode($result);
}
?>

In the above example is a one of the basic models for angular server side scripting. In this article we have just tried to put a basic idea on angular.js.

If you find this article helpful, you can connect us in  help writing 5 paragraph essay Google+ and source site  Twitter.

Leave a Reply

Your email address will not be published. Required fields are marked *