How to load data from another file as an option value in JavaScript / jQuery?

Brett

Consider the below code:

$(".sceditor").sceditor({
    emoticons: // contents from another file here
});

I want to load another file which contains a JSON object as the value of the emoticons option.

I tried the below:

$(".sceditor").sceditor({
    emoticons: $.getJSON('../../images/emoticons/default/emoticons.json')
});

But that doesn't work because it returns a JQXHR object, not a JSON object.

To get the value I believe I would need to do something like:

$(".sceditor").sceditor({
    emoticons: $.getJSON('../../images/emoticons/default/emoticons.json', function(response) {
        // response is the JSON object
    })
});

But obviously that is not going to work as the response variable is inside the anonymous function and isn't returned to become the value of the emoticons value.

I know I could wrap the whole sceditor call inside the $.getJSON call, but I don't really want the whole code to rely on a successful call to the emoticons file.

I then thought about doing this above it:

$.getJSON('../../images/emoticons/default/emoticons.json', function(response) {
    var foo = response;
})

...but then how do I access foo outside the scope?

What is generally the best way to do what I am trying to accomplish?

jeremysawesome

I would use the .done() and .always() promise methods on the jqxhr object to accomplish this.

It sounds like what you want is to initialize the sceditor regardless of whether you have emoticons or not. However, if you are able to load emoticons then you want to use those.

Here is a quick solution showing how to use the promise methods to achieve what you want. Obviously you can expand and make this better, but this can serve as a starting point.

    // create a variable to store the results
    var emoticons = false;

    $.getJSON('../../images/emoticons/default/emoticons.json')
    .done(function(result){
        emoticons = result;
    })
    .always(function(){
        // always initialize the sceditor
        $('.my-selector').sceditor({emoticons: emoticons}); 
    });

And the requisite jsFiddle: http://jsfiddle.net/oLspfLby/1/

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

How to load xls data from multiple xls file into hive?

분류에서Dev

How to fetch data from another excel file and sort it under name?

분류에서Dev

Load rectangle data from txt file with Python?

분류에서Dev

Load rectangle data from txt file with Python?

분류에서Dev

Javascript - use to load xml data from URL

분류에서Dev

AdoNetAppender get connectionstring value from another file

분류에서Dev

Render data from/to Javascript/jQuery

분류에서Dev

How to continously load data in Riak with erlang file

분류에서Dev

How to automatically execute a javascript function from an external js file on page load with a specific condition

분류에서Dev

In jquery each Loop from json object How (make loop find a value from object and compare to another object value to not repeat a print)?

분류에서Dev

how to call a variable value in another php file

분류에서Dev

How to use jQuery to process html checkboxes populated from another PHP file?

분류에서Dev

How to add a dynamic value from one form to another page in javascript conversion coding?

분류에서Dev

How can I post data to another file and render from it using DomPDF

분류에서Dev

how do I insert data when the DB value of "position" is equal to "id" in jquery-file-upload

분류에서Dev

Get Data from JSON file into jQuery

분류에서Dev

How to access a XML file from another machine

분류에서Dev

Use osmar to load all data from an OSM file

분류에서Dev

How to fill TableView with data from another ViewController

분류에서Dev

How to count data from another table

분류에서Dev

how to return an integer value from a php file using ajax method of jquery?

분류에서Dev

How to use wget --wait option when using input from a file?

분류에서Dev

jquery $.each nested in another $each. getting data from a JSON

분류에서Dev

Appending JSON data from one file to another in Python

분류에서Dev

Navigate from one php file to another carrying array of data

분류에서Dev

Send values from one PHP file to another with JavaScript/AJAX

분류에서Dev

Pass a variable from a javascript function to another php file

분류에서Dev

How to insert the lines from a file into another file after a key word?

분류에서Dev

jquery breaking another javascript

Related 관련 기사

  1. 1

    How to load xls data from multiple xls file into hive?

  2. 2

    How to fetch data from another excel file and sort it under name?

  3. 3

    Load rectangle data from txt file with Python?

  4. 4

    Load rectangle data from txt file with Python?

  5. 5

    Javascript - use to load xml data from URL

  6. 6

    AdoNetAppender get connectionstring value from another file

  7. 7

    Render data from/to Javascript/jQuery

  8. 8

    How to continously load data in Riak with erlang file

  9. 9

    How to automatically execute a javascript function from an external js file on page load with a specific condition

  10. 10

    In jquery each Loop from json object How (make loop find a value from object and compare to another object value to not repeat a print)?

  11. 11

    how to call a variable value in another php file

  12. 12

    How to use jQuery to process html checkboxes populated from another PHP file?

  13. 13

    How to add a dynamic value from one form to another page in javascript conversion coding?

  14. 14

    How can I post data to another file and render from it using DomPDF

  15. 15

    how do I insert data when the DB value of "position" is equal to "id" in jquery-file-upload

  16. 16

    Get Data from JSON file into jQuery

  17. 17

    How to access a XML file from another machine

  18. 18

    Use osmar to load all data from an OSM file

  19. 19

    How to fill TableView with data from another ViewController

  20. 20

    How to count data from another table

  21. 21

    how to return an integer value from a php file using ajax method of jquery?

  22. 22

    How to use wget --wait option when using input from a file?

  23. 23

    jquery $.each nested in another $each. getting data from a JSON

  24. 24

    Appending JSON data from one file to another in Python

  25. 25

    Navigate from one php file to another carrying array of data

  26. 26

    Send values from one PHP file to another with JavaScript/AJAX

  27. 27

    Pass a variable from a javascript function to another php file

  28. 28

    How to insert the lines from a file into another file after a key word?

  29. 29

    jquery breaking another javascript

뜨겁다태그

보관