1 // crmResource: Given a templateUrl "~/mymodule/myfile.html", load the matching HTML.
2 // This implementation loads all partials and strings in one batch.
3 (function(angular
, $, _
) {
4 angular
.module('crmResource', []);
6 angular
.module('crmResource').factory('crmResource', function($q
, $http
) {
7 var deferreds
= {}; // null|object; deferreds[url][idx] = Deferred;
8 var templates
= null; // null|object; templates[url] = HTML;
10 var notify
= function notify() {
11 var oldDfrds
= deferreds
;
14 angular
.forEach(oldDfrds
, function(dfrs
, url
) {
16 angular
.forEach(dfrs
, function(dfr
) {
19 headers: function(name
) {
20 var headers
= {'Content-type': 'text/html'};
21 return name
? headers
[name
] : headers
;
28 angular
.forEach(dfrs
, function(dfr
) {
29 dfr
.reject({status
: 500}); // FIXME
35 var moduleUrl
= CRM
.angular
.bundleUrl
;
37 .success(function httpSuccess(data
) {
39 angular
.forEach(data
, function(module
) {
40 if (module
.partials
) {
41 angular
.extend(templates
, module
.partials
);
44 CRM
.addStrings(module
.domain
, module
.strings
);
49 .error(function httpError() {
55 // @return string|Promise<string>
56 getUrl
: function getUrl(url
) {
57 if (templates
!== null) {
58 return templates
[url
];
61 var deferred
= $q
.defer();
62 if (!deferreds
[url
]) {
65 deferreds
[url
].push(deferred
);
66 return deferred
.promise
;
72 angular
.module('crmResource').config(function($provide
) {
73 $provide
.decorator('$templateCache', function($delegate
, $http
, $q
, crmResource
) {
74 var origGet
= $delegate
.get;
76 $delegate
.get = function(url
) {
77 if (urlPat
.test(url
)) {
78 return crmResource
.getUrl(url
);
81 return origGet
.call(this, url
);
88 })(angular
, CRM
.$, CRM
._
);