Polling a resource in Angular

3,125 views
Skip to first unread message

Ruiwen Chua

unread,
Jan 12, 2011, 8:58:48 AM1/12/11
to Angular
Hi all,

I'm new to Angular so I've been going through the documentation trying
to familiarise myself with it.

Was just wondering, is there a way to create a polling function using
Angular, ie. one that checks a resource periodically and updates the
view if necessary?


Cheers
Ruiwen

Misko Hevery

unread,
Jan 12, 2011, 12:01:01 PM1/12/11
to ang...@googlegroups.com
Hi Ruiwen,

Yes, there is. In general you can do evrything you need with normal JavaScript, but when you go outside of angular, such as when you call setTimeout, you may have to call scope.$root.$eval() manually. Here is an example

function MyAppCntroller(){
  var self = this;
  this.startPolling = function(){
    function poll(){
      // do something.
      self.$root.$eval();
      window.setTimeout(poll, 1000);
    };
    poll();
  };
};


we are about to release a new wersion of angular 0.9.9 http://code.angularjs.org and then you will be able to write this
function MyAppCntroller($defer){
  var self = this;
  this.startPolling = function(){
    function poll(){
      // do something.
      // self.$root.$eval(); // not needed as $defer does this for you
      $defer(poll, 1000);
    };
    poll();
  };
};
MyAppController.$inject=['$defer'];




--
You received this message because you are subscribed to the Google Groups "Angular" group.
To post to this group, send email to ang...@googlegroups.com.
To unsubscribe from this group, send email to angular+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/angular?hl=en.


Ruiwen Chua

unread,
Feb 13, 2011, 2:54:08 PM2/13/11
to ang...@googlegroups.com
Thanks for the top Misko!

Have managed to get polling to work now. 

Next question, how do I get polling to stop? =P


Cheers
Ruiwen

Igor Minar

unread,
Feb 13, 2011, 6:43:08 PM2/13/11
to ang...@googlegroups.com
you jus don't schedule the next $defer execution



function MyAppCntroller($defer){
  var self = this;
  this.startPolling = function(){
    function poll(){
      // do something.
      // self.$root.$eval(); // not needed as $defer does this for you
      if (keepOnGoing) {
        $defer(poll, 1000);
      }
    };
    poll();
  };
};
MyAppController.$inject=['$defer'];






Cheers
Ruiwen

--

Ruiwen Chua

unread,
Feb 13, 2011, 9:24:15 PM2/13/11
to ang...@googlegroups.com
Ah, thought as much =)

Thanks Igor!

mineralf

unread,
Mar 23, 2011, 2:18:12 PM3/23/11
to Angular
Hi Misko, Igor and anybody out there having trouble with $defer,

I wrote a little full functional code for counting up a number with a
start and stop button.
I pulled out the inner function as I personally didn't want it to be a
inner function for testability reasons...

Anybody interested:
http://jsfiddle.net/zh2GU/

Would be really great to have such an example on the angular docs
http://docs.angularjs.org/#!angular.service.$defer as I needed much
too long (several hours) to figure out how $defer works and how to
avoid the scope problem etc. (self = this)...

Thx for a great framework - keep on like this!!
Ralf

mineralf

unread,
Mar 23, 2011, 2:29:15 PM3/23/11
to Angular
Hi all,
my message did somehow not show up? Or is it revised manually?
Whatever...

Took me far too long to get $defer work as I wanted it. (self=this
trick etc.) Thx god I found this thread here. I've build a small
example without inner functions for testability reasons...

Here's the example code: http://jsfiddle.net/zh2GU/
And please include sth. similar to the docs: http://docs.angularjs.org/#!angular.service.$defer

Thx for the great work on angular! Love it!
Ralf

Igor Minar

unread,
Mar 23, 2011, 7:01:15 PM3/23/11
to ang...@googlegroups.com, mineralf
On Wed, Mar 23, 2011 at 11:29 AM, mineralf <mine...@gmail.com> wrote:
> Hi all,
> my message did somehow not show up? Or is it revised manually?

yes, the *first* time someone post to the mailing list we have to
approve it to weed out all the spammers.

> Whatever...
>
> Took me far too long to get $defer work as I wanted it. (self=this
> trick etc.) Thx god I found this thread here. I've build a small
> example without inner functions for testability reasons...
>
> Here's the example code: http://jsfiddle.net/zh2GU/
> And please include sth. similar to the docs: http://docs.angularjs.org/#!angular.service.$defer

good idea, we do need an example there. however, I think that running
a task at scheduled intervals is too common of a use case not to have
a api to do just that. you can use $defer to achieve the same thing,
but that's not what $defer was primarily designed for, so the code is
longer than what it needs to be.

$schedule anyone? :)

/i

> Thx for the great work on angular! Love it!
> Ralf
>

mineralf

unread,
Mar 24, 2011, 8:25:34 AM3/24/11
to Angular
On 24 Mrz., 00:01, Igor Minar <iimi...@gmail.com> wrote:
> good idea, we do need an example there. however, I think that running
> a task at scheduled intervals is too common of a use case not to have
> a api to do just that. you can use $defer to achieve the same thing,
> but that's not what $defer was primarily designed for, so the code is
> longer than what it needs to be.
>
> $schedule anyone? :)

Your right. Sounds like a own service would be what we need... Maybe
I'll give it a try if I find some free time the next days/weeks.
But following all steps described at http://docs.angularjs.org/#!contribute
will cost me forever. (Never did any open source contribution so far)

The example code from before without Syntax Errors and JS seperated
from HTML: http://jsfiddle.net/zh2GU/3/

Cheers,
Ralf

Igor Minar

unread,
Mar 24, 2011, 11:41:20 AM3/24/11
to ang...@googlegroups.com, mineralf
On Thu, Mar 24, 2011 at 5:25 AM, mineralf <mine...@gmail.com> wrote:
> On 24 Mrz., 00:01, Igor Minar <iimi...@gmail.com> wrote:
>> $schedule anyone? :)
>
> Your right. Sounds like a own service would be what we need... Maybe
> I'll give it a try if I find some free time the next days/weeks.

cool :)

> But following all steps described at http://docs.angularjs.org/#!contribute
> will cost me forever. (Never did any open source contribution so far)

That doc is more of a guide on how to work with our git repo, build
angular, run tests, etc.

The requirements really are just three:
- send us a git pull request
- include tests
- follow the handful of style rules we have

We can delete the rest of the doc, but then you'll need to figure out
how we write, test and build our code on your own.

> The example code from before without Syntax Errors and JS seperated
> from HTML: http://jsfiddle.net/zh2GU/3/

This looks much better!

/i

>
> Cheers,

Pepper Lebeck-Jobe

unread,
Feb 23, 2012, 2:14:15 PM2/23/12
to ang...@googlegroups.com, mineralf
Suppose I want to start polling as soon as the page is fully loaded and the controller is initialized.  What hooks does the angular life-cycle management provide for this?

Thanks,
Pepper

Miško Hevery

unread,
Feb 23, 2012, 3:38:27 PM2/23/12
to ang...@googlegroups.com, mineralf
If you are using 10.6 then you can put that code in the module run method, which gets called as soon as the app gets bootstraped
Reply all
Reply to author
Forward
0 new messages