Another way for Htmldialog get_element_value. Need Help

2017

#1

When get_element_value do not support on HTHML dialog. So i deicide make a method called get_value to replace it. This is my code at first i write this method.

     # param 1: dialog was created before, html id
def get_value(dlg,id)
  # temporaty var
  @get_value_tmp_data ='';
  # add new action callback 
  dlg.add_action_callback("getvalue"){|dialogs, var|
    # assgin data
    @get_value_tmp_data = var
    puts 'go new value:' +@get_value_tmp_data
  }
  dlg.execute_script("get_element_value('"+id+"')")
   puts 'last checking before return:' @get_value_tmp_data
  return @get_value_tmp_data
end

The javascript is:
function get_element_value(e) {
var value = $(e).val();
window.location=‘skp:getvalue@’+value;
}
When i run the medthod everything seem be ok but the last checking before return is not. That return the old value = ''
Thanks for reading and helping me.


#2

in your HtmlDialog javascript you should be using…

sketchup.getvalue(value)

does that not work?

john


#3

As John suggest, passing the values directly to the sketchup. callback is the way to go with HtmlDialog. Because HtmlDialog is all async one need a different way to pass data - this means using callbacks. Another aspect that is different is that where one often used input element to pass data with the old WebDialog because the skp: callbacks had encoding and size issues the new sketchup. callbacks are much better in that they don’t have any size limits and they also automatically convert simple types, so you can send arrays or hashes of data from JS to Ruby with ease.


#4

@nhanco102 I’m trying to find an efficient solution to emulate the deprecated get_element_value method in UI::HtmlDialog, and I ended up here. In my case, it is not working either with

Did you find a solution?


#5

Can you elaborate on what isn’t working with sketchup.getvalue(value)? (This assumes you defined a callback “getvalue” on the Ruby side.)


#6
# Ruby
module MyTopLevelNamespaceModule::MyExtensionModule

  class ChromeDialog < ::UI::HtmlDialog
  
    def initialize(*args)
      super(args)
      @values = {}
    end

    def attach_callbacks
      add_action_callback('receiveValue') do |not_used,id,val|
        receive_value(id,val)
      end
    end

    def get_element_value(id)
      return unless id.is_a?(String)
      execute_script("sendValue('#{id}');")
    end
    
    def receive_value(id,val)
      @values[id]= val
      process_value(id)
    end

    def process_value(id)
      # Do something with @values[id]
    end

    def show(*args)
      attach_callbacks()
      super
    end

    def show_modal(*args)
      attach_callbacks()
      super
    end

  end # dialog subclass
end # extension module
// JavaScript:
function sendValue(id) {
  sketchup.receiveValue( id, document.getElementById(id).value );
}

#show_modal - HtmlDialog vs. WebDialog
Alternative to get_element_value method for HtmlDialog
Alternative to get_element_value method for HtmlDialog
Alternative to get_element_value method for HtmlDialog
#7

@DanRathbun That definitely works. Thanks.


#8

I updated the example, as it really should not have worked as it was.

I had not actually attached an action callback to the dialog object.
This is now done in the attach_callbacks() method, inside the show() and show_modal() method overrides.
This needs to be done this way because action callbacks are detached if the dialog is closed.


#9

Didn’t notice that, it worked for my scenario if I remember well, maybe with a little tuning. Thanks for the update, anyways.