Skip to content

Instantly share code, notes, and snippets.

@quangpham
Created December 13, 2013 21:34
Show Gist options
  • Save quangpham/7951734 to your computer and use it in GitHub Desktop.
Save quangpham/7951734 to your computer and use it in GitHub Desktop.
# Factories are used to give each class it's own dependency management
angular.module('myApp', ['ngCookies'])
.factory('BaseObject', ['$log', '$http', ($log, $http) ->
# The instance-classes are returned at the end of the factory and can be injected (unmodified)
class BaseObject
constructor: (@options) ->
send: (args) ->
$log.log('sending', args)
$http(args)
])
.factory('ParentObject', ['BaseObject', '$cookies', (BaseObject, $cookies) ->
class ParentObject extends BaseObject
constructor: (options) ->
super(options)
$log.log('Parent created with', options)
save: ->
@send(@)
])
.factory('ChildObject', ['ParentObject', (ParentObject) ->
class ChildObject extends ParentObject
save: ->
@name = 'child'
super()
])
.factory('SingletonObject', ['BaseObject', (BaseObject) ->
class SingletonObject extends BaseObject
get: (args) ->
@send(args)
# Since the factory is also a constructor, you can use it to return a singleton instance
new SingletonObject()
])
.controller('MyCtrl', ['$scope', 'ChildObject', 'SingletonObject', ($scope, ChildObject, SingletonObject) ->
$scope.item = new ChildObject()
SingletonObject.get().then (data) ->
$scope.options = data
])
var __hasProp = {}.hasOwnProperty,
__extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; };
angular.module('myApp', ['ngCookies']).factory('BaseObject', [
'$log', '$http', function($log, $http) {
var BaseObject;
return BaseObject = (function() {
function BaseObject(options) {
this.options = options;
}
BaseObject.prototype.send = function(args) {
$log.log('sending', args);
return $http(args);
};
return BaseObject;
})();
}
]).factory('ParentObject', [
'BaseObject', '$cookies', function(BaseObject, $cookies) {
var ParentObject;
return ParentObject = (function(_super) {
__extends(ParentObject, _super);
function ParentObject(options) {
ParentObject.__super__.constructor.call(this, options);
$log.log('Parent created with', options);
}
ParentObject.prototype.save = function() {
return this.send(this);
};
return ParentObject;
})(BaseObject);
}
]).factory('ChildObject', [
'ParentObject', function(ParentObject) {
var ChildObject;
return ChildObject = (function(_super) {
__extends(ChildObject, _super);
function ChildObject() {
return ChildObject.__super__.constructor.apply(this, arguments);
}
ChildObject.prototype.save = function() {
this.name = 'child';
return ChildObject.__super__.save.call(this);
};
return ChildObject;
})(ParentObject);
}
]).factory('SingletonObject', [
'BaseObject', function(BaseObject) {
var SingletonObject;
SingletonObject = (function(_super) {
__extends(SingletonObject, _super);
function SingletonObject() {
return SingletonObject.__super__.constructor.apply(this, arguments);
}
SingletonObject.prototype.get = function(args) {
return this.send(args);
};
return SingletonObject;
})(BaseObject);
return new SingletonObject();
}
]).controller('MyCtrl', [
'$scope', 'ChildObject', 'SingletonObject', function($scope, ChildObject, SingletonObject) {
$scope.item = new ChildObject();
return SingletonObject.get().then(function(data) {
return $scope.options = data;
});
}
]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment