Access to controller variable from CoffeeScript - coffeescript

I have #list value in my controller and i have view where i m adding products.Form looks like product_name and Add Button.I need to add all thees values to #list thought ajax request using coffe script or some other way, where i will not submit whole form.
How to achieve that? Thanks.

Your question sounds quite strange...
First, any CoffeScript source will be compiled to Javascript, which is downloaded on the client side (in the browser). Your controller is running server-side, so it is only addressable through HTTP requests.
Nevertheless, an #list attribute is not persistent between two HTTP requests, so you will have to persist any change if you need to accumulate data, as you state in your question.
To achieve the AJAX call, I would recommend to use JQuery's helpers: http://api.jquery.com/jQuery.ajax/, or shortcuts like http://api.jquery.com/jQuery.get/, http://api.jquery.com/jQuery.getJSON/, ...
The rest is Rails routing, as can be teached in http://guides.rubyonrails.org/action_controller_overview.html#methods-and-actions and http://guides.rubyonrails.org/routing.html.
Hope this helps.

Related

Get bean->id when in _headerModuleList.tpl (suiteCRM, SugarCRM CE)

I'm trying to adjust the nav bar at the very top when inside a specific bean, but I can't figure out how to read the current module name/bean->id
(or if it's even possible).
As a workaround I thought of indicating a custom header, but in meta we can only put custom headerTPL, example
$viewdefs[mod][DetailView][templateMeta][form][headerTpl] => 'custom/themes/SuiteP/tpls/headerModuleList_c.tpl',
But we can't indicate a custom headerMODULELIST it seems
Would appreciate your help
You can USE Jquery as well with ajax enabled or even ajax disabled modules. This is beneficial for you only if you need these values at Browser Side.
Anyhow,
Try these
$("input:hidden[name='record']").val()
$("input:hidden[name='module']").val()
For going more specific you can access it by Parent Form ID
$("form#formDetailView input:hidden[name='module']").val()
$("form#formDetailView input:hidden[name='record']").val()
For Server Side you can try this thing to get URL and you can parse it accordingly
$url = "//{$_SERVER['HTTP_HOST']}{$_SERVER['REQUEST_URI']}";
In the end the only thing that worked for me is disabling the AjaxUI which loads the page partially and makes any sort of complex modification to the header navigation pretty difficult
Once disabled you can just call $_REQUEST in _headerModuleList.tpl to get all the details you need

How do relative URLs work in Sinatra?

I am hosting my Sinatra application using Apache with Passenger. It's hosted within a subfolder -- meaning, my main site is example.com, my application is at example.com/popcorn.
So I have a get '/' route, and that works fine. The problem is that my view includes a HTML form that makes a post request to upload, and the post '/upload' route isn't handling it. Instead of example.com/popcorn/upload, it's trying to get example.com/upload.
So I figure okay, not the ideal solution, but for now I'll hardcode the form action URL. But that doesn't work either -- making the action popcorn/upload fails too. This is where I get a little baffled, and my Google-fu was weak, I couldn't find help there.
Maybe I could have some kind of Apache rewrite rule, but is that the correct solution? Am I missing something? I would really appreciate a tip here because it feels like I've messed up something very simple and it's really bugging me.
You probably want the url helper method. That takes into account where the app is mounted on the server:
url('/upload')
The above code will evaluate to something like this:
http://example.com/popcord/upload
Inside your app you shouldn’t need to change anything, this will be routed to the existing post '/upload' handler.

Stopping Ember.js Controller

My question is very basic on one hand but on the other hand the general situation is more complex, plus I cannot really get any working sample.
I'm developing/maintaing a web-application which is currently in transition from GWT code base into Ember.js.
Most of the newer code already relies on Ember.js and I think it's really awesome.
The problem is we cannot use Ember Router as all the request are being handled by the GWT.
In order to enabled the application run in this unusual configuration we have special JavaScript files that create our Ember main objects (Controllers & Models) for us.
As you can imagine navigation between tabs is cumbersome and is handled by GWT who creates Ember objects when needed. We are in transit toward a brave new world Ember Router and all.
But in the meantime, this is the problem I'm facing right now.
The user clicks a link which opens a page that contains some Ember based table.
The data is retrieved form the server using some Ajax code. Upon success it spawns a forEach loop which tries to pushObject all the received date into our Ember based components.
My problem happens when the user quickly switches between tabs. In this case the first list of object has not finished rendering yet and suddenly there's a new set of objects to handle. This causes Ember to throw errors like:
"Uncaught Error: Cannot perform operations on a Metamorph that is not in the DOM. "
and
"Uncaught NotFoundError: An attempt was made to reference a Node in a context where it does not exist."
Is it possible to prevent the loop from trying to render?
I've tried checking if the controller in question is already inDOM and it is, is there a way to notify Ember this object is no longer valid?
Sorry for a lengthy question and lack of running sample.
I'd probably modify the switch tab code to only execute afterRender has completed, that way you aren't mucking with ember objects while they are being used.
Ember.run.scheduleOnce('afterRender', this, function(){
// call GWT switch tab routine
});
Thank you Daniel and Márcio Rodrigues Correa Júnior. eventually what I did is to add a patch that would check the current context of the application (in my case the currently selected tab). If upon receiving the AJAX response the application is in the correct context (meaning the user haven't change the tab) go on. Otherwise just ignore the response and do not try to render it.
Now it seems to be working

Can I call session in template/view on Play Framework

I'm new at using Play Framework 2.0 (I am using Scala) and have a question about sessions.
I come from a Ruby on Rails background, so I tend to think of everything that I learn in Play Framework with respect to Ruby on Rails.
With that in mind, is there any way for me to call stuff stored in the Session while I am in the view?
If I have "hello" -> "world" stored in the Session, I want to be able to do something like #session.get("hello") and be able to use "world" in the view. Is this possible?
The other option I see is to store the value in a variable in the controller, and pass that along to the view by doing something like OK( var ), but that way seems kind of clunky especially if I start using more variables.
Thanks!
Sessions in Play store in cookies and are really just for cross-request data. If that is what you want then you can use #session.get("hello") but what you might actually be after is a way to pass stuff from controllers to templates without having to specify them as parameters. In that case, see the very detailed answer to that question here:
https://stackoverflow.com/a/9632085/77409
Yes you can use #session.get("hello") in template, however it looks that you need to specify at least implicit parameter named 'session' at the beginning of the template when using templates with Scala controllers:
#()(implicit session: play.api.mvc.Session)
Also there is flash scope - it differs from session that it lives only for one request and is not signed. So it is most often used just for transporting error/inf messages.
See Session and flash scopes doc
Finally as each template is just a Scala function, you can also call some action from your controller and retrieve session data
You can definetly call ur session in play templates.
Try this code - it works in views:
$session.session_variable_name;

How to get request properties in routeStartup plugin method?

I'm developing a multilanguage application, and use routes with translated segments. For multilingual support I created special Multilingual plugin.
To use translated segments I need set translator for Zend_Controller_Router_Route before routes init. So only possible place for this in my plugin is routeStartup method, but there is one problem here - for determine right locale I need to use properties of request (Zend_Controller_Request_Abstract), like module, controller and action names, but they are not defined yet here in routeStartup method. They are already defined, for example, in routeShutdown - but I can't set translator for route there, because it have to be done before routes init.
So what can I do:
can I get request properties somehow in routeStartup
or can I re-setup translator later in routeShutdown
P.S: there is a question with exactly the same problem Zend_Controller_Router_Route: Could not find a translator, but proposed answers is not the option for me, because I can't just retrieve language code from url with Regex, I have much more complicated code to define right language code.
Thanks.
What about putting your code in preDispatch? That's what I personally do when I need to check if the person is logged in. Maybe you can move your code there too?