Broadcast model changes

I'm trying to set up a little POC to see if angular will work for something I'm in the middle.

I have set up a REST server that I can use CRUD using angular. However, since the documentation and tutorials are so all over there (read: SUPER is incompatible), I'm not sure if the behavior I'm not seeing is the result of incorrect code, or it's not something I can do like this.

I gleaned from the docs that two-way binding is available, but it's not clear how this works. NB I have read dozens of articles explaining how it works at a low level a'la on this stackoverflow question but could not answer my own question.

I have an angular call to a REST service that is modifying a sql db.

What I am interested in and I am trying to POC if I have 2 browsers open and I change the value in the db, will it be reflected in another browser window?

As I said, I have a db update, but at the moment it is not updating another browser window.

app.js

angular.module('myApp', ['ngResource']);

var appMock = angular.module('appMock', ['myApp', 'ngMockE2E']);
appMock.run(function($httpBackend) {});

      

controllers.js

function MainCtrl($scope, $http, $resource) {
  $scope.message = "";
  $scope.fruits = [];
  $scope.fruit = {};
  $scope.view = 'partials/list.html';

  var _URL_ = '/cirest/index.php/rest/fruit';


  function _use_$resources_() { return false; }
  function _fn_error(err) {
    $scope.message = err;
  } 

  $scope.listFruits = function() {

    $scope.view = 'partials/list.html';

    var fn_success = function(data) {
        $scope.fruits = data;
    };

    $http.get(_URL_).success(fn_success).error(_fn_error);

  }


  function _fn_success_put_post(data) {
        $scope.fruit = {};
        $scope.listFruits();  
  }

  function createFruit() {
        $http.post(_URL_, $scope.fruit).success(function(data){
        $scope.listFruits()
      }).error(_fn_error);

  }


  function updateFruit() {
      $http.post(_URL_, $scope.fruit).success(_fn_success_put_post).error(_fn_error);
  }

  function deleteFruit() {

      $http.put(_URL_, $scope.fruit).success(_fn_success_put_post).error(_fn_error);

  }

 $scope.delete = function(id) {
    if (!confirm("Are you sure you want do delete the fruit?")) return; 
        $http.delete("/cirest/index.php/rest/fruit?id=" + id).success(_fn_success_put_post).error(_fn_error);

  }


  $scope.newFruit = function() {
    $scope.fruit = {};
    $scope.fruitOperation = "New fruit";
    $scope.buttonLabel = "Create";
    $scope.view = "partials/form.html";
  }

  $scope.edit = function(id) {
    $scope.fruitOperation = "Modify fruit";
    $scope.buttonLabel = "Save";

    $scope.message = "";

    var fn_success = function(data) {
        $scope.fruit = {};
        $scope.fruit.id = id;
        $scope.view = 'partials/form.html';
    };


      $http.get(_URL_ + '/' + id).success(fn_success).error(_fn_error);

  }


  $scope.save = function() {
    if ($scope.fruit.id) {
      updateFruit();
    }
    else {
      createFruit();
    }
  }

  $scope.cancel = function() {
    $scope.message = "";
    $scope.fruit = {};
    $scope.fruits = [];
    $scope.listFruits();    
  }

  $scope.listFruits();
}
MainCtrl.$inject = ['$scope', '$http', '$resource'];

      

list.html

{{message}}
<hr/>
   <a href="" ng-click="newFruit()">New Fruit</a>  
   <ul ng-model="listFruit">
      <li ng-repeat="fruit in fruits">
        <a href="" ng-click="edit(fruit.id)">id [{{fruit.id}}] {{fruit.name}} is {{fruit.color}}</a>
        [<a href="" ng-click="delete(fruit.id)">X</a>]
    </li>
   </ul>      

      

index.html

<!doctype html>
<html lang="en" ng-app="myApp">
<head>
  <meta charset="utf-8">
  <title>FRUUUUUUUUUUUUUUUUUUUUUUUUUUUIT</title>
  <link rel="stylesheet" href="css/bootstrap/css/bootstrap.css"/>
</head>
<body>
  <div class="navbar">NAVBARRRRRRRRRRR</div>
  <div class="container">
    <div class="row">
      <div ng-controller="MainCtrl">

          <button ng-click="listFruits()">ListFruit()</button>
          <button ng-click="cancel()">Cancel()</button>


                <ng-include src="view"></ng-include>          

        </div>

    </div>
  </div>


  <!-- In production use:
  <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.0.4/angular.min.js"></script>
  -->
  <script src="lib/angular/angular.js"></script>
  <script src="lib/angular/angular-resource.js"></script>
  <script src="js/app.js"></script>
  <script src="js/services.js"></script>
  <script src="js/controllers.js"></script>
  <script src="js/filters.js"></script>
  <script src="js/directives.js"></script>
</body>
</html>

      

form.html

<h3>{{fruitOperation}}</h3>
<hr/>
<form name="fruitForm">
  <input type="hidden" name="" ng-model="fruit.id" />
  <p><label>name</label><input type="text" name="name" ng-model="fruit.name" value="dfgdfgdfg" required="true" /></p>
  <p><label>color</label><input type="text" name="color" ng-model="fruit.color" value="fruit.color" required="true" /></p>
  <hr/>
  <input type="submit" ng-click="save()" value="{{buttonLabel}}" /> <button ng-click="cancel()">Cancel</button>
</form>

      

Thanks for your understanding or pointers.

+3


source to share


1 answer


Bidirectional binding refers to changes occurring in the scope of your controller, displayed in your views, and vice versa. Angular has no hidden knowledge of your data on the server side. For example, in order for your changes to appear in another open browser window, you will need a notification layer that pushes changes to the client using long polls, websockets, etc.



+2


source







All Articles