0

I am learning dojo and a begginer. I would like to load and edit a text file from interface using Dojo. Please direct me which component in dojo would suffice this.

Skanda
  • 835
  • 1
  • 15
  • 32
  • why do you ask for dojo ?? if java already have option to manipulate text file – Garry Feb 13 '13 at 14:55
  • The reason i am looking for dojo is because i am looking to edit the file from user interface and save it back. Please suggest me any other way to suffice this. – Skanda Feb 13 '13 at 15:14
  • 1
    This question is seriously lacking in details. How is this text file obtained? From your server; from another server; via a WebService; user upload; input into textfield ...what are you looking for? What have you tried already? – Stephen Simpson Feb 13 '13 at 15:25
  • I have an interface where i key in the file name available on my server and am looking to load the file content into my interface. On editing the same my file need to upload/save back to my server. I understand as Garry mentioned dojo cannot do it. – Skanda Feb 13 '13 at 15:33

1 Answers1

0

From the answer to my comment, my understanding is that you want to:

  1. Enter a file name into a form on your webpage.
  2. Dojo will then load that particular text file from your sever.
  3. The file then needs to be displayed on the screen in some sort of editing componant.
  4. The user then needs the facility to post the file back to the server.

I would have thought that the best approach is to use dojo/request to get the text file and then use it again to post it back. You could the various dojo dijits to do the selecting and displaying.

A very crude solution would be:

<form data-dojo-type="dijit/form/Form">

    <input type="text" id="fileName" data-dojo-type="dijit/form/TextBox" />
    &nbsp;<button data-dojo-type="dijit/form/Button" type="button">Get
        <script type="dojo/on" data-dojo-event="click">
            require([
                "dijit/registry",
                 "dojo/request"
            ], function(registry, request) {
                var fileName = registry.byId("fileName").get("value");

                request(fileName, {
                    "handleAs": "text"
                }).then(function(content){
                    registry.byId("content").set("value", content);
                });
            }); 
        </script>
    </button><br /><br />

    <textarea id="content" data-dojo-type="dijit/form/TextBox"></textarea>
    <button data-dojo-type="dijit/form/Button" type="button">Send
        <script type="dojo/on" data-dojo-event="click">
            require([
                "dijit/registry",
                "dojo/request"
            ], function(registry, request) {
                var content = registry.byId("content").get("value");
                request("myhandler.php", {
                    "method": "post",
                    "data": {
                        "content": content
                    }
                }).then(function(content){
                    // deal with the response
                });
            }); 
        </script>
    </button>

</form>

This will load a text file with the filename you enter in the text box (after clicking get). The content is loaded into the textarea for editing and can be sent back to a server script by clicking send.

This is as I said, "a very crude example". However, it shows the use of dojo/request to receive and post information to/from the server. Obviously, you'd want a more sophisticated solution that hides/shows widgets at the appropriate moment. You would probably want to replace the filename textbox with a some sort of combo populated via your server code...etc, etc.

I'd suggest you write your own widget to encapsulate the whole operation, rather than declare it all in markup. You could use dojo/request to load a json file from the server to populate a combo box to select the file. You'd also want to ensure the information being posted back is from a trusted source.

Important! This will only work if the textfile and you webpage are residing on the same domain. It will not work for cross-domain requests. If you want to do cross-domain you'll need to create a json solution.

Stephen Simpson
  • 1,381
  • 1
  • 11
  • 23