I'm trying to get a better understanding of a new codebase, and haven't found a ton of info (or maybe I'm not fully understanding what I have read) about the performance implications of some choices that were made. The previous maintainer isn't available for insight.
Each controller is set up to make its data request before being rendered initially using this pattern:
$routeProvider.when('/path', {
resolve: { someMethod: function($q) {
var deferred = $q.defer();
.... do some stuff ...
return deferred.promise;
}
});
This is then injected into the controller - that part makes sense and from my understanding is more performant because you're saving digests by waiting for the data to come through first. A loading state is shown by default and removed when we digest.
The value of the injected param - in this case someMethod
- is then assigned to this.someModel
.
Throughout the controller, remaining methods and properties are set with this.doStuff = function() { ... }
, this.property = 'some string'
, etc.
In the end, a watch is setup and properties made available to the $scope
by setting a $watchCollection
like this:
$scope.$watchCollection(angular.bind(this.someModel, function() {
return this;
}), function(newTitle, oldTitle) {
if (newTitle.title !== oldTitle.title)
{
self.updateThings(newTitle);
}
});
I understand that it's binding the context of the watch to the current context of this
, then watching to changes on its properties using $watchCollection
which will perform shallow reference checks of its top level properties.
Per this article I get that simply using properties in my templates with {{ property }}
will cause them to be watched, but in this case they aren't watched until they're bound to the $watchCollection.
Of course this is less expensive than using $watch
and passing it true
for the optional object equality param, but is it better or worse from a performance perspective than putting things directly onto $scope
? Why, specifically?
I've never seen it done this way before, so any readings, insights, or things that could give me a better understanding will be greatly appreciated.