This page describes how to perform various functions using the Rhodes / RhoSync frameworks and RhoHub.
Edit the app/index.erb
to create some content:
<div class="toolbar"> <h1 id="pageTitle"> Small demo app </h1> </div> <ul id="home" selected="true" title="Hello"> <li><a href="another.erb">Hello, world which links to another page</a></li> </ul>
The generated code from running (rhodes model <modelname> <attributelist>
) contains several samples. Here’s a smaller fragment.
<form method="POST" action="<%= url_for(:action =>:update) %>"> <input type="submit" value="OK"/> </form>
Adding to the above example:
<ul id="home" selected="true" title="Hello"> <li><a href="another.erb"><img src="/public/images/bluebutton.png"></a></li> </ul>
This is done with AsyncHttp
. You will then use either JSON (if its a JSON web service) or REXML (if the web service returns XML) to process the data.
See the AsyncHttp
example code which connects to an XML feed at and parses it with rexml.
To make a POST request, use the AsyncHttp.post
method. See the AsyncHttp.post
docs here.
Yes, first add the following to your build.yml
:
extensions: ["rexml", "set"]
Now parse the xml in your controller action:
require 'rexml/document' file = File.new("bibliography.xml") doc = REXML::Document.new(file) puts doc
If you are planning to run your app on Blackberry, you should use the RhoXml
extension. It has the same syntax as rexml, but has limited functionality and a much smaller memory footprint. Enable it in your application by adding to build.yml
:
extensions: ["rhoxml"]
Now parse the xml in your controller action: require ‘rexml/document’ file = File.new(“bibliography.xml”) doc = REXML::Document.new(file) puts doc
If you have a very large XML document to parse, you should use the XML stream parser extension.
You can either use the built-in Rhom functions or use a database transaction:
db = ::Rho::RHO.get_src_db('Model') db.start_transaction begin items.each do |item| # create hash of attribute/value pairs data = { :field1 => item['value1'], :field2 => item['value2'] } # Creates a new Model object and saves it new_item = Model.create(data) end db.commit rescue db.rollback end
If ::Rho::RHO.get_src_db(‘Model’) return nil, it means that you never call this models methods before(models are loaded by demand). To fix it call ‘require_source’:
require_source 'Model'
When controller action is called Ruby Garbage Collector is disabled. If you create a lot of objects enable GC at the beginning of action:
def my_objects_generator GC.enable #Do some heavy operations end
To avoid UI block while controller action is performed, use AsyncHttp to make operation asynchronously (Local server will be blocked, but Html and JavaScript are not): :::ruby def start_objects_generator #callback has to be provided, otherwise operation will be synchronized AsyncHttp.get( :url => url_for(:action => do_objects_generation), :callback => (url_for :action => :generation_callback) ) render page_with_progress_updated_by_javascript end
def do_objects_generation GC.enable #Do some heavy operations #do not call render here, call WebView.navigate or do it in generation_callback end def generation_callback WebView.navigate ( url_for :action => :show_results ) end
Follow the build section on setting the application name and icon.
Before test application for performance set warning log level in rhoconfig.txt:
MinSeverity = 3
Database is most critical part for performance improvement. See Database Performance Tips
- public/images/iphone - public/images/android
It takes a bit of time for WebView to load and parse html/css of your page; meanwhile WebView show empty page which is white by default. Try setting default background color of the toolbar to the color of your pages: :::ruby Rho::NativeToolbar.create( :buttons => toolbar, :background_color => 0x0000FF ) Please see more details about toolbar here
If you are loading JavaScript, consider doing it at the very end of your page; this way WebView will not wait for JS before parsing css and showing html.
Edit app/application.rb file: initialize method
class AppApplication < Rho::RhoApplication def initialize #To remove default toolbar uncomment next line: #it should be before super @@toolbar = nil super end end
There is no way to define controller for top-level index.erb, so create special model and put index.erb there:
in rhoconfig.txt modify ‘start_path’ :
start_path = '/app/Home'
‘Home’ model index method:
def index #do something render end
Open this file by
System.open_url(file_path)
Print document from document application
Add ‘mapping’ and ‘apikey’ parameters to your build.yml
android: mapping: yes apikey: <YOUR-API-KEY>
Add ‘network_state’ to capabilities list in your build.yml.
capabilities: - network_state
There are several Rhodes components which consume system memory( list order by memory consumptions, bigger first):
Use system memory and a lot of graphics objects to display content. Rhodes Turn on Cache in Browser for static files like images, css, JavaScript etc
Tips to optimize Browser performance and memory consumption:
Use system memory. Here is the notes about RubyVM memory usage:
Tips to optimize RubyVM performance and memory consumption:
Use system memory. Here is the notes about Sqlite memory usage:
Use System memory and graphics objects.
Tips to optimize Native Controls performance and memory consumption: