I've never used AJAX and am, quite frankly, not much versed in JS either.
I have a page (Product Browser) that has the following breakdown:
- Navigation (First, Prev, 1, 2, 3 Next, Last) - full post back
- Items per Page drop down - I want to AJAX this
- Left Column - Filters - I want to AJAX this
- Product Display area - displays products based on 1, 2 & 3 above
I am starting with Item #2 and am trying to figure out how to change item 4 based off of the OnChange event for the drop down. I am at a loss as to where. or how, to start. Should it be AJAX? JQuery? A combination of both? And remember - it is in a MVC based app.
TIA
-
You can try this simple demo from my blog
-
AJAX is just a request pulling information from a page without refreshing the browser. You can use ASP.NET AJAX or JQuery's AJAX and you should get the same results.
And JQuery is just a library to select and manipulate the DOM. JQuery has a few AJAX functions you can use as well but JQuery can be used for much much more.
I don't know how ASP's version works exactly but AJAX is just a page request.
-
I would definately check out jQuery and not bother with MS's ajax implementation. For the things you want, you can check out jQuery's docs for $post for example or ajax/load functions.
You can either add events to the controls on your page, like the carpets / area rugs radio buttons, or add that event to the submit button and then make a call to the products controller to retrieve a partial view that loads into the products div.
-
The answer to your question is definitely, "yes".
I've got one in front of me right now, so I'll try to abstract it out.
First, create a controller action that returns a JsonResult (rather than ActionResult). Other than its return type it's just like any other action, so you can send parameters, etc. The only thing that's really different is that you're going to return a JsonResult() object, setting its Data property and any other properties that you may need. Mine looks something like this (very pseudo-codish...):
public JsonResult GetList(int parentId) { var results = dataRepository.GetById(parentId); return new JsonResult() { Data = results.ToArray(); }; }
Now, in your view, create a script that looks something like this. Note that this is jQuery syntax, so it may look a bit unusual if you're not familiar with it.
<script language="javascript" type="text/javascript"> // When the document is ready, start firing our AJAX $(document).ready(function() { // Bind a function to the "change" event of our drop-down list $("#dropDownId").bind("change", function(e) { updateList(); }); } var retrieveData = function(path, parentId, fnHandleCallback) { // Use the getJSON method to call our JsonResult action $.getJSON(path, { parentId: parentId }, function(data) { fnHandleCallback(data); }); }; // The path parameter is our JSON controller action function updateList() { retrieveData("/Controller/GetList", $("#dropDownId").val(), handleResponse); } function handleResponse(data) { // Ok, now we have the JSON data, we need to do something with it. I'm adding it to another dropdown. $("#otherDropDownId > option").remove(); for (d in data) { var item = data[d]; $("#otherDropDownId").append("<option value=\"" + item.Value + "\">" + item.Text + "</option>"); } } </script> <%= Html.DropDownList("dropDownId", new SelectList(new List<SelectListItem>())) %> <%= Html.DropDownList("otherDropDownId", new SelectList(new List<SelectListItem>())) %>
This is all very much off the top of my head, so let me know if something needs to be clarified or corrected.
Edit
As noted in my comment, in order to "AJAXify" your page, you don't really want to push everything around in your Model. Instead, it sounds like you want something like this:
Controller action:
public JsonResult GetPagedData(int page, int itemsPerPage, string[] filters) { var results = dataRepository.GetPagedItems(pageId, itemsPerPage, filters); return new JsonResult() { Data = results.ToArray(); }; }
JS changes:
var retrieveData = function(path, pageNumber, pageSize, filters, fnHandleCallback) { // Use the getJSON method to call our JsonResult action $.getJSON(path, { page: pageNumber, itemsPerPage: pageSize, filters: filters }, function(data) { fnHandleCallback(data); }); }; // The path parameter is our JSON controller action function updateList() { retrieveData("/Controller/GetPagedData", $("#pageNumber").val(), $("#dropDownId").val(), null, handleResponse); }
I've intentionally ignored figuring out both the page number and the filters - they would follow essentially the same principles.
Finally, when you're rendering the data you'll put it into your product grid rather than another drop-down.
How to add Stroke to Text in AS3?
I am coding in AS3 and want to add a stroke to text that I'm displaying to the screen. My current code is:
format = new TextFormat("BN Elements", 14, 0xEEEEEE, false, false, false, null, null, "left");
format.font = "BN Elements"
scoreText = initText(starsleftText, format, "", 160,5, 545, 61);
scoreText = "Stroke This Text";
As the text is dynamically generated I can't create it as a text object in the Flash IDE - where I know to add a stroke I can use the "glow" function set to 1000% and Low quality.
I suppose my question is, how can I apply the "glow" filter effect with similar properties within AS3 directly? Or is there an alternative "stroke" function I don't know about?
-
It's pretty easy:
http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/flash/filters/GlowFilter.html (There's an example at the bottom of the page)
I'm not sure if they've added anything new as far as adding a real stroke, but the glow filter 'stroke' works just as well with code.
You could also create a pixel bender filter that would give you a bit more control over the effect, I can't seem to find any already written ones out there though:(
quoo : oh also, beware, if the text is selectable, using the glow filter will add a stroke to the selection as well. -
Flash generates its strokes from the edges of shapes. Since you are using text dynamically (not in authoring) Flash can render with either 1) device text which in drawn by the operating system, or 2) embedded text in your SWF file. In the first case, Flash doesn't (currently) have access to the edges to stroke them. In the 2nd, Flash uses a specialized sub-renderer for text that (again currently) doesn't support stroking, or for that matter, anything but solid color fills.
Short answer: stroking of text currently isn't supported in the runtime, although the glow or pixel bender approach suggested is a good idea.
SQL Server- ORDER BY CASE problem
I have the following the query running dynamically
SELECT *
FROM Vehicles
WHERE (DKID IN (69954))
ORDER BY case when ImageName1 = 'na' then 0 else 1 end, Make , Model, Year DESC
This is returning the following error:
Error Executing Database Query. [Macromedia][SQLServer JDBC Driver][SQLServer]Incorrect syntax near 'na'.
Thanks in advanced for your help
-
Try enclosing the
case
statement in parentheses.SELECT * FROM Vehicles WHERE (DKID IN (69954)) ORDER BY (case when ImageName1 = 'na' then 0 else 1 end), Make , Model, Year DESC
-
works for me
here is repo script
use tempdb go create table Vehicles(DKID int,ImageName1 varchar(50), Make int, Model int, Year int) insert Vehicles values (69954,'na',1,1,2007) insert Vehicles values(69954,'bla',1,1,2008) go SELECT * FROM Vehicles WHERE (DKID IN (69954)) ORDER BY case when ImageName1 = 'na' then 0 else 1 end, Make , Model, Year DESC
-
are you running this query dynamically?, if so you might need to escape the quotes around 'na':
SELECT * FROM Vehicles WHERE (DKID IN (69954)) ORDER BY case when ImageName1 = ''na'' then 0 else 1 end, Make , Model, Year DESC
Patcouch22 : I am running it dynamically, however the escape quotes still provide the same error.KM : try replacing 'na' with char(110)+char(97), which will be slow, but will help diagnose it more...Patcouch22 : that did allow it to run...KM : that shows that you are not escaping it properly, is "na" a constant or part of the dynamic query? edit your question to show that is generating the query, the propblem is there. -
Your query works fine for me in SQL Mgmt Studio... Maybe try it this way instead to see if it gets you anywhere:
SELECT case when ImageName1 = 'na' then 0 else 1 end as OrderCol, * FROM Vehicles WHERE (DKID IN (69954)) ORDER BY OrderCol,Make,Model,Year DESC
-
You're using JDBC. Is there probably a transformation / interpretation from JDBC? Try making the 'na' a parameter. Check if there is a certain syntax in JDBC for string constants in queries. I don't use JDBC, so I could be completely wrong.
-
As KMike said, it looks like you didn't not escape properly.
Basically, when you ran your statement, it did not generate a syntactically correct SQL statement from the dynamic SQL.
Generally, when I am writing dynamic sql, I use a print statement to print the generated SQL. I can then review the generated sql visually for obvious mistakes, and then execute it it to make sure it works as expected.
If I made a mistake in the dynamic SQL, it will usually be revealed here.
How do you make your Java application memory efficient?
How do you optimize the heap size usage of an application that has a lot (millions) of long-lived objects? (big cache, loading lots of records from a db)
- Use the right data type
- Avoid java.lang.String to represent other data types
- Avoid duplicated objects
- Use enums if the values are known in advance
- Use object pools
- String.intern() (good idea?)
- Load/keep only the objects you need
I am looking for general programming or Java specific answers. No funky compiler switch.
Edit:
Optimize the memory representation of a POJO that can appear millions of times in the heap.
Use cases
- Load a huge csv file in memory (converted into POJOs)
- Use hibernate to retrieve million of records from a database
Resume of answers:
- Use flyweight pattern
- Copy on write
- Instead of loading 10M objects with 3 properties, is it more efficient to have 3 arrays (or other data structure) of size 10M? (Could be a pain to manipulate data but if you are really short on memory...)
-
You don't say what sort of objects you're looking to store, so it's a little difficult to offer detailed advice. However some (not exclusive) approaches, in no particular order, are:
- Use a flyweight pattern wherever possible.
- Caching to disc. There are numerous cache solutions for Java.
- There is some debate as to whether String.intern is a good idea. See here for a question re. String.intern(), and the amount of debate around its suitability.
- Make use of soft or weak references to store data that you can recreate/reload on demand. See here for how to use soft references with caching techniques.
Knowing more about the internals and lifetime of the objects you're storing would result in a more detailed answer.
-
I suggest you use a memory profiler, see where the memory is being consumed and optimise that. Without quantitative information you could end up changing thing which either have no effect or actually make things worse.
You could look at changing the representation of your data, esp if your objects are small. For example, you could represent a table of data as a series of columns with object arrays for each column, rather than one object per row. This can save a significant amount of overhead for each object if you don't need to represent an individual row. e.g. a table with 12 columns and 10,000,000 rows could use 12 objects (one per column) rather than 10 million (one per row)
Boune : Good trick for minimizing the number of objects.Boune : I agree that a memory profiler is a good starting point for someone who does not know which Class instances are taking all the memory. The question is more, if I know in advance I will have 10M pojo#1 in memory, how do minimize the consumption of each instance? -
Ensure good normalization of your object model, don't duplicate values.
Ahem, and, if it's only millions of objects I think I'd just go for a decent 64 bit VM and lots of ram ;)
Brian Agnew : Which is quite possibly the most cost-effective solution :-)duffymo : +1 - That's cutting to the heart of the issue.Fortyrunner : Great answer. Using caches of data and reducing duplicate records and fields is a major saver.Boune : How do you minimize the number of duplicated values? Original question mentions usage of Enum, String.intern, object pools. How would you insure that values are not duplicated?krosenvold : @Boune There may be combinations (subsets) of values that are duplicate. -
I want to add something to the point Peter alredy made(can't comment on his answer :() it's always better to use a memory profiler(check java memory profiler) than to go by intution.80% of time it's routine that we ignore has some problem in it.also collection classes are more prone to memory leaks.
-
Normal "profilers" won't help you much, because you need an overview of all your "live" objects. You need heap dump analyzer. I recommend the Eclipse Memory analyzer.
Check for duplicated objects, starting with Strings. Check whether you can apply patterns like flightweight, copyonwrite, lazy initialization (google will be your friend).
-
You could just store fewer objects in memory. :) Use a cache that spills to disk or use Terracotta to cluster your heap (which is virtual) allowing unused parts to be flushed out of memory and transparently faulted back in.
-
A fancy one: keep most data compressed in ram. Only expand the current working set. If your data has good locality that can work nicely.
Use better data structures. The standard collections in java are rather memory intensive.
[what is a better data structure]
- If you take a look at the source for the collections, you'll see that if you restrict yourself in how you access the collection, you can save space per element.
- The way the collection handle growing is no good for large collections. Too much copying. For large collections, you need some block-based algorithm, like btree.
Boune : How would you define better data structures? How would you implement that? -
Spend some time getting acquainted with and tuning the VM command line options, especially those concerning garbage collection. While this won't change the memory used by your objects, it can have a big impact on performance with memory-intensive apps on machines with a lot of RAM.
-
If you have millions of Integers and Floats etc. then see if your algorithms allow for representing the data in arrays of primitives. That means fewer references and lower CPU cost of each garbage collection.
-
Assign null
value to all thevariables
which areno longer
used. Thusmake it available for Garbage collection
.De-reference the collections
once usage is over, otherwise GC won't sweep those.
Boune : I disagree with item 1. I would just let the gc do what it is suppose to do. There are only a few cases (arrays, collections) where this could be useful, not all variables. http://stackoverflow.com/questions/449409/does-assigning-objects-to-null-in-java-impact-garbage-collection
.htaccess redirect after replace a word?
Hello, I need to use .htaccess file to replace a world in URL
something like this:
example URL: http://example.com/oldword/test-page.html
redirect to: http://example.com/newword/test-page.html
how can I use mod_rewrite to redirect every URL containt "/oldword/" to the same URL after replacing that word?
-
This should do it for you:
RewriteRule ^oldword/(.*) /newword/$1 [L]
Edit: It might not work exactly depending on your RewriteBase settings, but it'll be close.
Second Edit: If you need to have a 301 Moved Permanently header associated with the old URLs, you can do something like this as well:
RewriteRule ^oldword/(.*) /newword/$1 [R=301,L]
-
Hi,
see here:
<IfModule mod_rewrite.c> RewriteEngine on RewriteBase / RewriteRule ^oldword(.*)$ http://%{HTTP_HOST}/newword$1 [L] </IfModule>
ciao,
Chris
Gumbo : It’s better to leave the protocol, host and port part away like zombat did. Additionally your rule would also redirect `/oldword-and-some-more` to `/newword-and-some-more` and I don’t think that’s what chiaf wanted.
windows user domain account question
Hi
I have one question regarding domain account. I have one domain controller where all the user information are stored. I have joined the domain on my laptop but I observed that I am able to log-in using domain credentials even if domain controller is down. How is this possible?
Kind Regards PK
-
You're using cached credentials. You can disable this in the registry if you want.
-
If you've logged on to the laptop prior to the Domain Controller being down, your credentials are cached locally on the machine.
This occurs so that you can use your domain credentials to log into the machine even if you are disconnected from the network (working from home, etc.).
-
Windows will cache previous user login credentials locally so you can still log in if the DC is down. You shouldn't be able to access network resources that might need you to login though.
Simple inter-proccess communication in Qt4
I need to make so that my application can have only one instance running at a time. Also when it's launched with a command line parameter ( like when registered to open certain file types ) it should pass the parameter to an existing instance and quit immediately without displaying graphical interface. You all probably know what I mean. The framework used is Qt 4, and it seems like it must have some facilities for that. Any ideas?
-
There is a Qt Solutions project doing this:
-
It's also possible to implement a this sort of class oneself using QSharedMemory (see QSharedMemory::attach() ). It's capable of being used for both determining whether other instance is already running and communicating/sending messages. Except some pointer magic and memory copying it's quite straightforward.
-
There are several ways to do inter process communication. Examples can be found in Qt's examples section.
PHP simpleXML how to save the file in a formatted way?
I'm trying add some data to an existing XML file using PHP's SimpleXML. The problem is it adds all the data in a single line:
<name>blah</name><class>blah</class><area>blah</area> ...
And so on. All in a single line. How to introduce line breaks?
How do I make it like this?
<name>blah</name>
<class>blah</class>
<area>blah</area>
I am using asXML()
function.
Thanks.
-
You could use the DOMDocument class to reformat your code:
$dom = new DOMDocument('1.0'); $dom->preserveWhiteSpace = false; $dom->formatOutput = true; $dom->loadXML($simpleXml->asXML()); echo $dom->saveXML();
-
There's also the PEAR XML_Beautifier package.
-
Use
dom_import_simplexml
to convert to a DomElement. Then use its capacity to format output.$dom = dom_import_simplexml($simple_xml); $dom->preserveWhiteSpace = false; $dom->formatOutput = true; echo $dom->documentElement->saveXML($dom);
-
Gumbo's solution does the trick. You can do work with simpleXml above and then add this at the end to echo and/or save it with formatting.
Code below echos it and saves it to a file (see comments in code and remove whatever you don't want):
//Format XML to save indented tree rather than one line $dom = new DOMDocument('1.0'); $dom->preserveWhiteSpace = false; $dom->formatOutput = true; $dom->loadXML($simpleXml->asXML()); //Echo XML - remove this and following line if echo not desired echo $dom->saveXML(); //Save XML to file - remove this and following line if save not desired $dom->save('fileName.xml');
-
Cool solution Gumbo
(rails) taking from DB and rendering into HTML
Hi. I'm building a website for my Web Dev class, and I'm stuck on rendering HTML. I want to be able to use a simple form (Pretty much all I have right now is a scaffold for this controller, and I attempted sticking a content_type into my controller, but no progress.) to submit text and have it rendered as HTML. The idea is that, since this class requires a bunch of crap copied out of the book as examples and reference for HTML, maybe I could serve them up in the same way as the blog posts. (All on the same page, using the same layout. The only thing that changes is a content div below the Blog list and the Data (Controller in question) list.
So, in short, my question is: How do I get text fetched from DB to render the html tags rather than displaying as plaintext?
Thank you, and please let me know if supplementary information is necessary. Cameron
Edit: (Adding code. It's really almost nothing past scaffolding, but, whatevs.) Also, not sure how the code snippet tool is supposed to work. I hope it folds.
class DatapostsController < ApplicationController
before_filter :header
def header
response.headers['Content-type'] = 'text/html; charset=utf-8'
end
# GET /dataposts
# GET /dataposts.xml
def index
@dataposts = Datapost.all
@posts = Post.all
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => @dataposts }
end
end
# GET /dataposts/1
# GET /dataposts/1.xml
def show
@dataposts = Datapost.all
@datapost = Datapost.find(params[:id])
@posts = Post.all
respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => @datapost }
end
end
end
This is the view where it's to be rendered. It's a partial that's called from a content_for that's called by the homepage.
<p>
<small>Post title</small>
<%=h @datapost.title %>
</p>
<hr />
<p>
<%=h @datapost.body %>
</p>
<hr />
<hr />
<%= link_to 'Back', dataposts_path %>
I'll go ahead and push what I have onto prod. server for an idea of what I want the functionality to be like.
http://www.sanarothe.com (~5 minutes after edit)
-
The
h
method you're calling here:<%=h @datapost.body %>
is also known as
html_escape
- here's the relevant link on Railsbrain. Remove it and your HTML tags should render appropriately.You should always display code you get from a user with the
h
method to prevent cross-site scripting attacks. But if it's code you scraped from a book (or whatever) it should be fine.
Is a good idea to put all projects in the same trunk?
We understand the default and usually recommended svn repository organization, in case of having multiple projects, is something like this:
root/projectA/(trunk, branches, tags)
root/projectB/(trunk, branches, tags)
...
Our projects have a lot of interdependence, and that would demand an extense use of svn:externals between them, considering we don't do dll referencing to internal projects, we'd prefer to view their source code instead of working with binaries.
Using externals too much, when branching repositores, syncing changes, can become a complex and error-prone experience, so the team didn't trust this solution at all.
So a team member suggested something that we all think this could be a better solution: put all projects in the same trunk.
At first, we recognized some problems with this approach, but as a whole we agree these problems are based on hypotethical situations that very probably we'd never experience.
Do you see some serious problems we may have with this solution?
-
We do this at our company and have had a lot of success.
We have 3 top level directories:
- tags
- branches
- trunk
And then we have each project as a sub-directory of those.
We still branch at the project level though and still use svn:externals. But if we had a smaller source tree we would branch at the trunk level and not use svn:extenrals.
It's nice to be able to have all projects' trunk at the same place. You can back it up, you can check it all out and you have all the most recent stuff together. You don't lose the single location for all branches nor single location for all tags either because they are all in subdirectories of /branches/projectX and /tags/projectX
Problems with svn:externals:
If your projects are not extermely HUGE then you could just branch the whole trunk each time and avoid all of the problems with svn:externals.
The problem with svn:externals is that when you make a branch, it doesn't automatically create a branch for each of the svn:externals for you. This is a problem because then over time all of your old branches won't be able to compile as your trunk gets more updated. Another problem is that if you make a fix in any branch to an svn:external, all your other branches break.
Another problem with svn externals is that when you do an svn:log at the root level, you don't see any changes from svn externals.
Hopefully one day svn externals will be fixed to address the above problems, but until that day branching and svn:externals is an absolute nightmare.
Nick Haddad : I agree with this. Having the projects in separate repositories makes it harder to share code, and merge changes between products if needed. Working in separate project branches is cleaner because you can work independently but still push changes down the trunk.leander : Agreed; we have a separate repo for each project and it's causing problems. We've experimented with multiple-projects-per-repo and it has worked better; the primary thing preventing us from migrating to this permanently is permissions. (commit-access-control.pl isn't very configurable, while you can control separate repositories using an LDAP module with apache or the like. We can also selectively only expose certain repositories for offsite access. There's probably a newer/better way to do all of this, but for the time being, that's why we're using separate repos.)Brian R. Bondy : Ya I use apache and configure it that way, there's a thread on SO about it http://stackoverflow.com/questions/484499/how-do-i-restrict-apache-svn-access-to-specific-users-ldap-file-based-authentica/484721#484721Bert Huijben : You don't have to put projects in their own repository to create their own versions of trunk and branches. E.g. The Apache foundation has all their projects in a single repository, but each has their own trunk and branches. -
What you've done is what I've set up at my company, and it is also referenced as a "very common layout" in the svnbook topic, Strategies for Repository Deployment.
Nothing wrong with it.
-
Hi,
to "putting all projects into same trunk":
From my experience this is no good idea - because each project has it's own history and changes and often projects are/will be maintained by different developers. This can lead to conflicts - even if you state out, that currently the projects interfere heavily.
So why don't you use common tags (with milestones as names to) for all of your projects to ensure a same code base and a build script, which can check out the projects (trunks) automagically? It's more work, but like usual in OOP (capsulation) I would prefer to split the projects into separate SVN directories, too.
But: Collecting a bunch of small libs and apps into a common directory under the same trunk is no problem (see "apps" and "tools" in my example below) - so maybe "small projects" can stay in the shared/big trunk.
Here as example my directory structure of SVN:
/projects/ /projects/CustomerA/ /projects/CustomerA/ProjectX/ /projects/CustomerA/ProjectX/trunk/ /projects/CustomerA/ProjectX/tags/ /projects/CustomerA/ProjectX/branches/ /thirdparty/ /thirdparty/ExtLibY/ /thirdparty/ExtLibZ/ /tools/ /tools/trunk/ /tools/tags/ /tools/branches/ /apps/ /apps/trunk/ /apps/tags/ /apps/branches/
So all of the external stuff is stored in /thirdparty/ and all internals (projects, tools, apps) have the subdirs:
/trunk/ /tags/ /branches/
...like advised in the Subversion book and the previous post.
Even if that looks a little bit much efforts at first sight - it is really worth it, especially when your code base grows.
ciao, Chris
Bert Huijben : We use about the same layout.. We use projects (internal), products (delivered) and libraries (reused components) as top level names. -
Do you see some serious problems we may have with this solution?
Your solution only works as long as you can put everything in a single repository.
This means the entire repository has to fit on a single disk (or storage pool) for the foreseeable future. Similar considerations apply for other server resources like network and disk I/O bandwidth.
Splitting the repository later will require a big overhaul of your whole set-up, and might cause headaches when you need to rebuild or branch old versions.
Your solution only works if you don't need to limit read/write permissions on a per-user basis
If you need to give read/write permission for projectA, you'll actually have to give permission for /trunk/projectA, and /branch1/projectA, and /branch2/projectA etc.
Branching then becomes a heavy-weight process that requires lots of permission tweaking. Say goodbye to feature branches.
How to increase the page height by X pixels by using only javascript
Assume an HTML page:
<html>
<body>
.. html content (outside of our control) ..
.. javascript block ..
.. some more html content (outside of our control) ..
</body>
</html>
Assume further that the only part of the HTML page that we're able to control is a javascript block in the middle of the page.
Using that javascript block, how do I increase the total page height by 150 pixels by "padding" the bottom of the page?
If I could alter the HTML a simple solution would be:
<html>
<body>
.. html content (outside of our control) ..
.. javascript block ..
.. some more html content (outside of our control) ..
<div style="height: 150px; clear: both;"></div>
</body>
</html>
But how do I achieve this only by using the javascript block?
The ideal solution should be cross-browser compatible and 100 % inline (that is using no javascript library).
Update #1: The javascript block has access to the entire DOM of the page.
Update #2: The total length of the page (before altering the DOM) varies.
Update #3: Assume that no javascript library (such as the otherwise excellent jQuery) can be used.
-
Can you generate DOM elements via javascript and simply add that div to the bottom?
I'd vote for cobbal If I had any rep.. heh.
-
Updated code:
var space = document.createElement('div'); space.style.height = "150px"; space.style.clear = "both"; document.getElementsByTagName("body")[0].appendChild(space);
knorv : Thanks - I've clarified the question. Please see update #2.zombat : This should work no matter what the length of the page is. I'm out of votes, or I'd vote it up. I was about to post basically the same thing.cobbal : the original code only produced an absolute height, this is the updated version -
$('body').append($('<div style="height: 150px; clear: both;"></div>'));
maybe
Multiple signin pages in one asp.net application
- I have one asp.net web application.
- It is using two membership provider.
- Two sign-in pages one for each provider.
- Now i have two folders in root Folder1 & Folder2
- Folder1 uses 1st membership provider
- Folder2 uses 2nd membership provider
I got almost everything working including signin, create user etc in both provider. Only issue is in Form authentication i can define only one loginpath. So when session expires or need login to access secure pages. it can only redirct to one sign in page.
Also that section can't be defined by location. by application only.
How can i get folder2 to use 2nd sign in page?
- if there is anything i can define by location?
-
You need to use the
<location>
element in your web.config. You can use the<location>
tag to apply authorization settings to an individual file or directory.<location path="/root"> <system.web> <authentication mode="Forms" > <forms name="LoginForm" defaultUrl="default.aspx" loginUrl="/root/login.aspx" protection="Encryption" timeout="30" path="/"/> </authentication> <authorization> <allow users="?" /> </authorization> </system.web> </location> <location path="/root/admin"> <system.web> <authentication mode="Forms" > <forms name="formName" defaultUrl="login.aspx" loginUrl="/root/admin/login.aspx" protection="Encryption" timeout="30" path="/"/> </authentication> <authorization> <allow users="?" /> </authorization> </system.web> </location>
For centralized administration, settings can be applied in the Machine.config file. The settings in the Machine.config file define machine-wide policy and can also be used to apply application-specific configuration using
<location>
elements. Developers can provide application-configuration files to override aspects of machine policy. For ASP.NET Web applications, a Web.config file is located in the application's virtual root directory and optionally in subdirectories beneath the virtual root.If you would like 1 login location and different access levels you might want to use roles.
<location path="/root"> <system.web> <authorization> <allow roles="admin,root" />/*admin, root is allowed */ <deny users="*" /> </authorization> <system.web> </location> <location path="/root/admin"> <system.web> <authorization> <allow roles="admin" />/*admin is allowed */ <deny users="*" /> </authorization> <system.web> </location>
Users can belong to more than one role. For example, if your site is a discussion forum, some users might be in the role of both Members and Moderators. You might define each role to have different privileges on the site, and a user who is in both roles would then have both sets of privileges.
You can access all these element at the code level if you would like to manipulate the roles/authentication programmatically
Page.User.Identity.Name Page.User.Identity.IsAuthenticated Page.User.Identity.AuthenticationType Page.User.IsInRole("string");
Additional Links
Using 2 Membership Providers in asp.net
The ASP.NET web.config File Demystified
mamu : Already tried this authorization is allowmachinetoapplication so can't be set for location -
It appears from various people researching, that you cannot tell FormsAuthentication to have two different Login pages. But there is nothing preventing you from creating some base page class or other code in your two folders that can determine which login page to direct to. Or, I think that the Application_BeginRequest event fires before the FormsAuthentication module fires, so you could examine requests before they get redirected by FormsAuthentication. Either way though, you would be forced to allow anonymous users to Folder1 and Folder2, which is not ideal.
mamu : That appears to be only way doing it.
Python html output (first attempt), several questions (code included)
While I have been playing with python for a few months now (just a hobbyist), I know very little about web programming (a little html, zero javascript, etc). That said, I have a current project that is making me look at web programming for the first time. This led me to ask:
http://stackoverflow.com/questions/731470/whats-easiest-way-to-get-python-script-output-on-the-web
Thx to the answers, I made some progress. For now, I'm just using python and html. I can't post my project code, so I wrote a small example using twitter search (pls see below).
My questions are:
(1) am I doing anything terribly stupid? I feel like WebOutput() is clear but inefficient. If I used javascript, I'm assuming I could write a html template file and then just update the data. yes? better way to do this?
(2) at what point would a framework be appropriate for an app like this? overkill?
Sorry for the basic questions - but I don't want to spend too much time going down the wrong path.
import simplejson, urllib, time
#query, results per page
query = "swineflu"
rpp = 25
jsonURL = "http://search.twitter.com/search.json?q=" + query + "&rpp=" + str(rpp)
#currently storing all search results, really only need most recent but want the data avail for other stuff
data = []
#iterate over search results
def SearchResults():
jsonResults = simplejson.load(urllib.urlopen(jsonURL))
for tweet in jsonResults["results"]:
try:
#terminal output
feed = tweet["from_user"] + " | " + tweet["text"]
print feed
data.append(feed)
except:
print "exception??"
# writes latest tweets to file/web
def WebOutput():
f = open("outw.html", "w")
f.write("<html>\n")
f.write("<title>python newb's twitter search</title>\n")
f.write("<head><meta http-equiv='refresh' content='60'></head>\n")
f.write("<body>\n")
f.write("<h1 style='font-size:150%'>Python Newb's Twitter Search</h1>")
f.write("<h2 style='font-size:125%'>Searching Twitter for: " + query + "</h2>\n")
f.write("<h2 style='font-size:125%'>" + time.ctime() + " (updates every 60 seconds)</h2>\n")
for i in range(1,rpp):
try:
f.write("<p style='font-size:90%'>" + data[-i] + "</p>\n")
except:
continue
f.write("</body>\n")
f.write("</html>\n")
f.close()
while True:
print ""
print "\nSearching Twitter for: " + query + " | current date/time is: " + time.ctime()
print ""
SearchResults()
WebOutput()
time.sleep(60)
-
The issue that you will run into is that you will need to change the python whenever you want to change the html. For this case, that might be fine, but it can lead to trouble. I think using something with a template system makes a lot of sense. I would suggest looking at Django. The tutorial is very good.
-
I'd suggest using a template to generate the output, you can start with the buildin string.Template or try something fancier, for example Mako (or Cheetah, Genshi, Jinja, Kid, etc).
Python has many nice web frameworks, the smallest of them would be web.py or werkzeug
If you want a fullblown framework, look at Pylons or Django but these are really overkill for a project like that.
-
It would not be overkill to use a framework for something like this; python frameworks tend to be very light and easy to work with, and would make it much easier for you to add features to your tiny site. But neither is it required; I'll assume you're doing this for learning purposes and talk about how I would change the code.
You're doing templating without a template engine in your WebOutput function; there are all kinds of neat template languages for python, my favorite of which is mako. If the code in that function ever gets hairier than it is currently, I would break it out into a template; I'll show you what that would look like in a moment. But first, I'd use multiline strings to replace all those f.write's, and string substitution instead of adding strings:
f.write("""<html> <title>python newb's twitter search</title> <head><meta http-equiv='refresh' content='60'></head> <body> <h1 style='font-size:150%'>Python Newb's Twitter Search</h1> <h2 style='font-size:125%'>Searching Twitter for: %s</h2> <h2 style='font-size:125%'>%s (updates every 60 seconds)</h2>""" % (query, time.ctime())) for datum in reversed(data): f.write("<p style='font-size:90%'>%s</p>" % (datum)) f.write("</body></html>")
Also note that I simplified your for loop a bit; I'll explain further if what I put doesn't make sense.
If you were to convert your WebOutput function to Mako, you would first import mako at the top of your file with:
import mako
Then you would replace the whole body of WebOutput() with:
f = file("outw.html", "w") data = reversed(data) t = Template(filename='/path/to/mytmpl.txt').render({"query":query, "time":time.ctime(), "data":data}) f.write(t)
Finally, you would make a file /path/to/mytmpl.txt that looks like this:
<html> <title>python newb's twitter search</title> <head><meta http-equiv='refresh' content='60'></head> <body> <h1 style='font-size:150%'>Python Newb's Twitter Search</h1> <h2 style='font-size:125%'>Searching Twitter for: ${query}</h2> <h2 style='font-size:125%'>${time} (updates every 60 seconds)</h2> % for datum in data: <p style'font-size:90%'>${datum}</p> % endfor </body> </html>
And you can see that the nice thing you've accomplished is separating the output (or "view layer" in web terms) from the code that grabs and formats the data (the "model layer" and "controller layer"). This will make it much easier for you to change the output of your script in the future.
(Note: I didn't test the code I've presented here; apologies if it isn't quite right. It should basically work though)
dbr : I think reversed(data[-rpp:]) can be simplified to reversed(data) (rpp is how many items the API returns)llimllib : good point.... I copied that bit of his range() without thinking. Fixed. -
String formatting can make things a lot neater, and less error-prone.
Simple example,
%s
is replaced bya title
:my_html = "<html><body><h1>%s</h1></body></html>" % ("a title")
Or multiple times (title is the same, and now "my content" is displayed where the second
%s
is:my_html = "<html><body><h1>%s</h1>%s</body></html>" % ("a title", "my content")
You can also use named keys when doing
%s
, like%(thekey)s
, which means you don't have to keep track of which order the%s
are in. Instead of a list, you use a dictionary, which maps the key to a value:my_html = "<html><body><h1>%(title)s</h1>%(content)s</body></html>" % { "title": "a title", "content":"my content" }
The biggest issue with your script is, you are using a global variable (
data
). A much better way would be:- call search_results, with an argument of "swineflu"
- search_results returns a list of results, store the result in a variable
- call WebOutput, with the search results variable as the argument
- WebOutput returns a string, containing your HTML
- write the returned HTML to your file
WebOutput would return the HTML (as a string), and write it to a file. Something like:
results = SearchResults("swineflu", 25) html = WebOutput(results) f = open("outw.html", "w") f.write(html) f.close()
Finally, the twitterd module is only required if you are accessing data that requires a login. The public timeline is, well, public, and can be accessed without any authentication, so you can remove the twitterd import, and the
api =
line. If you did want to use twitterd, you would have to do something with theapi
variable, for example:api = twitterd.Api(username='username', password='password') statuses = api.GetPublicTimeline()
So, the way I might have written the script is:
import time import urllib import simplejson def search_results(query, rpp = 25): # 25 is default value for rpp url = "http://search.twitter.com/search.json?q=%s&%s" % (query, rpp) jsonResults = simplejson.load(urllib.urlopen(url)) data = [] # setup empty list, within function scope for tweet in jsonResults["results"]: # Unicode! # And tweet is a dict, so we can use the string-formmating key thing data.append(u"%(from_user)s | %(text)s" % tweet) return data # instead of modifying the global data! def web_output(data, query): results_html = "" # loop over each index of data, storing the item in "result" for result in data: # append to string results_html += " <p style='font-size:90%%'>%s</p>\n" % (result) html = """<html> <head> <meta http-equiv='refresh' content='60'> <title>python newb's twitter search</title> </head> <body> <h1 style='font-size:150%%'>Python Newb's Twitter Search</h1> <h2 style='font-size:125%%'>Searching Twitter for: %(query)s</h2> <h2 style='font-size:125%%'> %(ctime)s (updates every 60 seconds)</h2> %(results_html)s </body> </html> """ % { 'query': query, 'ctime': time.ctime(), 'results_html': results_html } return html def main(): query_string = "swineflu" results = search_results(query_string) # second value defaults to 25 html = web_output(results, query_string) # Moved the file writing stuff to main, so WebOutput is reusable f = open("outw.html", "w") f.write(html) f.close() # Once the file is written, display the output to the terminal: for formatted_tweet in results: # the .encode() turns the unicode string into an ASCII one, ignoring # characters it cannot display correctly print formatted_tweet.encode('ascii', 'ignore') if __name__ == '__main__': main() # Common Python idiom, only runs main if directly run (not imported). # Then means you can do.. # import myscript # myscript.search_results("#python") # without your "main" function being run
(2) at what point would a framework be appropriate for an app like this? overkill?
I would say always use a web-framework (with a few exceptions)
Now, that might seem strange, given all time I just spent explaining fixes to your script.. but, with the above modifications to your script, it's incredibly easy to do, since everything has been nicely function'ified!
Using CherryPy, which is a really simple HTTP framework for Python, you can easily send data to the browser, rather than constantly writing a file.
This assumes the above script is saved as
twitter_searcher.py
.Note I've never used CherryPy before, this is just the HelloWorld example on the CherryPy homepage, with a few lines copied from the above script's main() function!
import cherrypy # import the twitter_searcher.py script import twitter_searcher # you can now call the the functions in that script, for example: # twitter_searcher.search_results("something") class TwitterSearcher(object): def index(self): query_string = "swineflu" results = twitter_searcher.search_results(query_string) # second value defaults to 25 html = twitter_searcher.web_output(results, query_string) return html index.exposed = True cherrypy.quickstart(TwitterSearcher())
Save and run that script, then browse to
http://0.0.0.0:8080/
and it'll show your page!The problem with this, on every page load it will query the Twitter API. This will not be a problem if it's just you using it, but with hundreds (or even tens) of people looking at the page, it would start to slow down (and you could get rate-limited/blocked by the twitter API, eventually)
The solution is basically back to the start.. You would write (cache) the search result to disc, re-searching twitter if the data is more than ~60 seconds old. You could also look into CherryPy's caching options.. but this answer is getting rather absurdly long..
Generating data structures by parsing plain text files
I wrote a file parser for a game I'm writing to make it easy for myself to change various aspects of the game (things like the character/stage/collision data). For example, I might have a character class like this:
class Character
{
public:
int x, y; // Character's location
Character* teammate;
}
I set up my parser to read in from a file the data structure with syntax similar to C++
Character Sidekick
{
X = 12
Y = 0
}
Character AwesomeDude
{
X = 10
Y = 50
Teammate = Sidekick
}
This will create two data structures and put them in a map<std::string, Character*>
, where the key string is whatever name I gave it (in this case Sidekick and AwesomeDude). When my parser sees a pointer to a class, like the teammate pointer, it's smart enough to look up in the map to fetch the pointer to that data structure. The problem is that I can't declare Sidekick's teammate to be AwesomeDude because it hasn't been placed into the Character map yet.
I'm trying to find the best way to solve this so that I can have my data structures reference objects that haven't yet been added to the map. The two easiest solutions that I can think of are (a) add the ability to forward declare data structures or (b) have the parser read through the file twice, once to populate the map with pointers to empty data structures and a second time to go through and fill them in.
The problem with (a) is that I also can decide which constructor to call on a class, and if I forward declare something I'd have to have the constructor be apart from the rest of the data, which could be confusing. The problem with (b) is that I might want to declare Sidekick and AwesomeDude in their own files. I'd have to make my parser be able to take a list of files to read rather than just one at a time (this isn't so bad I guess, although sometimes I might want to get a list of files to read from a file). (b) also has the drawback of not being able to use data structures declared later in the constructor itself, but I don't think that's a huge deal.
Which way sounds like a better approach? Is there a third option I haven't thought of? It seems like there ought to be some clever solution to this with pointer references or binding or something... :-/ I suppose this is somewhat subjective based on what features I want to give myself, but any input is welcome.
-
When you encounter the reference the first time, simply store it as a reference. Then, you can put the character, or the reference, or whatever on a list of "references that need to be resolved later".
When the file is done, run through those that have references and resolve them.
Denis Troller : Typical two-phase parsing, akin to a compiler/linker system. Good, standard way to go. -
Will said exactly what I was about to write. Just keep a list or something with the unsolved references.
And don't forget to throw an error if there are unsolved references once you finish reading the file =P
-
Personally, I'd go with b). Splitting your code into Parser and Validator classes, both operating on the same data structure. The Parser will read and parse a file, filling the data structure and storing any object references as their textual names, leaving the real pointer null in your structure for now.
When you are finished loading the files, use the Validator class to validate and resolve any references, filling in the "real" pointers. You will want to consider how to structure your data to make these lookups nice and fast.
-
Well, you asked for a third option. You don't have to use XML, but if you follow the following structure, it would be very simple to use a SAX parser to build your data structure.
At any rate, instead of referencing a teammate, each character references a team (Blue team in this case). This will decouple the circular reference issue. Just make sure you list the teams before the characters.
<team>Blue</team> <character> <name>Sidekick</name> <X>12</X> <Y>0</Y> <teamref>Blue</teamref> </character> <character> <name>Sidekick</name> <X>10</X> <Y>50</Y> <teamref>Blue</teamref> </character>
Alex : This would certainly work for the example I've given, but the example was an oversimplification of what I really want to do, and there are some cases where decoupling two objects isn't really feasible. Or, at least, it would make it harder for the person making the file to be parsed. -
Instead of storing Character object in your map, store a proxy for Character. The proxy will than contain a pointer to the actual Character object when the object is loaded. The type of Character::teammate will be changed to this proxy type. When you read in a reference that is not already in your map, you create a proxy and use the proxy. When you load an character which you already have an empty proxy in the map, populate it with your newly loaded character. You may also want to add a counter to keep track of how many empty proxy you have in the map so you know when all referenced characters have been loaded.
Another layer of indirection....it always make programming easier and slower.
-
One option would be to reverse the obligation. The Map is responsible for filling in the reference
template<T> class SymbolMap // I never could rememeber C++ template syntax { ... /// fill in target with thing name /// if no name yet, add it to the list of thing that will be name void Set(T& target, std::string name); /// define name as target /// go back and fill in anything that needs to be name void Define(T target, std::string name); /// make sure everything is resolved ~SymbolMap() }
that won't interact well with value/moving semantics but I suspect that not much will.
<% foreach(RoomsAlive.Models.ProductDetail myRecord in Model.ProductDetail) { %>-
">
<%= myRecord.ProductSeriesName %> - <%= myRecord.ProductName %>
MSRP: $<%= myRecord.SKU_BasePrice %>
[Label] in <%= myRecord.ProductTypeName %>
Product Description: <%= myRecord.ProductDescription %>
Product Series Description: <%= myRecord.ProductSeriesDescription %>