Backbone sends form through GET - javascript

I'm learning about Backbone.js and I built an app which has a form.
Following the rules, I have a Book model, a Library collection, and a Book view. This view controls the form.
Everything runs ok, the communication with the API (which is running on Node.js) is also ok. But when I submit the form to save a new book, Backbone sends it through GET, and my API is waiting a POST (as it is used by CRUD operations).
The form submission is done by Backbone; when I click on submit, the called action is this.collection.create( new BookModel(formData) ). As we can see on Backbone's documentation, this action sends the model to the server, and after that the model is added to the collection.
Backbone.sync says that it sends the data through POST, but it is sending through GET.
I'm following the example of Developing Backbone.js Applications book.

I found the problem. I set the view's el as tbody (I would like to have all the books listed inside a table).
If I set the el to body it works. It seems that we have to set a exclusive View to the form, and another one to the list (table).

Related

django rest framework API edit function

though I am new to django rest framework, all in all, i get how the posting and viewing works each using jquery ajax to post and angular js for rendering the API json data.
but i don't understand yet how the 'edit' and 'delete' function should be implemented here.
it means i have to load preexisting title and contents to the designated field forms and resave the post into that specific post id.
how can i do that?
and how can i check the permission when executing edit or delete function using either jquery or angular?
please consider the fact that my website is SPA (single page app) that shouldn't require any sort of page refresh.
so these concepts are fairly new to me, and i don't understand how i can manually check the permission using only the API
here is the live site : http://192.241.153.25:8000
You can use class based views for this. Using class based views you can have different end points for different functionalities, differentiating on Request types.
class AView(APIView):
def get(self, request, format=None):
pass
def put(self, request, format=None):
pass
for authentication and permissions do refer to http://www.django-rest-framework.org/api-guide/authentication/#tokenauthentication

Multiple part form - update database each submission

I'm sure this isn't the hardest issue to overcome, but I have a lack of understanding on how to handle this particular situation.
Learning MongoDB, Express, Angular, Node to create a web application for learning purposes.
I have a 2 stage submission form.
Step 1: Pet info is collected. text based input (pet name, pet description... ).
Submission of this info goes through my client side controller. Processed and saved to mongo as a new entry.
Step 2: Using ng-show/ng-hide, the first form is hidden and a second input form is shown for submitting an image.
Step 2 upload doesn't use a client side controller. Instead I make a direct method=post call to my express route. Multer module is called as middleware to upload my picture to a server side directory and generate JSON image info.
All I want to do is follow up with an update to mongoDB to insert the multer image_path into my previous mongodb collection, so that I can later render the image directly from the upload folder.
My issue is that I can't figure out how to pass the "_id" of the first steps submission to the 2nd step, so that I can perform the update.
How does one go about passing data that I have in my client side controller to my server side controller.
Can I use my view or do I need to call another express route to pass the data?
My project resides at https://github.com/astemborskim/anidopt.git
Currently working out of the temp branch.
Also posting the view, client side and server side controllers in the following gist.
https://gist.github.com/astemborskim/edc5d4a80dfe6e154c99
Here is one possible way of doing multiple part form.
You can use Bootstrap and use Tagglable Tabs
You can always customize it to your liking. Maybe hide or redesign tabs to look like a step. Then have your part one form submit/next button to toggle the second tab
Each tab will represent you form part
Now, when you submit you first part, you will get the _id back
and can use it for further requests

MVC query model data with jQuery

In my MVC site I am displaying data from my model in a table in the view. On the first column (which is a unique number within the model data) I have made it into a click-able link that displays an alert box using jQuery.
What I would like to do is when the link is clicked it queries the model for another piece of data using this unique number and displays that in the alert.
Would someone be able to help me with the query I would need to write?
What you are looking for is Backbone.js
http://backbonejs.org/
What you describe is a perfect example of a collection of models, by clicking on one of them you want to fetch the full model from the server.
With Backbone.js this is easy made and whats even better, you can easy save changed date without writing complex code to do it.
var Model = new Backbone.Model.extend({
url: 'www.call-this.com/to-fetch-from-server/1'
}).
//gets the data from the api
Model.fetch()
//sends data to the same defined api via post
Model.save()
You might have great fun with Backbone ;)

How can i use REST in python django for multiple tasks

This is the first time i am using REST for any web applications.
For normal get an post and i simply call the API done in Django Rest Framework.
But i am not able to think how can i deal with situations where something more needs to be done.
Suppose I have
List of users in database and their product they have bought.
Now i have web form where if someone adds the user and then submit the button , then
I have to get the list of items bought by that user in 5 hour window
Update the row in database which says buy_succeessful to false
Then again get the list of orders from the items he has bought and then update the rows with order_successful to false
Now current in my submit actions i am doing like
call to api to add the user in override manual enrty table. This is simple post to that table
Then after getting the sucessful tehn i again call api to list of items this user has bought using Query parameters . Then i have the list
Then again i loop through the list and post to api for updating that record in datbase
and so on
I am feeling this is not right.
I have found that quite often there are some more things to do tahn just saving individual objects in database.
whats the best way to do that. DO i need to have view api for every function
Try the 3rd step of the DRF Tutorial:
http://www.django-rest-framework.org/tutorial/3-class-based-views
Here, it shows how to do a "PUT" request for updating data. And also some of the other DRF features.
Also, you can reference serializer.object which is the object instance of the django model record that you are saving to the database. This question here talks about adding extra attributes, etc... before saving to the database:
Editing django-rest-framework serializer object before save
You can also access the record post_save and there are other hooks in the framework that you can use.

Confusion about Backbone.js and Django

I'm trying to use Backbone.js for my Django project and it's confusing. So to my understanding, I need tastypie for the RESTful API with Django to which I'm new, so for example I have a SongResource like follow :
class SongResource(ModelResource):
class Meta:
queryset = Song.objects.all()
authorization = Authorization()
All what this does is gets back a list of all the songs I have in the database, right? To my understanding, I should use this in the Backbone.js router to get all the songs, and then do all the data manipulation in my JS code instead of the Django's view?
So if I want to get all the songs that the logged-in user purchased, I should get all the songs from Django, and search for the user's songs in JS code?
Also, what if I want to save songs the user listened to for example, I'm used to do that by sending an Ajax request to a view where I save the action.
Another thing is, let's say I have five models in my Django app, should I create the give models in Backbone.js too?
So in Backbone.js, I just get the data from Django and manipulate them in the front end instead of the Django views as I'm used to?
If you can see my confusion please guide me to some articles, tutorials, videos whatever !
Thanks a lot
You definitely have to do the filtering on Django side :) I know nothing about tastypie, but as of current (logged in) user, you have that in django session, therefore you cannot rely on Meta.queryset, instead the queryset changes for every request. You probably need to override some view method.
As of saving listened songs, you first decide when to do that (start or end of song), and upon that event you save() some Listening (Backbone) model, that will trigger the XHR request (see Backbone.sync).
Yes, you should Backbone model counterparts for your Django models if you use them client side. Again, see Backbone.sync

Categories

Resources