Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I came up with a solution to completing the problem: how to get data from client to server using the default backbone save() and .sync - passed over to the Slim php framework and going through Paris/Idiorm to my DB.</p> <p>I am including my working updated code below:</p> <p><strong>Client-side: Backbone.js</strong></p> <pre><code>var Donut = Backbone.Model.extend({ defaults: { name: null, sparkles: false, creamFilled: false }, url: function() { return '/donut'; } }); var bostonCream = new Donut({ name: 'Bawston Cream', sparkles: true, creamFilled: true }); bostonCream.save(); /***** If you want to check out the response to save() ? *** bostonCream.save({}, { success: function(model, response) { console.log('SUCCESS:'); console.log(response); }, error: function(model, response) { console.log('FAIL:'); console.log(response); } }); ************************************************************/ </code></pre> <p><strong>Sever-side: Slim PHP w/ Paris/Idorm</strong></p> <pre><code>class Donut extends Model {} $app-&gt;post('/donut', function() use ($app) { $donuts = Model::factory('Donut')-&gt;create(); /* EDIT: Works... but not the Slim way $parameters = json_decode(file_get_contents('php://input'), true); $donuts-&gt;name = $parameters['name']; $donuts-&gt;sparkles = $parameters['sparkles']; $donuts-&gt;creamFilled = $parameters['creamFilled']; */ /* SLIM: Using Slim Request Object */ $requestBody = $app-&gt;request()-&gt;getBody(); // &lt;- getBody() of http request $json_a = json_decode($requestBody, true); $donuts-&gt;name = $json_a['name']; $donuts-&gt;sparkles = $json_a['sparkles']; $donuts-&gt;creamFilled = $json_a['creamFilled']; $donuts-&gt;save(); // echo json_encode($parameters); // Prove you've captured POST data, send it back } </code></pre> <p>Now my code is happily using the default settings of Backbone.js (no changes to sync) and sending proper model attribute information to my server which seems to be successfully accepting the data and saving it to my DB.</p> <p>The key here seems to be this line...</p> <pre><code>/* $parameters = json_decode(file_get_contents('php://input'), true); */ // EDITED: getBody() method not documented in Develop Doc, only Stable @ time of post $requestBody = $app-&gt;request()-&gt;getBody(); </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload