Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using hunchentoot to parse post request sent by model.save() in Backbone.js

I am a javascript/web application newbie and trying to implement my first web application using hunchentoot and backbone.js. The first thing I was experimenting is to understand how model.fetch() and model.save() work.

It seems to me that model.fetch() fires a "GET" request and model.save() fires a "POST" request instead. Therefore, I wrote a easy-handler in hunchentoot as below:

(hunchentoot:define-easy-handler (dataset-handler :uri "/dataset") ()
  (setf (hunchentoot:content-type*) "text/html")

  ;; get the request type, canbe :get or :post
  (let ((request-type (hunchentoot:request-method hunchentoot:*request*)))
    (cond ((eq request-type :get) 
           (dataset-update)
           ;; return the json boject constructed by jsown
           (jsown:to-json (list :obj 
                                (cons "length" *dataset-size*)
                                (cons "folder" *dataset-folder*)
                                (cons "list" *dataset-list*))))
          ((eq request-type :post)
           ;; have no idea on what to do here
           ....))))

This is designed to handle the fetch/save of a model whose corresponding url is "/dataset". The fetch works fine, but I got really confused by the save(). I saw the "post" request fired and handled by the easy-handler, but the request seems to have only a meaningful header, I cannot find the actual json object hidden in the request. So my question is

  1. How can I get the json object out of the post request fired by model.save(), so that later json library (e.g., jsown) can be used to parse it?
  2. What should hunchentoot reply in order to let the client know that the "save" is successful?

I tried "post-parameters" function in hunchentoot and it returns nil, and didn't see many people using hunchentoot+backbone.js by googling. It is also helpful if you can direct me to some articles/blog posts that help understanding how the backbone.js save() works.

Thank you very much for your patience!

like image 556
BreakDS Avatar asked Oct 06 '12 23:10

BreakDS


1 Answers

Thanks to wvxvw's comments, I found the solution to this question. The json object can be retrieved by calling hunchentoot:raw-post-data. To be more detailed, we first call (hunchentoot:raw-post-data :force-text t) to get the post data as a string, and then feed it to jsown:parse. A complete easy-handler is shown below:

(hunchentoot:define-easy-handler (some-handler :uri "/some") ()
  (setf (hunchentoot:content-type*) "text/html")
  (let ((request-type (hunchentoot:request-method hunchentoot:*request*)))
    (cond ((eq request-type :get) ... );; handle get request
          ((eq request-type :post)
           (let* ((data-string (hunchentoot:raw-post-data :force-text t))
                  (json-obj (jsown:parse data-string))) ;; use jsown to parse the string
               .... ;; play with json-obj
               data-string))))) ;; return the original post data string, so that the save() in backbone.js will be notified about the success.

Hope this helps others who have the same confusion.

like image 146
BreakDS Avatar answered Nov 18 '22 15:11

BreakDS