Last active
August 29, 2015 14:22
-
-
Save justsml/5b34390096242d8380e1 to your computer and use it in GitHub Desktop.
Browserify and JS `Object.Prototype` Magic - this is Browser && || Server code!!!!
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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