2012-06-19 2 views
1

직접 웹 서비스를 사용하여 Rhosync 또는 Rhoconnect없이 Rhodes를 사용하여 데이터를 동기화 할 수 있다는 것을 알고 있습니다. 그러나 여기서는 webservice 호출을위한 코드를 어디에 두어야하는지, 어떻게 초기화할까요? 누구든지 작은 모범을 도울 수 있습니까?Rhosync 또는 RhoConnect없이 Rhodes를 사용하는 방법?

미리 감사드립니다.

답변

3

나는 그것을 가지고 그것이 나를 위해 작동합니다. HTTP 서버 요청의

class ProductController < Rho::RhoController 
    include BrowserHelper 

    # GET /product 
    def index 
    response = Rho::AsyncHttp.get(:url => "example.com/products.json", 
    :headers => {"Content-Type" => "application/json"}) 
    @result = response["body"] 

    render :back => '/app' 
    end 

    # GET /product/{1} 
    def show 
    id [email protected]['id'] 
    response = Rho::AsyncHttp.get(:url => "example.com/products/"+ id +".json", 
    :headers => {"Content-Type" => "application/json"}) 

    @result = response["body"] 
    end 

    # GET /product/new 
    def new 
    @product = product.new 

    render :action => :new, :back => url_for(:action => :index) 
    end 

    # GET /product/{1}/edit 
    def edit 
    id [email protected]['product_id'].to_s 
    response = Rho::AsyncHttp.get(:url => "example.com/products/#{id}.json", 
    :headers => {"Content-Type" => "application/json"}) 

    @result = response["body"] 
    end 

    # POST /product/create 
    def create 
    name = @params['product']['name'] 
    price = @params['product']['price'] 
    body = '{"product" : {"name" : "'+ name +'","price" :"'+ price +'" } }' 

    @result = Rho::AsyncHttp.post(:url => "example.com/products.json", 
    :body => body, :http_command => "POST", :headers => {"Content-Type" => "application/json"}) 

    redirect :action => :index 
    end 

    # POST /product/{1}/update 
    def update 

    [email protected]['product']['name'] 
    [email protected]['product']['price'] 

    body = '{"product" : {"name" : "' + name + '","price" :"' + price + '" } }' 
    id = @params["product_id"].to_s 

    response = Rho::AsyncHttp.post(:url => "example.com/products/#{id}.json", 
    :body => body, :http_command => "PUT",:headers => {"Content-Type" => "application/json"}) 

    redirect :action => :index 
    end 

    # POST /product/{1}/delete 
    def delete 
    id = @params["product_id"].to_s 
    response = Rho::AsyncHttp.post(:url => "example.com/products/#{id}.json", 
    :http_command => "DELETE", 
    :headers => {"Content-Type" => "application/json"}) 

    redirect :action => :index 
    end 
end 
+0

좋은 사례가 나에게 완전한 예제를 줄 수 있니? –

0

가장 쉬운 형태는 다음과 같습니다 httpget_callback 컨트롤러 콜백 메소드의 이름입니다

Rho::AsyncHttp.get(
    :url => "http://www.example.com", 
    :callback => (url_for :action => :httpget_callback) 
) 

.

자세한 내용은 official Rhodes docs을 참조하십시오.