0

In 2sxc module app I want to get values for "String Drop Down Values" from function or external web api and not with entered list of values.

Is this possible by default or can somebody give me some guides where to start to do this?

(EDIT/ADD) Is making Content Custom Input Type the only way, or there is some simpler way?

Jernej Pirc
  • 504
  • 1
  • 4
  • 13

3 Answers3

1

So basically your code is running inside Angular, which also has 2sxc4ng loaded. There are at least 3 ways to solve your problem.

  1. you can use the sxc object, which is the same as $2sxc(moduleId)
  2. you can use the iid
  3. you can just use $http, as it's already pre-loaded with the module-id in the headers

Using $http is pretty clear - and if you watch the wire, you'll see that the moduleid is already included in the headers.

to access iid or the sxc object, you need to put them as dependencies in this line .controller("FieldTemplate-String-Nn-Int-Json-List", function($scope, $filter, $modal, appId, debugState, eavAdminDialogs, $http) so you need .controller("FieldTemplate-String-Nn-Int-Json-List", function($scope, $filter, $modal, appId, debugState, eavAdminDialogs, $http, iid, sxc)

That should do it.

iJungleBoy
  • 5,325
  • 1
  • 9
  • 21
  • sxc in controller function work OK and from sxc.id I can get module id – Jernej Pirc Aug 13 '16 at 04:42
  • iid in conntroller function give me error: (screen shot : http://shrani.si/f/2U/UD/4Zcvh7tX/error-iid.jpg ) – Jernej Pirc Aug 13 '16 at 04:48
  • sxc.webApi.get("Controller/Method).... give me error: (screen shot : http://shrani.si/f/3S/zd/3YE6Tb1j/error-sxc-webapi-get.jpg ) – Jernej Pirc Aug 13 '16 at 04:54
  • $http.get("Controller/Method)... generate me url : /desktopmodules/tosic_sexycontent/dist/dnn/{controller}/{method} and return me http error 404 @iJungleBoy – Jernej Pirc Aug 13 '16 at 04:56
  • found the solution but don't know if is OK. If I remove [ValidateAntiForgeryToken] from SxcApiController and call from angular controller with $http.get("/DesktopModules/2sxc/API/app-api/{Controller}/{Method}")... then I can get result. For now this is OK for me. (thanks for help) – Jernej Pirc Aug 13 '16 at 05:18
  • Removing the validate-anti-forgery is usually ok. The main idea is to prevent ddos attacks on your api, but if you require login & permissions for your api to react, then the forgery-token is usually not important any more. – iJungleBoy Aug 15 '16 at 06:21
0

Creating a custom input type is the only way as of now. Since many people are looking for something like this, it would be great if you would then sponsor/contribute it :)

iJungleBoy
  • 5,325
  • 1
  • 9
  • 21
  • I already working on that way. I will post sample when finished – Jernej Pirc Aug 09 '16 at 14:35
  • can you give me advice how to pass moduleId to custom editor javascript? I can't use like in angular api sample (with DIV attribute) – Jernej Pirc Aug 09 '16 at 16:11
  • Since your plugin is running inside the angular, you can just request it in your function call to get it injected by 2sxc4ng. The property is called iid (for instance id). So if your controller also expects an iid, you have the module id. – iJungleBoy Aug 10 '16 at 08:05
  • thanks @iJungleBoy i just post answer with sample app for external web api, and in next days I will use iid for some sample who get data from 2sxc api controller this is why I need iid (i think). Or what is the right way to call 2sxc module api from custom input type code? – Jernej Pirc Aug 10 '16 at 11:50
  • i trying to get this iid but with no luck... I edited answered question and add the current problem code. – Jernej Pirc Aug 10 '16 at 14:19
0

I attached sample app with little description.

download

All stuff is already described OK on this link: http://2sxc.org/en/blog/post/configurable-custom-input-type-dynamic-data-content

I just change this sample to use string and main code change is in angular controller and my code look like this:

        var url;
        var controlSettings = $scope.to.settings["string-nn-json-list"];
        if (controlSettings)  url = controlSettings.jsonUrl || null;
        // if null set default apiUrl
        if (url === null)     url = "http://www.mocky.io/v2/57aae03e120000be10739d3b";
        $http({
            method: 'GET',
            url: url,
            headers: {
                'TabId': undefined,
                'Pragma' : undefined,
                'RequestVerificationToken' : undefined,
                'Cache-Control' : undefined,
                'Debugging-Hint' : undefined,
                'ContentBlockId' : undefined,
                'ModuleId' : undefined,
            }
        }).then(function successCallback(response) {
            $scope.json_list_data = response.data;
        });

For external json api data I use this : http://www.mocky.io/v2/57aae03e120000be10739d3b created on www.mocky.io and this is the default jsonUrl for "string-nn-json-list". You can change this url in field settings.


EDIT(1)

I trying get moduleId with help of iid into controller but with no luck..

(function() {
    "use strict";
    angular.module("nnStringFromIntJsonData", [])
        .config(function(formlyConfigProvider, defaultFieldWrappers) {
        })
        .controller("FieldTemplate-String-Nn-Int-Json-List", function($scope, $filter, $modal, appId, debugState, eavAdminDialogs, $http) {
            // What to change in this code
            // that I can get iid (moduleId) in this part of code?
            debugger;
        })
        .run(
            function($templateCache) {
            })
})();

What I must change (add) to this code that iid is accessible?


EDIT(2)

One way but with lost of security (I use only for read not important data) is that way you remove Api controller attributes :

[DnnModuleAuthorize] and [ValidateAntiForgeryToken] and leave only [HttpGet]

and when calling from custom input type controller use something like :

var url = "/DesktopModules/2sxc/API/app-api/{ControllerName}/{MethodName}";
$http.get(url).then(function successCallback(response) {
    $scope.json_list_data = response.data;
});

I realy don't know what I am doing... It seems that more I learn less I know :-( and code with try/error system to get something partly usible...

Jernej Pirc
  • 504
  • 1
  • 4
  • 13
  • I add the part to get moduleId inside custom input-type js code – Jernej Pirc Aug 10 '16 at 14:20
  • I trying this becouse I want to use not only external data from web api but also application api values with call like : $2sxc(moduleId).webApi.post("Controller/Method" ... But don't know how to get iid inside this part of code. – Jernej Pirc Aug 10 '16 at 14:22