[Pkg-javascript-commits] [backbone] 01/15: Rails include_root_in_json tips.

Jonas Smedegaard js at moszumanska.debian.org
Sat May 3 16:59:29 UTC 2014


This is an automated email from the git hooks/post-receive script.

js pushed a commit to tag 0.3.2
in repository backbone.

commit 7e363dfc8f4950ee2aab1b5c5aeed18dedc67aae
Author: Jeremy Ashkenas <jashkenas at gmail.com>
Date:   Mon Nov 15 15:46:50 2010 -0500

    Rails include_root_in_json tips.
---
 index.html | 19 +++++++++++++++++--
 1 file changed, 17 insertions(+), 2 deletions(-)

diff --git a/index.html b/index.html
index 50cade2..5ed826c 100644
--- a/index.html
+++ b/index.html
@@ -786,6 +786,16 @@ account.set({access: "unlimited"}, {
       Override this if you need to work with a preexisting API, or better namespace
       your responses.
     </p>
+    
+    <p>
+      If you're working with a Rails backend, you'll notice that Rails' default
+      <tt>to_json</tt> implementation includes a model's attributes under a
+      namespace. To disable this behavior for seamless Backbone integration, set:
+    </p>
+    
+<pre>
+ActiveRecord::Base.include_root_in_json = false
+</pre>
 
     <p id="Model-clone">
       <b class="header">clone</b><code>model.clone()</code>
@@ -1465,12 +1475,17 @@ $(function(){
 
 <pre>
 def update
-  account = Account.find(params[:id])
-  account.update_attributes JSON.parse params[:model]
+  account = Account.find params[:id]
+  account.update_attributes params
   render :json => account
 end
 </pre>
 
+    <p>
+      One more tip for Rails integration is to disable the default namespacing for
+      <tt>to_json</tt> calls on models by setting <tt>ActiveRecord::Base.include_root_in_json = false</tt>
+    </p>
+
     <p id="Sync-emulateHTTP">
       <b class="header">emulateHTTP</b><code>Backbone.emulateHTTP = true</code>
       <br />

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/backbone.git



More information about the Pkg-javascript-commits mailing list