Skip to content

Instantly share code, notes, and snippets.

@evangalen
Created November 5, 2012 19:28
Show Gist options
  • Save evangalen/4019799 to your computer and use it in GitHub Desktop.
Save evangalen/4019799 to your computer and use it in GitHub Desktop.
<script type="text/javascript">
'use strict';
var defineGetterAndSetterProperty = function(sourceObject, targetObject, property) {
Object.defineProperty(targetObject, property, {
set: function (newValue) {
sourceObject[property] = newValue;
},
get: function () {
return sourceObject[property];
}
});
};
var createUndeclaredScope = function(scope) {
var UndeclaredScope = function() {
var self = this;
this.declare = function(obj) {
angular.forEach(obj, function(value, key) {
scope[key] = angular.isFunction(value) ? angular.bind(scope, value) : value;
});
return Object.seal(scope);
};
angular.forEach(scope, function(value, key) {
if (!scope.hasOwnProperty(key)) {
return;
}
defineGetterAndSetterProperty(scope, self, key);
});
return Object.seal(this);
};
UndeclaredScope.prototype = scope;
return new UndeclaredScope();
};
var isStrictModeEnabled = function() {
return (this === undefined);
};
var prepareControllerScope = function(scope) {
if (isStrictModeEnabled()) {
return createUndeclaredScope(scope);
} else {
scope.declare = function(obj) {
angular.extend(scope, obj);
return this;
};
return scope;
}
};
angular.module('jdStrictScope', [])
.config(function($provide) {
$provide.decorator('$controller', function($delegate) {
return function(constructor, locals) {
var originalScope = locals.$scope;
locals.$scope = prepareControllerScope(locals.$scope);
return (function(window, document, undefined) {
return $delegate(constructor, locals);
}(window, document));
};
})
});
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment