-
Notifications
You must be signed in to change notification settings - Fork 27.4k
Make $debounce service part of angular !? #2690
Comments
Is it best practice to save to your back-end whenever a $watch is triggered On Fri, May 17, 2013 at 10:07 AM, honza [email protected] wrote:
|
@MikeMcElroy I can see reasons for both behaviors, and both of them can be named "best practices" depending on application being implemented. I think developer should have an option here (as well as customer at the end, as he pairs the money). |
👍 Great for delaying AJAX requests on input changes for features like autocomplete and suggestions. |
Shouldn't be much of an issue if you're careful (i.e. don't allow submitting instantly unless you also specify The idea behind delaying the model update is that any watch you bind to it will be called only once even if there are lots of changes in a row. If you use the model for filtering or for making AJAX requests, load will be reduced. You could also attach a database save to a model watch to achieve the same behaviour exposed in the first post. |
👍 |
+1 |
+1 I like it as a service since it's more flexible. |
+1 |
10 similar comments
+1 |
+1 |
+1 |
+1 |
+1 |
+1 |
+1 |
+1 |
+1 |
+1 |
+1 We're currently using a directive that wraps _.debounce to accomplish delayed searches on text inputs, but it would be great if this were a native directive (ngDebounce). |
I have just seen Karl Seamon's talk on ng-conf 2014, where he mentions $$postDigest event .. another approach to the same issue. He said these matters are being discussed:) See: #5828. |
my usage is outside ng-form ng-submit usecase for debounce - I've added the code to stackoverflow original posting:http://stackoverflow.com/questions/13320015/how-to-write-a-debounce-service-in-angularjs/22056002#22056002 |
I just published a nice implementation of both a debounce service and directive that can work with any ng-model at: https://github.com/shahata/angular-debounce Or simply install it using: |
Very nice, well documented work, @shahata! |
@shahata how can i modify this to work with ngChange attribute instead (and not with ngModel)? |
@fidoboy ngChange requires ngModel (it doesn't make sense without it), all it does is register a listener with ngModel to be called when the view value changes. Anyways, as for this not being included in angular core, actually this is going to end up in angular core (pete has been working on an implementation as part of an initiative to address some requests for ngModel), so, it's pretty much going to happen. |
+1 |
I am simply watching some objects on my scope that are changed as part of several digest cycles. After digesting them (changing their values via databinding) has finished, I want to save them to databse, but only ONCE and that is the point of the $debounce service.
From asking this question on SO, http://stackoverflow.com/questions/16278216/angularjs-save-changes-after-digest-has-finished, I didn't see that this common use-case that will once arise for anyone doing something more complex, is clearly solved.
So I came with this:
Question 1 to you: Do you see the need for such service?
Question 2 to you: Is this the optimal solution?
The text was updated successfully, but these errors were encountered: