The Author Online Book Forums are Moving

The Author Online Book Forums will soon redirect to Manning's liveBook and liveVideo. All book forum content will migrate to liveBook's discussion forum and all video forum content will migrate to liveVideo. Log in to liveBook or liveVideo with your Manning credentials to join the discussion!

Thank you for your engagement in the AoF over the years! We look forward to offering you a more enhanced forum experience.

372567 (12) [Avatar] Offline
#1
These are more a JavaScript questions than an AngularJS questions.

myModule.factory('AngelloHelper', function() {
var buildIndex = function (source, property) {
var tempArray = [];

for (var i = 0, len = source.length; i < len; ++i) {
tempArray[source[i][property]] = source[i];
}

alert(typeof tempArray);
return tempArray;
};

return {
buildIndex: buildIndex
};
});

1. I am confused by the syntax of

buildIndex: buildIndex and


tempArray[source[i][property]] = source[i];

The caller is
main.statusesIndex = AngelloHelper.buildIndex(main.statuses, 'name');

I understand what is happening but the syntax isc onfusing. After the execution of the second line,

tempArray[0] will be {Back Log: {name: "Back Log"} }

2. Is "factory" described in the book? Why is 'AngelloModel' a service and 'AngelloHelper' a factory?

Thanks.

Lukas Ruebbelke (107) [Avatar] Offline
#2
Excellent questions!

1. In Angular, a factory is service (yes this term is overloaded in Angular BIG TIME!) that returns a factory method that gets called when the factory is instantiated. Essentially, I am using the revealing module pattern to return an object that I want to represent the AngelloHelper service. I have defined the buildIndex method internally but until I expose it in the return object, it is essentially a private method. So the return object is basically saying "expose the buildIndex method to the outside world as buildIndex"

The buildIndex is a bit confusing, but I am using it to build a data structure that allows me to look up objects without having to iterate over the collection. For instance, I can say "get me the object that has a name of 'Back Log'" and I can do that via main.statusesIndex['Back Log']. I am certain there are easier ways to do this with something like lodash and definitely with ES6 using a map.

2. I do get into factories vs services in the book. The are almost identical in terms of consumption, but the difference is that a service is instantiated via a constructor function while a factory is instantiated via a factory method. People who really like OOP tend to favor the service way as it references the instance via the 'this' keyword.

Does this make sense? Happy to elaborate further if you would like!
372567 (12) [Avatar] Offline
#3
As usual, thanks for the answers.It helps me a great deal.

return {
buildIndex: buildIndex
};

Can you explain the "buildIndex: buildIndex" line? I am guessing that the second buildIndex calls the function. Why do we need the first buildIndex: ? Would

return {
buildIndex
};

work? I think that you are providing clues in your answer above. I need to study it some more.

Lukas Ruebbelke (107) [Avatar] Offline
#4
The reason you have to return this is because it is a valid JSON object.

return {
buildIndex: buildIndex
};

This, on the other hand, is not returning a valid JSON object.

return {
buildIndex
};

Anything you return in this object is available to the outside world.

return {
someProperty: 'property value',
anotherProperty: true
anotherMethod: reallyThisMethod,
buildIndex: buildIndex
};

Definitely check out JavaScript Patterns by Stoyan Stefanov when you get a chance. It is excellent!

http://www.amazon.com/JavaScript-Patterns-Stoyan-Stefanov/dp/0596806752