Can you tell me what this routing code is doing?

34 views
Skip to first unread message

Jonathan Matthew Beck

unread,
Aug 18, 2014, 3:53:37 PM8/18/14
to ang...@googlegroups.com
I came into a project and found things like this in the routing:

                .when('/home', {

                    templateUrl
: 'partials/home.html',

                    controller
: 'HomeController as vm',

                    resolve
: {

                        init
: ['dataService', function (dataService) {

                           
return dataService.init();

                       
}]

                   
}

               
})

I looked for documentation on the use of resolve like that.  As far as I can tell, it's creating an object and then passing, or overriding, the objects init function.  Then it's creating an un-named Angular service (?) and passing in the dataService.

Questions:

1) What is the above code doing?

2) Does it create a new instance of an un-named something (service?) every time the user is routed?

3) The square brackets are confusing me, are we over-writing the Controller’s init() function every time?

Mark Volkmann

unread,
Aug 18, 2014, 4:09:34 PM8/18/14
to ang...@googlegroups.com
On Mon, Aug 18, 2014 at 2:53 PM, Jonathan Matthew Beck <dnc...@gmail.com> wrote:
I came into a project and found things like this in the routing:

                .when('/home', {

                    templateUrl
: 'partials/home.html',

                    controller
: 'HomeController as vm',

                    resolve
: {

                        init
: ['dataService', function (dataService) {

                           
return dataService.init();

                       
}]

                   
}

               
})

I looked for documentation on the use of resolve like that.  As far as I can tell, it's creating an object and then passing, or overriding, the objects init function.  Then it's creating an un-named Angular service (?) and passing in the dataService.

Questions:

1) What is the above code doing?

It is defining a route. The resolve part basically says "Don't render this route until all this data is available." The most common thing that resolve functions do is retrieve data from REST services. Maybe dataService.init does that. If the functions in a resolve return promises, Angular waits for all of those be resolved before it renders the view for that route. 

2) Does it create a new instance of an un-named something (service?) every time the user is routed?

It creates a new instance of HomeController every time the user goes to that route. 

3) The square brackets are confusing me, are we over-writing the Controller’s init() function every time?

The square brackets are just creating a literal JavaScript array that contains a string and a function. It is one way to make an injectable function save for minimizing. That won't mean much if you are just getting started with AngularJS, but later it will make sense.

--
R. Mark Volkmann
Object Computing, Inc.

Jonathan Matthew Beck

unread,
Aug 19, 2014, 8:24:44 AM8/19/14
to ang...@googlegroups.com
Thanks.  I tested the app yesterday, removed all elements from pages (partials/directives) and just navigated from page to page. 

The memory the app uses increases steadily until the eventually it crashes.  I am trying to figure out what is not getting released.

The resolve: { init:
is basically meaningless, it could be:
resolve: { fooBar:

Right?

And is creating an anonymous "literal JavaScript array that contains a string and a function. It is one way to make an injectable function save for minimizing." mean that it isn't garbage collected?


--
You received this message because you are subscribed to a topic in the Google Groups "AngularJS" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/angular/lU30ZFaNwJc/unsubscribe.
To unsubscribe from this group and all its topics, 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/d/optout.

Mark Volkmann

unread,
Aug 19, 2014, 10:31:32 AM8/19/14
to ang...@googlegroups.com
On Tue, Aug 19, 2014 at 7:24 AM, Jonathan Matthew Beck <dnc...@gmail.com> wrote:
Thanks.  I tested the app yesterday, removed all elements from pages (partials/directives) and just navigated from page to page. 

The memory the app uses increases steadily until the eventually it crashes.  I am trying to figure out what is not getting released.

I have seen other people report issues with memory usage, but I haven't experienced that yet so I don't know what to recommend.

The resolve: { init: 
is basically meaningless, it could be:
resolve: { fooBar:

Right?

The name "init" is something that can be injected into the controller for that route. That is how the controller gets the data that is not available until all the promises returned by the resolve functions have been resolved. So if you change "init" to "fooBar" in the resolve then you have to make the same change in the controller where it is injected.
 
And is creating an anonymous "literal JavaScript array that contains a string and a function. It is one way to make an injectable function save for minimizing." mean that it isn't garbage collected?

I meant to say "safe" instead of "save". That is not related to garbage collection.
 

--
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/d/optout.
Reply all
Reply to author
Forward
0 new messages