Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to managing multiple elements of resource in AngularJS
    primarykey
    data
    text
    <p>I'm very new to AngularJS but I love it's methodology so far but it comes with some mindset changes that I'm still adapting to. (I'm using Coffeescript and Haml. And PLEASE let me know if you see anything that I should/n't be doing that doesn't relate to my question - again, I'm very new to Angular.)</p> <p>I have a resource that is set up like this:</p> <pre><code>app.factory "ServiceRequest", ["$resource", ($resource) -&gt; $resource("/service_requests/:id", {id: "@id"}, {update: {method: "PUT"}}) ] app.controller "ServiceRequestController", ["$scope", "ServiceRequest", ($scope, ServiceRequest) -&gt; $scope.service_requests = ServiceRequest.query() </code></pre> <p>Nothing too fancy, especially if you are familiar with Rails and RESTful Controllers. Now I want to display those on the index.html.haml page..</p> <pre><code>.service-request{'ng-repeat' =&gt; 'service_request in service_requests'} .upper #... %a.btn{:href =&gt; "", 'ng-click' =&gt; "toggleExpand(service_request)"} .lower{'ng-show' =&gt; 'service_request.expanded'} #... </code></pre> <p>Notice the button that calls toggleExpand(service_request).</p> <pre><code>$scope.toggleExpand = (service_request) -&gt; service_request.expanded = not service_request.expanded </code></pre> <p>So.. this works until I click another button that changes a variable and then service_request.$update(). My Rails controller returns a JSON service_request variable that wipes out my expanded variable. Crap.</p> <pre><code>$scope.acceptRequest = (service_request) -&gt; service_request.state_change = "accept" service_request.$update() </code></pre> <p><strong>What is a better way to manage these non-database variables that are only meant to be used in the browser that still takes advantage of Angular's spectacular two-way binding?</strong></p> <p>Thanks! Let me know if you need anymore code. =)</p> <p><strong>// This is what I did to solve the problem.</strong></p> <p>For the JS:</p> <pre><code>$scope.init = () -&gt; $scope.toggleFlags = {} angular.forEach $scope.service_requests, (service_request) -&gt; $scope.toggleFlags[service_request.id] = false $scope.toggleExpand = (id) -&gt; if not $scope.toggleFlags $scope.init() $scope.toggleFlags[id] = not $scope.toggleFlags[id] </code></pre> <p>For the HTML:</p> <pre><code>%a.btn{:href =&gt; "", 'ng-click' =&gt; "toggleExpand(service_request.id)"} </code></pre>
    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.
 

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