skozlov/netzke-basepack — Issue #51: Problem when use set_result for return value back from endpoint.
Author: virudson
Created: 2011-11-16 11:43:40+00:00
State: open

I have create an endpoint for create new Client.

At first I have create Client and use transaction (see code below).

It's work normally.

```ruby

# Before add set_result
js_method :on_new_contact, <<-JS
function() {
var form = this.items.first().getForm();
if (form.isValid()) {
this.newContact(form.getFieldValues());
this.closeRes = 'create_user';
this.close();
}
}
JS

endpoint :new_contact do |params|
data_hash = params.to_hash

Client.transaction do
new_client = Client.new

data_hash.each do |field, value|
new_client.__send__("#{field}=", value)
end

if new_client.save!
flash :notice => "Client #{new_client.full_name} has been created"
else
flash :error => new_user.errors.full_messages.to_sentence
end # if
end # transaction

{:netzkeFeedback => @flash}
end # endpoint
```

----------

And after I have add set result for return new Client id from endpoint.

It's save Client three times to my database.

```ruby
# After add set_result

js_method :on_new_contact, <<-JS
function() {
var form = this.items.first().getForm();
if (form.isValid()) {
this.newContact(form.getFieldValues(), function(value){
alert(form);
alert(value);
}, this);
this.closeRes = 'create_user';
this.close();
}
}
JS

endpoint :new_contact do |params|
data_hash = params.to_hash

Client.transaction do
new_client = Client.new

data_hash.each do |field, value|
new_client.__send__("#{field}=", value)
end

if new_client.save!
flash :notice => "Client #{new_client.full_name} has been created"
else
flash :error => new_user.errors.full_messages.to_sentence
end # if
end # transaction

{:netzkeFeedback => @flash, :set_result => new_client.id}
end # endpoint
```

I already try to find what the problem.

**And I found it happen when I use set_result with transaction.** But still don't know why.

If I remove `transaction` OR `set_result` code. It will work normally.

Thanks for the answer :)

**PS.** I'm sorry, If my English skill really bad.