Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>We find <a href="http://docs.angularjs.org/api/ngResource.%24resource">$resource</a> to be a great way to go. The <a href="http://docs.angularjs.org/api/ngMock.%24httpBackend">$httpBackend</a> service allows for easy testing as well. We have something like the following and it works well for us. You can always drop back to the <a href="http://docs.angularjs.org/api/ng.%24http">$http</a> service if you want a little more control.</p> <p><strong>View</strong></p> <pre><code>&lt;!DOCTYPE html &gt; &lt;html ng-app="myApp"&gt; &lt;head&gt; &lt;/head&gt; &lt;body ng-controller="CustomerController"&gt; &lt;form name="form" novalidate&gt; &lt;input type="text" ng-model="customer.name" required /&gt; &lt;input type="text" ng-model="customer.address" required /&gt; &lt;button ng-click="add(customer)"&gt;Save&lt;/button&gt; &lt;/form&gt; &lt;script src="~/Scripts/angular.js"&gt;&lt;/script&gt; &lt;script src="~/Scripts/angular-resource.js"&gt;&lt;/script&gt; &lt;script src="~/Scripts/app/app.js"&gt;&lt;/script&gt; &lt;script src="~/Scripts/app/services/customerResource.js"&gt;&lt;/script&gt; &lt;script src="~/Scripts/app/controllers/CustomerController.js"&gt;&lt;/script&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p><strong>Service:</strong></p> <pre><code>myApp.factory('customerResource', function($resource){ var resource = $resource('/data/customer/:id', { id: '@id' }, { 'put' : {method:'PUT' } }); return { getAll : function(){ return resource.query(); }, add : function(customer){ return resource.save(customer); }, update : function(customer){ return resource.put({ id: customer._id }, customer); }, remove : function(id){ return resource.remove( { id: id }); } }; }); </code></pre> <p><strong>Controller:</strong></p> <pre><code>myApp.controller('CustomerController', function($scope, customerResource) { $scope.customer = {}; $scope.customers = customerResource.getAll(); $scope.add = function(customer){ $scope.customers.push(customerResource.add(customer)); } $scope.update = function(customer){ customerResource.update(customer); } $scope.remove = function(customer){ customerResource.remove(customer._id); $scope.customers.splice($scope.customers.indexOf(customer), 1); } }); </code></pre> <p><strong>Very basic testing:</strong></p> <pre><code>describe('customerResource', function(){ beforeEach(module('myApp')); describe('getAll', function(){ it('should issue a GET request to /data/customer', inject(function(customerResource, $httpBackend){ $httpBackend.expectGET('/data/customer').respond([{ level: '5'}]); var customers = customerResource.getAll(); $httpBackend.flush(); expect(customers[0].level).toBe('5'); })); it('should return an array of custoemrs', inject(function(customerResource, $httpBackend){ $httpBackend.when('GET', '/data/customer').respond([{ level: '5'}, { level: '6'}]); var customers = customerResource.getAll(); $httpBackend.flush(); expect(customers[0].level).toBe('5'); expect(customers[1].level).toBe('6'); })); }); </code></pre> <p><strong>MVC Action</strong> (ADD - MVC model binders will do the work of parsing the html params into the VM):</p> <pre><code>[HttpPost] public ActionResult Customer(Customer customer) { // add the customer to the database or whatever } </code></pre> <p><strong>ViewModel:</strong></p> <pre><code>public class Customer { public string Name { get; set; } public string Address { get; set; } } </code></pre> <p>The HTTP request will look something like:</p> <pre><code>Request URL:http://mywebsite/data/customer Request Method:POST Status Code:200 OK Request Headersview source Accept:application/json, text/plain, */* Accept-Encoding:gzip,deflate,sdch Accept-Language:en-GB,en;q=0.8,en-US;q=0.6 Cache-Control:no-cache Connection:keep-alive Content-Length:30 Content-Type:application/json;charset=UTF-8 Host:mywebsite Origin:http://mywebsite Pragma:no-cache Request Payloadview source {name:somename, address:someaddress} address: "somename" name: "someaddress" </code></pre> <p>HTH</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload