Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <h2>$RESOURCE EXAMPLE</h2> <p>For <strong>csrf</strong>, In a rails app you have to add <code>&lt;%= csrf_meta_tags %&gt;</code> in your header layout (if not there by default)</p> <pre class="lang-js prettyprint-override"><code>var app = angular.module('app', ['ngResource']); app.config(["$httpProvider", function(provider) { provider.defaults.headers.common['X-CSRF-Token'] = $('meta[name=csrf-token]').attr('content'); }]); </code></pre> <p><strong>Here is a service object</strong></p> <pre class="lang-js prettyprint-override"><code>app.factory('$folders', ['$resource', function($resource) { return $resource('/folders/:id', { id: '@id' }, { list: { method: 'GET' , isArray: true }, //same as query create: { method: 'POST' }, // same as save update: { method: 'PUT' } // DEFAULT IMPLEMENTATION OF $RESOURCE // 'get': {method:'GET'}, // 'save': {method:'POST'}, // 'query': {method:'GET', isArray:true}, // 'remove': {method:'DELETE'}, // 'delete': {method:'DELETE'} }); }]); </code></pre> <p><strong>Here is an example into a controller</strong></p> <pre class="lang-js prettyprint-override"><code>app.controller('projectController', ['$scope', '$folders', function($scope, $folders) { $scope.folders = $folders.list(); }]); </code></pre> <p><code>Folders.list()</code> will automatically do <code>GET /folders/</code> on the server and return the result(that should be json) as an object.</p> <h2>Tip:</h2> <p>Directly after <code>$scope.folders = Folders.list();</code> in the controller, <code>$scope.folders</code> will be empty, it will be populated in time, when the response comes back from the server.</p> <p>Simple examples with $http and $ressources <a href="http://jsfiddle.net/jhsousa/aQ4XX/" rel="nofollow">http://jsfiddle.net/jhsousa/aQ4XX/</a></p> <h2>ABOUT THE FORM</h2> <p><strong>here is a form</strong></p> <pre class="lang-haml prettyprint-override"><code>%form{'ng-submit' =&gt; 'create(item)', 'ng-controller' =&gt; 'projectController', 'ng-init' =&gt; 'item.orientation=1;'} %input{'ng-model'=&gt;'item.folderName', :type =&gt; :text, :placeholder =&gt; 'Name', :name =&gt; 'folderName'} %textarea{'ng-model'=&gt;'item.description', :placeholder =&gt; 'Description', :required=&gt;true, :name =&gt; 'description'} %input{'ng-model'=&gt;'item.orientation', :type=&gt;'radio', :name=&gt;'orientation', :value=&gt;'1'} %input{'ng-model'=&gt;'item.orientation', :type=&gt;'radio', :name=&gt;'orientation', :value=&gt;'2'} %input{:type =&gt; :submit, :value =&gt; 'Create', :name =&gt; 'new-project'} </code></pre> <p>You'll notice the <code>ng-model</code>. <code>'ng-model'=&gt;'item.folderName'</code> will create a <code>$scope.item</code> in the <code>projectController</code> and add a key <code>folderName</code> in it. Same for the others <code>ng-model</code>'s.</p> <p><code>'ng-init'=&gt;'item.orientation=1;'</code> is going to execute the expression. So it will do the following <code>item.orientation=1</code>, this way we set a default value for our radio inputs, so simple.</p> <p>When the form is submitted <code>ng-submit</code> will catch it and call the <code>create</code> action from the <code>projectController</code> with <code>item</code>as parameter, no need to say that <code>item</code> will contain the inputs values?</p> <p><strong>Here is the controller part</strong></p> <pre class="lang-js prettyprint-override"><code>app.controller('projectController', ['$scope', '$folders', function($scope, $folders) { $scope.create = function(item) { f = new $folders(item); f.$save(function(data,headers) { alert('SAVED'); }, function(err,headers) { alert('FAILED'); }); }; }]); </code></pre> <p><code>$scope.create</code> is the action that will be called by <code>ng-submit</code>, <code>item</code> is the <code>item</code> parameter from the form as well, so you'll find stuff inside such as <code>item.description</code>.</p> <p><code>Folders</code> is the service object we talked about earlier.</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. 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.
    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