Javascript Remoting in Static Resource
Javascript remoting can be used within a static resource.
This method fails in a static resource because of the use of the merge field '{!$RemoteAction.MyJSRemoteController.getContentList}'
and not because of the use of javascript remoting.
Using the alternate javascript remoting syntax below will work because it is not dependent on a merge field:
MyJSRemoteController.getContentList(function(result, event) {
//callback stuff
});
Although less elegant, you can also use the following syntax, which is closer to the documented syntax for JS Remoting.:
<script>
salesCentral.factory('contentData', ['$q', '$rootScope', function($q, $rootScope) {
return function() {
var deferred = $q.defer();
Visualforce.remoting.Manager.invokeAction(
'MyJSRemoteController.getContentList',
function(result, event) {
$rootScope.$apply(function() {
if (event.status) {
deferred.resolve(result);
} else {
deferred.reject(event);
}
})
},
{ buffer: true, escape: true, timeout: 30000 }
);
return deferred.promise;
}
}]);
</script>
To add to Phil's answer, you can use Javascript remoting in a static resource. However, the only tricky issue is namespacing.
There are very few good examples of the alternate javascript remoting syntax available. And none of the dealt with namespacing. So this is how you do it. You will need to pass in the namespace to your static js file. We do this in the onclick function that kicks off the remoting.
<input id="input" onclick="javascriptRemoteFunction(window['{!Namespace}']);" type="button">
To actually get the namespace you will need this function in your apex controller:
public String getNamespace(){
ApexClass getNamespaceClass = [
select NamespacePrefix
from ApexClass
where Name = 'NameOfClass'
];
return getNamespaceClass.NameSpacePrefix;
}
}
And finally your static javascript file, that uses the alternate syntax will look like this:
function javascriptRemoteFunction(namespaceObject) {
var searchDropdownCategory = document.getElementById('sharing_search').value;
var searchBoxValue = document.getElementById('searchValue_sharing_search').value;
namespaceObject.AjaxUserRoleSearch.ajaxGetRoles(
searchBoxValue, searchDropdownCategory,
function(result, event){
if (event.status) {
doStuff();
}
}
);
}
To augment Phil's answer, we can pass variables like this:
var myObj = {
// add properties
};
MyJSRemoteController.getContentList(myObj, function(result, event) {
//callback stuff
});