Change callback for get_spellCheckService().add_complete()

5 posts, 0 answers
  1. Henry
    Henry avatar
    5 posts
    Member since:
    Nov 2010

    Posted 21 Feb 2011 Link to this post

    I'm playing with your automaticspell example (see http://www.telerik.com/community/forums/aspnet-ajax/spell/outlook-like-functionality.aspx), and it works just great!  I've modified the example slightly so it's invoked from an ASP ImageButton click instead of the onblur event of a textbox.  So far, so good.

    Now what I'd like to do is have multiple buttons invoke the spellCheck script, and the results of the callback depend on which button was clicked.  One way I was thinking of doing this would be to have different callbacks for each of the buttons, and change to the appropriate one for the spellService.add_complete.  Is there a way to remove a callback from the get_spellCheckService?
  2. Rumen
    Admin
    Rumen avatar
    12851 posts

    Posted 24 Feb 2011 Link to this post

    Hi Henry,

    Can you please explain your scenario in more detail and if possible isolate the problem in a sample working project and send it for examination via a support ticket?

    Kind regards,
    Rumen
    the Telerik team
    Registration for Q1 2011 What’s New Webinar Week is now open. Mark your calendar for the week starting March 21st and book your seat for a walk through all the exciting stuff we ship with the new release!
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Henry
    Henry avatar
    5 posts
    Member since:
    Nov 2010

    Posted 24 Feb 2011 Link to this post

    Thanks for taking the time to reply.  It's not really a problem; more of a question.

    In your 'Outlook-like Functionality' example (see above link), you provide the following code snippet:

    <script type="text/javascript">
        var spellService = null;
      
        function spellCheck()
        {
            //Initialize spellcheck
            if (!spellService)
            {
                spellService = $find("<%= spellEditor.ClientID %>").get_spellCheckService();
                spellService.add_complete(spellCallback);
            }
            //Get text
            var text = $get("<%= txtSpellCheck.ClientID%>").value;
            spellService.spellCheck(text);
        }
      
        function spellCallback(sender, args)
        {
            if (args.badWords.length > 0)
            {
                //there are mistakes
                //call original spellcheck
                var spell = $find('<%= radSpellChecker.ClientID %>');
                spell.startSpellCheck();
            }
            else
            {
                //no mistakes, submit page
                submitPage();
            }
        }
        function checkFinished(sender, args)
        {
            // Supresses the javascript alert that occurs when
            // the spell check finishes.
            args.suppressCompleteMessage = true;
      
            // Fire button click event to do some server side work.
            //use small timeout to allow the spell control to update the textbox with the new value
            window.setTimeout(submitPage, 100);
        }
        function checkCancelled(sender, args)
        {
            // If they don't want to do the spell check, then fire off the server
            // side work.
            submitPage();
        }
          
        function submitPage()
        {
            var button = $get("<%= btnSubmit.ClientID %>");
            button.click();
        }
    </script>

    I'd like to lauch a spell check when a user prints a Save, Print or Next Record button.  An easy way for me to do this (without duplicating a lot of code) is to change the callback based on which button was pushed.  For example, if I push the Save button, I might want to set the callback to spellService.add_complete(spellCallbackThenSave); for the Print button, it might be set to spellService.add_complete(spellCallbackThenPrint) and so on.  Alternatively, of course, I could have the callback branch depending on the value of a parameter passed to it - except that I don't think I can change the callback prototype from your required function spellCallback(sender, args).

    So if I have to choose a different callback on the fly, how can I remove the current (if any) callback from spellService.add_complete and push a new one?

    If that still didn't make any sense, please let me know.

    Thanks again!
  5. Rumen
    Admin
    Rumen avatar
    12851 posts

    Posted 01 Mar 2011 Link to this post

    Hello Henry,

    It is not possible to modify the callback function arguments but you can use the following solution to check whether the pressed button is Save, Print or other and fire the spellchecker and execute custom code after that:

    The new code is highlighted in orange:
    <script type="text/javascript">
    var spellService = null;
    var buttonId = null;
    function spellCheck(id) {
        buttonId = id;
        //Initialize spellcheck
        if (!spellService)
        {
            alert(buttonId);
            spellService = $find("<%= spellEditor.ClientID %>").get_spellCheckService();
            spellService.add_complete(spellCallback);
        }
        //Get text
        var text = $get("<%= txtSpellCheck.ClientID%>").value;
        spellService.spellCheck(text);
    }
     
    function spellCallback(sender, args) {
        alert(buttonId);
     
        if (buttonId == "Save") { //spellcheck and save content }
        if (buttonId == "Print") { //spellcheck and print content }
        if (args.badWords.length > 0)
        {
            //there are mistakes
            //call original spellcheck
            var spell = $find('<%= radSpellChecker.ClientID %>');
            spell.startSpellCheck();
        }
        else
        {
            //no mistakes, submit page
            submitPage();
        }
    }
    function checkFinished(sender, args)
    {
        // Supresses the javascript alert that occurs when
        // the spell check finishes.
        args.suppressCompleteMessage = true;
     
        // Fire button click event to do some server side work.
        //use small timeout to allow the spell control to update the textbox with the new value
        window.setTimeout(submitPage, 100);
    }
    function checkCancelled(sender, args)
    {
        // If they don't want to do the spell check, then fire off the server
        // side work.
        submitPage();
    }
         
    function submitPage()
    {
        var button = $get("<%= btnSubmit.ClientID %>");
        button.click();
    }
        </script>
     
        <asp:TextBox ID="txtSpellCheck" runat="server" TextMode="MultiLine" Height="100%"
            ></asp:TextBox>
        <input type="button" id="Save" onclick="spellCheck(this.id);return false" value="Save" />
        <input type="button" id="Print" onclick="spellCheck(this.id);return false" value="Print" />
        <asp:Button ID="btnSubmit" runat="server" Style="display: none;" />
        <asp:Label ID="lblTest" runat="server"></asp:Label>
        <telerik:RadSpell ID="radSpellChecker" runat="server" ControlToCheck="txtSpellCheck"
            ButtonType="None" FragmentIgnoreOptions="All" SpellCheckProvider="EditDistanceProvider"
            SupportedLanguages="en-US,English" WordIgnoreOptions="None" OnClientCheckFinished="checkFinished"
            OnClientCheckCancelled="checkCancelled" />
        <div style="display: none">
            <telerik:RadEditor ID="spellEditor" runat="server">
                <Tools>
                    <telerik:EditorToolGroup>
                        <telerik:EditorTool Name="ajaxspellcheck" />
                    </telerik:EditorToolGroup>
                </Tools>
            </telerik:RadEditor>


    Greetings,
    Rumen
    the Telerik team
    Registration for Q1 2011 What’s New Webinar Week is now open. Mark your calendar for the week starting March 21st and book your seat for a walk through all the exciting stuff we ship with the new release!
  6. Henry
    Henry avatar
    5 posts
    Member since:
    Nov 2010

    Posted 01 Mar 2011 Link to this post

    Makes sense; then all I have to do is look at the buttonId variable in the submitPage function and not worry about the callback.

    Thanks again!
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017