Calling custom JavaScript function once view/model is loaded

2,059 views
Skip to first unread message

mahesh shinde

unread,
Oct 14, 2013, 11:13:29 AM10/14/13
to ang...@googlegroups.com

 Hi,

 How can I call custom JavaScript function once view/model is loaded?
 FYI - I don't want to use $watch function to do this.

Witold Szczerba

unread,
Oct 14, 2013, 4:56:09 PM10/14/13
to ang...@googlegroups.com

It's hard to guess what's on your mind.

Regards,
Witold Szczerba
---
Sent from my mobile phone.

--
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 angular+u...@googlegroups.com.
To post to this group, send email to ang...@googlegroups.com.
Visit this group at http://groups.google.com/group/angular.
For more options, visit https://groups.google.com/groups/opt_out.

mahesh shinde

unread,
Oct 15, 2013, 3:08:10 AM10/15/13
to ang...@googlegroups.com
Suppose I have an HTML page where I need to display list of employees from database. In my HTML I have a model/view that populates the data from DB using a REST service call. I use ng-repeat to render the HTML table rows. Now once the service call is been completed, the view is been rendered, but since the service calls are async there isn't a way by which I can identify the rendering of the view is completed. I need to know this as I have to call a JavaScript function that uses the rendered data for my next set of functionality.

Witold Szczerba

unread,
Oct 15, 2013, 8:50:19 AM10/15/13
to ang...@googlegroups.com
OK, so first and most important thing you have to acknowledge is a
very simple rule: your controller is supposed to decide what, when and
why happens. If something is supposed to happen - this is because
nothing but your controller wants it. What about view? View is
supposed to blindly follow. It is not about deciding or taking
control. It is just a reflection of the state of the application.

Why this is so important is out of the scope here, but having that in
mind is going to save your application from being a spaghetti.

Now, your specific problem. Your controller loads employees. I assume
you are calling a service, that service returns a promise, you attach
a "then" function which fires after data are downloaded and available
locally. There is nothing special in a case you want something to
happen after that, so you can launch another stuff in another "then"
of the said promise, or just add more lines of "things to happen"
inside what you already have. Remember: your view is supposed to
follow. You controller can issue hints like: "something has happened"
event or scope.something = "I did this or that", so your view can
follow, but not the other way. You are not going to wait for some kind
of "I AM RENDERED" events, because your controller does not give a
sh** about that.

Again: controllers are supposed to work without any view being
attached to them. Once you start troubling your controller with stuff
it SHOULD never care, like display events, you are on slippery slope.
Your app will become a bundle of scripts. If you want to crate and
support a bunch of scripts - then you better stop using Angular,
because it will get in your way.

Regards,
Witold Szczerba

2013/10/15 mahesh shinde <mahesh...@gmail.com>:

Will Seitz

unread,
Oct 15, 2013, 11:17:12 AM10/15/13
to ang...@googlegroups.com
Totally agree with what Witold is saying. To add a little more detail you want to do promise chaining and maybe look at the $q library in Angular. Promise chaining is intimidating at first, but I have found it to work very seamlessly. So your code will look something like:

$http.get("myEmployeesUrl").then(function(data) {
     $scope.employeeArray = data;
}).then(function() {
     $http.get("otherDataUrl").then(function(data) {
         $scope.otherThing = data;
     }
})

It looks a little complex and due to the flexibility of promise chaining can be written in many different ways, but promise chaining is something you should get used to if you plan on using web services. Again, what Witold said, the whole point of MVC and two way data binding is so you can write model code without thinking about what the UI is doing. 

Hope this helps,
-Will
Reply all
Reply to author
Forward
0 new messages