As I delve into learning Javascript and Angular JS, I'm exploring alternative approaches to using .factory. Instead of declaring what it returns as a variable and referencing it, I'm curious if there's a way around using factory.
Why is factory necessary instead of just defining what it returns as a function? Is there a workaround available?
JAVASCRIPT CODE 1 (MY CODE): http://jsfiddle.net/chrisguzman/s4dBE/4/
myapp.controller("SampleController",
function SampleController($scope,$firebase){
var ref = new Firebase("https://helloworldtest.firebaseio.com/text");
var service = $firebase(ref);
service.$bind($scope, "text");
})
JAVASCRIPT CODE 2 (ORIGINAL CODE): http://jsfiddle.net/chrisguzman/s4dBE/3/
angular.module("sampleApp", ["firebase"])
.factory("sampleService", ["$firebase", function($firebase) {
var ref = new Firebase("https://helloworldtest.firebaseio.com/text");
return $firebase(ref);
}])
.controller("SampleController", ["$scope", "sampleService",
function($scope, service) {
service.$bind($scope, "text");
}
]);
Bonus query: Can the second code snippet be written differently without using factory to gain a deeper understanding? It would be enlightening to accomplish the same task without relying on factory, possibly by utilizing a function as the controller's second argument (as that's where most exposure lies).