Note that there are some explanatory texts on larger screens.

plurals
  1. POProper place for data-saving logic in AngularJS
    primarykey
    data
    text
    <p>App design question. I have a project which has a very large number of highly customized inputs. Each input is implemented as a directive (and Angular has made this an absolute joy to develop).</p> <p>The inputs save their data upon blur, so there's no form to submit. That's been working great.</p> <p>Each input has an attribute called "saveable" which drives another directive which is shared by all these input types. the Saveable directive uses a $resource to post data back to the API.</p> <p>My question is, should this logic be in a directive at all? I initially put it there because I thought I would need the saving logic in multiple controllers, but it turns out they're really happening in the same one. Also, I read somewhere (lost the reference) that the directive is a bad place to put API logic.</p> <p>Additionally, I need to introduce unit testing for this saving logic soon, and testing controllers seems much more straightforward than testing directives.</p> <p>Thanks in advance; Angular's documentation may be… iffy… but the folks in the community are mega-rad.</p> <p>[edit] a non-functional, simplified look at what I'm doing:</p> <p><code>&lt;input ng-model="question.value" some-input-type-directive saveable ng-blur="saveModel(question)"&gt;</code></p> <pre><code>.directive('saveable', ['savingService', function(savingService) { return { restrict: 'A', link: function(scope) { scope.saveModel = function(question) { savingService.somethingOrOther.save( {id: question.id, answer: question.value}, function(response, getResponseHeaders) { // a bunch of post-processing } ); } } } }]) </code></pre>
    singulars
    1. This table or related slice is empty.
    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.
 

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