Question

I'm developing a simple(!) 2 page application.

Page1: Takes basic details from the user about the program is creating for his trip - name of the trip, dates etc.

After clicking Save, a entry is created on MongoDB using controllers in Lithium PHP for the program. A id is generated and passed on to page 2 as a parameter in URL like so:

$this->redirect('/iplans/save/' . $program_id . '/' . $program_name);

Page 2: is a single page application using many Backbone scripts (hosted in individual js files) and allows the user to add datewise details of his program. No js script is in-line.

I want the backbone collection to be saved on the server side when Finish is pressed but need to send in the program_id with this collection so that the right program is updated in MongoDB.

Questions:

  1. How does one ensure that the program_id from lithium/php server side is picked up by backbone javascripts that are in their own js files (not inline)
  2. What other options are available besides Controller::redirect to forward the control from page1 to page2?
  3. What other options can be used to pass-on the program_id and any other values that may be needed to backbone/JS layer. Is there a standard-way of passing such items?
Was it helpful?

Solution

  1. There are a couple of options:

    1. Parse out program_id from window.location.href
    2. Dynamicly create a bit of javascript in your lithium view. it could be as simple as:
      "<script> myvars = " . json_encode(array(...)) . "<script>"
      and then you can read the variables from the javascript.

    3. Create the onDomRady function dynamically from lithium

  2. That is a good way. That is, one page show form and receive data from the form, and one page that show some data. They are not related to each other, and it will make the code easy to understand.

  3. You could use AJAX/JSON to send/receive data to/from the server. Also see answer 1b,1c

OTHER TIPS

If it's just a variable being written to the session that you need to access in a single-page app, just make a controller (i.e. SessionsController) that returns the current session information, and turn content negotiation on.

Then you can get whatever keys you need any time.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top