Capturing/Logging image related data using Django/Python - c#

I come from a ASP.Net background and taking baby steps in open source world. I have an image based application and for every image click I want to log data on server. I'm using Django/Python to host this application.
My understanding is that I need to process this data on client side and send to server using Ajax calls. Please correct me if I'm wrong. In ASP.net world, we had "runat=server" tag for every HTML control that made logging data on server really easy. Is there something similar in Django/Python?
Also, what is the most efficient way of logging image data in this situation?
Thanks in advance. Your help is highly appreciated.
Cheers!!

Your understanding of needing to use AJAX calls is one correct answer.
You could also register onclick JavaScript events for all images and have the JavaScript call a function that submits a form with the needed values for the view to process. That's sort of how ASP.net's PostBack works. The only difference is you're writing the client-side code yourself and nothing is obfuscated with the __VIEWSTATE hidden field.
Basically, ASP.net and Django come from two very different schools of thought. ASP.net's runat=server stuff makes things accessible to the various Page Lifecycle Events (Init, PreLoad, Render, etc) using helpful nuggets from __VIEWSTATE.
Django has no such Page Lifecycle model. It keeps things much simpler: a request is directed to a view method (or class-based view) using urls.py. The view method then returns a response.
There are benefits and drawbacks to both ways of doing things.
The short answer is that different frameworks tend to solve the same problem in different ways.

Related

Creating javascript function on server side with string.format, best way and alternatives

on one of the project I was delegated to I saw some c# code like this
string.format("some JavaScript function(){{ {0}.setValue and do magic",param1,param2,...");
Then the function was set as index changed method of some JavaScript element.
It's not the first time I saw it but its the first time it struck me so hard. The actual code was enormous and there was large number of parameters passed.
I was wondering if there is a better way (Probably plenty?) cause this seemed for me like a poor one. Writing complex javascript logic can be painful and writing it using string.format is semi insane for me. Can some 1 explain to me what are the alternatives and best practices are?
Thanks for help.
Regards.
If you have not some really need of generating conditional javascript code for the client, bacause that is what the presented code is doing, I would strongly advice to keep JS on client, and deliver it to the client via different standard mechanisms available in browser.
In short
use client side JS delivered via JS files or like a text and after treated like JS, but this is often for advanced scenarious.
use any MVC framework on client side, to avoid continue callback to the server, if you don't really need it, and manage states and appearance of content on your site in easier way.
a) knockout
b) angular.js
... many others...

ASP.Net AJAX Call c# functions

Been looking around for the last few days trying to figure out what the best route is. I am fairly new to ASP.Net so I am in need of a little assistance.
I like the idea of using Master Pages as it will make making changes to the template a lot easier! But I am running into some problems. I will just list them below and see where we can go, maybe this will help some other newbies like myself.
Dynamic Menu:
I am trying to create a menu system that will show certain links depending on the users role. This is simple enough until I just want the link to perform some functions and thats it. So I dont want it to postback or anything. So my next step was to try to use jQuery as I would with my php development. Problem is I cant seem to get my jQUery to call the master page code behind function. I've gone through all the tutorials I could find with WebMethods but just keep getting an error to the like of This type of page is not served.
General Classes:
In PHP sometimes I would have the need to just have some General classes that pertained to a specific area of the application. I would just use these to hold all the function I may need to call from jQuery. Is there something like that in ASP.Net? I tried just adding a class but again couldn't call it from jQuery. Is this something Web Services would be good at? I am still trying to understand their full use. Seems like we could use Web Services as a buffer between the client and the back end classes.
I look forward to any pointers or tips!
Oded and jrummell made it very clear I should probably start with ASP.NET MVC first. It will most likely be an easier road for me moving from php.

Multiple AJAX calls - single aspx page or multiple aspx pages for better performance

I am currently rewriting a large website with the goal of replacing a large number of page/form submittals - with AJAX calls. The goal is to reduce the amount of server roundtrips - and all the state handling on pages that are rich with client .
Having spent some time considering the best way forward with regards to performance - my question is now the following.
Will it lead to better performance to have just one single aspx page that are used for all AJAX calls - or will it be better to have a aspx page for every use of AJAX on a given webage?
Thank you very much for any insights
Lars Kjeldsen
Performancewise either approach can be made to work on a similar order of magnitude.
Maintanancewise, I prefer to have separate pages for each logical part of your site. Again, either can work, but I've seen more people make a mess of things with "monolithic" type approaches. Single page you'll need a good amount of skill structuring your scripts and client side logic. Well done there isn't a problem, however, I just see more people getting it right when they use separate pages for separate parts of the site.
If you take a look at the site http://battlelog.battlefield.com/ (you'll have to create an account) you'll notice a few things about this it.
It never refreshes the page as you navigate the website. (Using JSON to transmit new data)
It updates the URL and keeps track of where you are.
You can use the updated URL and immediately navigate to that portion of the web-application. (In this case it returns the HTML page)
Here's a full write up on the website.
Personally, I like this approach from a technology/performance perspective, but I don't know what the impact it will have on SEO since this design relies on the HTML5 History state mechanism in JavaScript.
Here's an article on SEO and JavaScript, but you'll have to do more research.
NOTE: History.js provides graceful degradation for Browsers that do not support History state.

How can I emulate ajaxStart and ajaxStop without jQuery?

I've been looking at the jQuery code but is kinda huge. Would this be an easy task? Any idea how to do it?
The reason I want to do this is because I want to use it not for a web page but for a C# application that needs to know when there is ajax activity going on in a WebBrowser. So I would inject my javascript code in any web page I want to detect ajax activity, instead of injecting the whole jQuery which might be already present and cause a conflict.
jQuery's ajaxStart doesn't fire for arbitrary XMLHttpRequest events, only for those coming from within jQuery.
You can trivially test* this by spinning up your own XMLHttpRequest after registering an ajaxStart handler.
So, technically, its easy to emulate: just route all your ajax traffic through a common method.
I doubt that's going to help you much since you won't actually capture all ajax requests, just the ones you kick off (and thus already know about, by definition).
*I did, since I wasn't sure.

auto generate javascript to update client html compared to previous html?

do you think it would be difficult to write a framework where mvc compares last html it output to the current html we want to output, and instead of sending the entire html, figure out what has changed and generate js code that will do the updating as compared to previous html? (presuming nothing was manually changed on the client using js)... maybe an idea for a codeplex project? or maybe something like this exists? if so, do tell. thanks.
I think it's an interesting question, but one without a practical solution..
If I understand correctly you want to generate a diff from the current DOM into a new one, and you want to generate this change script (which is javascript executed client side) on the server.
The issue with that is that in order for the server to generate a diff, it needs to know what the previous DOM structure was in order to compare it with the new one (i.e. the new html page)
The only ways I can think of are:
1. The client sends back the full current page or some representation of it.
2. The server stores a copy of the previous page.
The problem with #1 is that you've already negated any performance benefit you from it. Sending a full page back to the server is just as bad or worse than sending it from the server to the client. You can achieve the same effect by requesting the full page body via AJAX and replacing it, and it would be just as efficient and simpler to implement.
The problem with #2 is now the server needs x copies of each page where x is the number of users. That's a lot of memory, unless you're persisting it to disk, in which case that's a a moderate sized disk write for every request. Then there's the problem of figuring out how long to keep these around, because if someone visits the site once, you don't want to keep it around forever.
The performance of either situation is most likely going to be worse than just getting the full page and will only get worse with more users.
That's not including the complexity of actually getting it right. I think hypothetically it could be done, but other than as a fun experiment there aren't any practical benefits that would outweigh the cost of such a solution, which is why I doubt you'll find one.
Have you considered caching? E.g. Caching in asp.net-mvc
Will be more straightforward and it makes more sense to me too.
You would need to save the state of any client at your server, and no response could be cached anywhere because every client needs a different response.
Even if this is possible, it would make no sense in the "HTTP world" imho.
You are attempting to suggest a solution for a problem that has already been solved. AJAX solved your question. You can use AJAX requests to load html that you know will change thus saving round trips.

Categories

Resources