At the moment in my app I'm handling all fetching of data inside my page 
controllers. A repeating pattern in all of them goes something like:

$http.get('/users.json').then(function (users) {
  scope.users = users;
});

After looking at and using Polymer's web components, I found their 
<core-ajax> element which is used for loading data and I'm thinking about 
implementing something similar in my code.

This would consist of a directive with a transclude which would load the 
data via ajax and place it into the page controllers scope (or directives 
scope if it's used inside a directive). It could look something like:

<load-ajax path="/users.json" params="{}>
  <div ng-repeat="user in users">
    {{user.name}}
    ...
  </div>
</load-ajax>

I want some feedback on if this is a good idea or if I'm trying to do too 
much with the directives. My thoughts are:

Pro
---
* DRY all the ajax loading code (my code is actually more complex then this 
example so this is quite good)
* Easy for the designers to fetch data for the templates.
* Easy to show loaders while the promises have not resolved (simple 
ng-show/hide inside the load-ajax template html)
* Easy to have attr.$observes on the params to automatically re-fetch the 
data. You could use this easily with route params etc.

Con
---
* Hides what is being added to the scope as it's not visible in the 
controller.

Thoughts? Am I going down the wrong track? ;)

-- 
You received this message because you are subscribed to the Google Groups 
"AngularJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.

Reply via email to