Created
November 5, 2012 19:28
-
-
Save evangalen/4019799 to your computer and use it in GitHub Desktop.
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
<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