Created
August 20, 2015 16:47
-
-
Save prettycode/c313acd19ccfbba7456f to your computer and use it in GitHub Desktop.
scopeSrc.js
This file contains hidden or 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
(function () { | |
'use strict'; | |
var componentName = 'scopeSrc'; | |
angular | |
.module('mixins.' + componentName, []) | |
.directive(componentName, [ | |
'$http', | |
'$parse', | |
function ($http, $parse) { | |
function hideElement(element, hide) { | |
element.get(0).style.visibility = hide ? 'hidden' : 'visible'; | |
} | |
return { | |
restrict: 'A', | |
link: function (scope, element, attrs) { | |
var targetScope = element.isolateScope() || element.scope(), | |
transform = attrs.scopeSrcTransform; | |
if (!targetScope) { | |
throw '$compile.debugInfoEnabled() must be true'; | |
} | |
hideElement(element, true); | |
$http | |
.get(attrs[componentName]) | |
.then(function (response) { | |
var scopeData = response.data; | |
if (transform) { | |
scopeData = scope.$eval(transform)(scopeData); | |
} | |
angular.extend(targetScope, scopeData); | |
hideElement(element, false); | |
}) | |
; | |
} | |
}; | |
} | |
]) | |
; | |
})(); | |
/* Example | |
<poor-calls-card scope-src="/api/operations-dashboard/card?callType=WirelessInternal" | |
scope-src-transform="poorCallList.transformPoorCallCard"></poor-calls-card> | |
poorCallList.transformPoorCallCard = function (data) { | |
var poorCallsCard = data.poorCallsCard; | |
poorCallsCard.iconIndicators = [ | |
{ | |
iconClass: 'fa-exclamation-triangle', | |
value: poorCallsCard.levelCurrent | |
}, { | |
iconClass: 'fa-line-chart', | |
value: poorCallsCard.levelVolume | |
} | |
]; | |
delete poorCallsCard.levelCurrent; | |
delete poorCallsCard.levelVolume; | |
return data; | |
}; | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment