Skip to content

Instantly share code, notes, and snippets.

@justsml
Last active August 29, 2015 14:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save justsml/5b34390096242d8380e1 to your computer and use it in GitHub Desktop.
Save justsml/5b34390096242d8380e1 to your computer and use it in GitHub Desktop.
Browserify and JS `Object.Prototype` Magic - this is Browser && || Server code!!!!
var inherits = require('inherits'),
EventEmitter = require('events').EventEmitter;
function BaseController($scope, $http, $location) {
if (!(this instanceof BaseController)) { return new BaseController.apply(null, [].slice(arguments)); }
$scope.load = this.load;
this.$scope = $scope;
this.$location = $location;
this.$http = $http;
}
inherits(BaseController, EventEmitter);
module.exports = ['$scope', '$http', '$location', BaseController];
/**
The load method has important behaviour:
The target url can be overriden via the ajaxUrl property of derived classes.
Security Notes: In general, this 'url overrride' pattern is at risk for XSS or content injection attacks.
Allowing or exposing 'public' class members to completely override a url is just asking for trouble - eventually,
This could deliver malicious content. The Url should be a hidden var or 'const' value if possible.
*/
BaseController.prototype.load = function _load() {
var self = this,
url = self.ajaxUrl || self.$location.absUrl(); // self.ajaxUrl Can be set by derived classes, it will apply when this is next called!
self.$scope.loading = 'load';
return self.$http({'method': 'GET', 'url': url})
.success(function(data) { self.$scope.results = data || []; })
.error(function(err) { self.$scope.error = err && err.message || err; })
.finally(function() { self.$scope.loading = false; })
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment