MVC query model data with jQuery - javascript

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 ;)

Related

Spring MVC Ajax Request to Refresh Dynamic Table

I currently have a spring mvc app that gets a list of users from a database and displays their information in a table using JSP to basically loop through each object in the list and create a table row for them.
Each user has an expiry date attribute as part of their record in the database. What I want to achieve is basically a button that when toggled shows or hides all users that have expired (i.e. their expiry date is less than today's date).
For this I am trying to use AJAX calls to my controller to fetch me all users expired or not OR only users that haven't expired depending on how the button is toggled.
What I would like help on is the best way to achieve this as I can think of a few nasty ways of doing it like having a separate page and refreshing but I am confused on a few things.
Should I just ditch the JSP looping through to make the table and make a method in JavaScript that creates that table when given the data? If so how do I get the data from the controller to JavaScript, can an AJAX call to a controller return me a list of my user objects?
My best guess is that instead of adding a list of objects to a model and letting JSP do the work, that I instead return a JSON with the data and use JavaScript to build the table. I can then call an update method to re-build the table.
You are correct. You have 2 options:
Have AJAX call return html (i.e. jsp) for the table and then replace
the body of the table
Use JavaScript to build the table and then
update the table with AJAX call which returns JSON.
If you want to get more sophisticated, you could use a JavaScript framework like Knockout.js which would let you mark up the table and refresh the table without too much of JavaScript writing.
Blurgh I'm not sure why this question has received so much attention, especially now in the days of angular but if you are struggling with this then I would strongly recommend the following library:
https://www.ag-grid.com/

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.

Converting a table from the JSP to Jquery data table

I am returning a JSP, which will have a table generated depending on the data attached with the model. I want to convert the table to a JQuery data table after the JSP is loaded. How can I achieve this?
In fact I think it may be more adequate do already use a JQuery Datatable in your JSP from the start and not doing any further conversions when JSP is loaded.
If you are to consider that approach, your work would be to create an integration model between your Datatables component and your controller, which I am assuming is Spring MVC since Spring is tagged in your question.
That being said, your roadmap would be something like:
Create value objects to move datatables parameters back and forth between your JSP view and your Spring Controller;
Create your controller, that are going to handle data requests. This controller must be able to convert your incoming HttpRequest parameters to your value object. If you are indeed using Spring, your Spring Controller would use a customized WebArgumentResolver which are going to read the request and return your VO. This controller must handle your request and retrieve whatever response you might have to provide. Oh, also don't forget to resolve that Resolver in your MVC configuration.
Finally, your response would be a JSON, understandable by Datatables.
This is a great tutorial, which I used the first time I needed to implement such integration. Might fill some details for you.
Best regards.

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

Passing Mustache template data between views

I'm using Mustache in combination with Jquery Mobile and I'm looking for a way to pass information from my template into different views here's an example of my code
var ppl={"ppl":[
{"title":"Man","description":"Vice President"},
{"title":"Man2","description":"Vice President"},
{"title":"Man3","description":"Vice President"}
]};
My template first outputs the all the titles inside of a link
{{ppl}}
{{title}}
{{/ppl}}
What I would like to do is when the user clicks the link they are shown the title and description of the individual person. I've been appending data attributes to the anchor tag but I'm wondering if there's an elegant way of doing this using mustache?
If I understand correctly what you are trying to do: when the user clicks on a person link, some JavaScript code (jQuery) will display a modal window that will show the title and the individual description of the person. This jQuery code has to fetch the individual description from the server, most likely using an AJAX call.
With this in mind:
Templates are not designed to contain logic or pass state. That makes the code hard to follow and maintain.
To pass state between views use:
if the views are implemented using JavaScript use JavaScript variables, parameters in function calls, events whatever.
if the views are implemented on the server side and you need to pass state across request you can use: the session if your state is simple and small, a DB column for more complicated state.
In your case, It seems to me, the server should be able to get the person's title when it fetches the individual person's description and to return one JSON object that contains both the the person's title and person's description so that the client can easily display it.
Hope this help.

Categories

Resources