Hi All!
I have a few design questions. I am new to angular and my background isn't
javascript - I have been doing some stuff using JS and JQuery here and
there but I newer build an application with it. I was using javascript on
top of ruby to sprinkle some sugar over the application.
So It is kind of hard for me at the moment to put my head around the
angular.js - promises, asynchronous way of doing things.
I am trying to solve this problem :
*Background*
I have a backend endpoint /cars.json When you hit this endpoint for the
first time the background job which will fetch all the cars information
gets started. Server then returns response with status 200:
{"status":"started", "percentage_completion":0, "data":null, "error":null}
Consecutive requests to this endpoint will keep on returning status 200
with updated *percentage_completion*
{"status":"started", "percentage_completion":45, "data":null,
"error":null}{"status":"started", "percentage_completion":90, "data":null,
"error":null}
Finally the request to the endpoint will return:
{"status":"finished", "percentage_completion":100, "data": {...}, "error":null}
After some reading I have come up with this solution:
http://plnkr.co/edit/vS1lfd9xQSPPQArzAcUK?p=info
But there is a lot of things from the design perspective I don't like about
it
* Dashboard controller knows to much
- it knows that there is a promise down the chain
- it knows that there is some external api call
- even in such a small example everything is tightly coupled
What I would like to achieve with it would be something as simple as having
the controller to ask CarService for list of cars and downloadStatus so the
dashboard controller simply sets the $scope variables without worrying
about the implementation:
DashboardController = (CarService) ->
vm = @
vm.cars = CarService.getCars()
vm.downloadStatus = CarService.downloadStatus()
getCars returns the array of cars when it finish processing - defer.resolve
downloadStatus() returns the status object something like {'status' =
'started', 'completion' = '70' } periodically when defer.notify kicks in
from inside the CarService.
I can't figure out how to get there from where I am at the moment:)
Would it make sense to do it this way, are there a better ways to do this
using angular?
Can you please suggest how would you tackle this problem?
Ps
Being required to call two separate methods to get the cars and data looks
strange anyway... I am probably missing another abstraction here.... or am
I simply overcomplicating?
--
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.