-5

I would like to instantiate a new object with dynamic parameters coming from an external String.

Here is the code:

const editorInstance = new Editor('#edit',
  {
    placeholderText: null,
    theme: 'dark',
    language: 'en',
    linkList:[{text: 'test',href: 'test',target: '_blank'}],
    events: {
      initialized: function () {
        const editor = this

        this.el.closest('form').addEventListener('submit', function (e) {
          jQuery('#gs_editor_content').hide();
          jQuery(this).append('<div class="loadingDiv">&nbsp;</div>');
          o.script
        });

        texta = jQuery('#myeditor').find('textarea');
        targetFile = texta.attr('rel');
        content = editor.$oel.val();

        e.preventDefault();

        var fd = new FormData(); 
        fd.append( 'name' ,targetFile);
        fd.append( 'html', editor.$oel.val() );
        $.ajax({
          url : 'http://localhost/Update',
          type : 'POST',
          data: fd,
          processData : false,
          contentType : false,
          async : false,
          success : function(data, textStatus, request) {}
        });

        jQuery('#myeditor').dialog("close");

      }
    }
  }
)

I would need to modify the parameters linkList before instantiating my object as I receive a new list received from my server.

I tried to use eval or parseFunction but I encounter an unexpected identifier error.

any idea how I can achieve this ?

EDIT

I really need to update the parameter before creating the Object, which is not the same thing as mentioned in the question suggested as duplicate. By the way I also need to create my object after updating the parameter...

EDIT 2

I also tried to simply include my var containing the String but it doesn't work:

var dropdownFiles = "[{text: 'test',href: 'test',target: '_blank'}]";
const editorInstance = new Editor('#edit',
  {
    placeholderText: null,
    theme: 'dark',
    language: 'en',
    linkList:dropdownFiles,
    events: {
    .....
Wyck
  • 10,311
  • 6
  • 39
  • 60
tiamat
  • 879
  • 2
  • 12
  • 35
  • How is your `linkList` received from the server? It probably is sent as a JSON string no? In this case, try using `linkList = JSON.parse(yourData)`. – Tino Caer Jul 24 '19 at 05:58
  • 1
    You should then be able to pass `linkList` to your new class. – Tino Caer Jul 24 '19 at 05:59
  • the linkList is a simple String – tiamat Jul 24 '19 at 06:12
  • Is it in JSON format? – Tino Caer Jul 24 '19 at 06:13
  • Possible duplicate of [Dynamically access object property using variable](https://stackoverflow.com/questions/4244896/dynamically-access-object-property-using-variable) – GalAbra Jul 24 '19 at 06:15
  • yes, but I'm formatting input data with the expected format, so this is a pure String, but I cannot include directly linkList = myString in the instantiation.... – tiamat Jul 24 '19 at 06:15
  • Can you give me an example of what linkList would look like as a pure string? and what the input data is? – Tino Caer Jul 24 '19 at 06:19
  • Sure, this is an example: [{text: 'script1.pdf',href: 'null',target: '_blank'}{text: 'script2.pdf',href: 'null',target: '_blank'}{text: 'script3.pdf',href: 'null',target: '_blank'}] this is the only parameters I need to update dynamically before instantiating my Editor – tiamat Jul 24 '19 at 06:23
  • input data received from server are text and href values for each item – tiamat Jul 24 '19 at 06:31
  • Why doesn’t the server return this as proper JSON? If you are in control of the server-side part, then you should simply fix that, that would make the rest probably quite easy to solve. – misorude Jul 24 '19 at 07:07

3 Answers3

1

If I understood your problem right, you're trying to update the linkList before including it to your new object?

If that's the situation, you could use something promise based, as fetch to fetch your data: Using Fetch by MDN documentation. You're already using JS Classes, so I'm sure you are already aware Browser compatibility or using Babel (If browser compatibility is important for you).

If you prefer using jQuery AJAX for fetching data, you could use async await to update linkList before your software instantiates the object. MDN Documentation for Async Await.

In this case, you could do something like this:

async function getData(paramIfNeeded) {
  var linkListData = await linkListFunction(paramIfNeeded2);
  return linkListData
}

function linkListFunction(paramIfNeeded2){
  //Your favourite data fetching function
}

getData().then((result) =>{
  var linkList = result;
  //instanciate the object
});

If you prefer ES 2015, something like this could be done:

function callData(){
  getData(paramIfNeeded)
  .then(result => {
    linkListFunction(paramIfNeeded2)
    .then( result => {
      //do things with linkList
    });
  });
}

I hope, this helps.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Kuu_Tamo
  • 302
  • 2
  • 10
0

There is no problem in change a value before the instantiation. The problem here, as I understand, is that you have an invalid JSON, see this https://json.org/. If you have control over the server response, you should send a valid JSON, otherwise, you should parse the string. As you say in comments, you have something like:

"[{text: 'script1.pdf',href: 'null',target: '_blank'}{text: 'script2.pdf',href: 'null',target: '_blank'}{text: 'script3.pdf',href: 'null',target: '_blank'}]"  

if you can change the server response, turn that into somethig like:

'[{"text": "script1.pdf","href": "null","target": "_blank"},{"text": "script2.pdf","href": "null","target": "_blank"},{"text": "script3.pdf","href": "null","target": "_blank"}]'

and then:

class Editor {
    constructor(array){ 
        this.linkList = array;
    }
}

const json = '[{"text": "script1.pdf","href": "null","target": "_blank"},{"text": "script2.pdf","href": "null","target": "_blank"},{"text": "script3.pdf","href": "null","target": "_blank"}]';
 
const array = JSON.parse(json).map(e=>{e.target="_self"; return e;});

//You could change what you want before the new instance
const e = new Editor(array);

console.log(e);
Emeeus
  • 5,072
  • 2
  • 25
  • 37
  • Hi Emeeus, it's not a problem of JSON format, just a problem on how to instantiate an object with a dynamic parameter (either a String or a JSON it doesn't matter). As you can see, my Objet Editor is having multiple parameters, all are static ones except parameter linkList which is provided by my server. – tiamat Jul 26 '19 at 14:44
0

The problem is probably caused by async. your class is initializing before the response received from your server. You need to initializing class after the response received.

For example

const editorInstance = new Editor("#edit", {
 placeholderText: null,
 theme: "dark",
 language: "en",
 linkList: serverExample() // undefined
});

function serverExample() {
 setTimeout(() => {
  return [
   { text: "test", href: "test", target: "_blank" },
   { text: "test2", href: "test", target: "_blank" }
  ];
 }, 3000);
}