Wait an response from ajax request

Hi,

I was had some problem with javascript processing why I need the response from ajax request and with return do some process and manipulation inserting contents, something like that:

$(“#btn-export”).click(function (event) {
var graphicPage = $(“<div id=’content’></div>”);

$.get(“/Report/graphic.htm”, function (data) {
graphicPage.html(data);
});

graphicPage.find(“img”).each(function(key, value) {

$(value).attr(“src”,  document.location.protocol + “//” + document.location.host + $(value).attr(“src”));

});

//more processing

$(“body”).append(‘<form id=”exportform” action=”../Report/Export” method=”post” target=”_blank”><input type=”hidden” id=”exportdata” name=”exportdata” /></form>’);
$(“#exportdata”).val(graphicPage.html().toString());
$(“#exportform”).submit().remove();
});

But my problem was on the final to send to the resource to be processed as Excel spreadsheet, because the ajax request is asynchronous and the graphicPage variable sometimes is empty and another time contain the original content from request. How to I solve that?

I was search this link (documentation of jQuery.get()) and the last comment an user (Hyponiq) gave the solution to another user. Solution:

var value = (function () {
var val = null;

$.ajax({
‘async’: false,
‘global’: false,
‘url’: ‘path/to/url.php’,
‘success’: function (data) {
val = data;
}
});

return val;
})();

Using this solution I adapt to my problem and came up with this code:

$(“#btn-export”).click(function (event) {
var graphicPage = (function () {
var value = $(“<div id=’content’></div>”);

$.ajax({
async: false,
global: false,
url: “/Report/graphic.htm”,
success: function (data) {
value.html(data);
}
});

return value;
})();

graphicPage.find(“img”).each(function (key, value) {
$(value).attr(“src”, document.location.protocol + “//” + document.location.host + $(value).attr(“src”));
});

//more processing

$(“body”).append(‘<form id=”exportform” action=”../Report/Export” method=”post” target=”_blank”><input type=”hidden” id=”exportdata” name=”exportdata” /></form>’);
$(“#exportdata”).val(graphicPage.html().toString());
$(“#exportform”).submit().remove();
});

There, now could cause the variable to be filled before processing.

Thanks,

[Tip] Workspace Cannot Be Created with Titanium Studio

Hi,

I stopped again with posts, but soon will return.

Today I had a tip for someone had been the error message “Workspace Cannot Be Created” with Titanium Studio at startup.

I’ve found the solution that’s work for me.

Simply, open the Terminal and type:

/Applications/Titanium Studio/TitaniumStudio.app/Contents/MacOS/TitaniumStudio -data “<path to your workspace>”

After press Enter and your Titanium Studio will working perfectly again!

I had tried to change the config.ini file, but it still fails.

PS: The path above is of my setup on OS X.

Thanks,

Rails Tip: Prohibit certain route confuse other

Hi,

I searched the internet about the problem below, but it still fails.

In my rails application I had somes routes similar to the following:

SomeApp::Application.routes.draw do
  root :to => 'home#index'

  resources :person

  match '/:username' => 'users#show', :as => :profile
end

And I had a problem with routes’ priority and I put a username route at my last line in routes.rb. But the another problem was found if I had Rails Engine routes? (Example: an Rails Engine to blog with routes as posts and comments)

Remembering: the Rails Engine routes load after Rails routes of an application.

And the Record Not Found Exception was raised (I was configured to rendered 404 page on this exception), because the username posts or comments was not found in the database. Solution?

SomeApp::Application.routes.draw do
  root :to => 'home#index'

  resources :person

  match '/:username' => 'users#show', :as => :profile, :constraints => lambda { |request| User.find_by_username(request.path_parameters[:username]) != nil }
end

With the constraints added at my last line, my application avoid the username param was the same name of another route, the Record Not Found exception was raised passing to routes configured at Rails Engine.

I hope this tip can help others with similar problems.

Thanks,

MongoDb, MongoMapper and Rails 3

Hi,

This post is my first in english.

I was thinking to use mongodb (NoSQL database category) in one of my personal projects because your many benefits, some as: scalable and handle high volumes of data.

And I thinked that relational database was lazy to my specific problem. (As soon as possible I will show my project)

To test mongodb I choose made one rails app using mongo_mapper and was wonderful because I doesn’t have many problems to configure the tools since my machine was configured to rails development and need to install mongodb, generate rails 3 app, adapt the Gemfile to add mongo_mapper and bson_ext.

After bundle install, configure your database (I like this approach http://www.viget.com/extend/getting-started-with-mongodb-mongomapper/, I was think that form is more rails like) and in your application.rb inside config folder add codes line using mongo_mapper as orm generator.

Resuming the steps are to use:

1º Rails stack on your machine to development;

2º Install mongodb;

3º Generate a new rails app;

4º Configure the Gemfile to using mongo_mapper and add bson_ext dependency to mongodb;

5º Configure database (using approach like I use or another);

6º Put code lines in your application.rb to config orm generator to use mongo_mapper, like code below;

  config.generators do |generator|
      generator.orm :mongo_mapper
  end

7º Generate your business rules and happy.

 

In my example app I was used InheritedResources and  facility to develop models scaffolds. Another particular thing in my app was nested resource but nothing unusual to conventional rails app.

The example code at my github: https://github.com/brunoarueira/example_mongodb

 

Thanks

 

 

 

Seguir

Obtenha todo post novo entregue na sua caixa de entrada.