Tuesday, May 22, 2012

run javascript code on AJAX request

i have a upload dialog that allow me to upload files with plupload, but i have a problem,
when i load content directly, it work, but when i load it with an ajax request, javascript code will execute, but dom elements not ready yet, when i make a small delay with timeout, it worked, how i can run javascript code , after ajax dom ready?



blew code load with ajax request:



    <form enctype="multipart/form-data" method="POST" action="admin.php?do=media&action=file-upload">

<div id="uploadForm">

<div id="fileList">



</div>

<a href="#" id="selectFile">[Select File]</a>
<a href="#" id="uploadFile">[Start Upload]</a>

</div>

</form>

<script type="text/javascript">
// $(function(){}); not working when data loaded with ajax request
var ajaxUpload = new plupload.Uploader({

runtimes: 'gears, html5, flash',
url: 'upload.php',
browse_button: 'selectFile',

});


ajaxUpload.bind('Init',function(param){
console.log(param);
console.log($('selectFile'));
});

$('#uploadFile').click(function(){
alert('Something');
});

ajaxUpload.init();


</script>




Get Value of Propperty ( List<long>) in Post Action at ASP.NET MVC3

This is My model:



    public class MyModel {
public List<long> NeededIds { get; set; }
public string Name { get; set; }
}


My Controllers:



public ActionResult Create() {

MyModel model = new MyModel();
model.NeededIds = new List<long> { 1, 2, 3, 4 };
return View(model);
}

[HttpPost]
public ActionResult Create(MyModel model) {

string name = model.Name;
List<long> ids = model.NeededIds;


return RedirectToAction("Index");
}


And View:



@model TestMVC.Models.MyModel

@using(Html.BeginForm()) {

<table>
<thead>
<tr>
<th>
Id
</th>
</tr>
</thead>
<tbody>
@foreach(long id in Model.NeededIds) {
<tr>
<td>
@id
</td>
</tr>
}
</tbody>
</table>

@Html.ValidationSummary(true)
<fieldset>
<legend>MyModel</legend>
<div class="editor-label">
@Html.LabelFor(model => model.Name)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Name)
@Html.ValidationMessageFor(model => model.Name)
</div>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
}


I set NeededIds in Get action and in view I can see NeededIds also I need it in Post action but in post action the NeededIds is always null, how can I get the property value in post action when i set it in get action? what is your suggestion?





Test notification bar in Android

I am writing testing framework to test an application.This application receives IM(audio/video) and then notifies the user through notification bar.



I have to automate the part that if a notification is received , I click on that notification item , is there any way to do it through testing framework in android.





LINQ2SQL Stack overflow

I am getting a stack overflow with the following code. I know what the problem is, that it executes all "GetAllPages" in the



           Children = new LazyList<Page>(from p in GetAllPages(language)
where p.ParentPage == s.Id
select p)


before it adds the p.ParentPage == s.Id



private IQueryable<Page> GetAllPages(string language)
{
return from s in context.Pages
where (from c in GetAllContent()
where c.PageId == s.Id &&
c.Language.ToLower() == language.ToLower()
select c).Any()

let contents = (from c in GetAllContent()
where c.PageId == s.Id
select c)
select new Page()
{
Id = s.Id,
SiteId = s.SiteId,
Type = s.Type,
Template = s.Template,
ParentPage = s.ParentPage,
Visible = s.Visible,
Order = s.Order,

Contents = contents.ToList(),
Children = new LazyList<Page>(from p in GetAllPages(language)
where p.ParentPage == s.Id
select p)
};
}


How can i do this, correctly?



UPDATE:
The reason behind the code is that, i have a tree structured menu, where one menu item can have 0 to many child items.
The language part can be skipped, but my site support multiple languages, and with the language parameters do i only want menu items there have a content of the given language.





Disable @Webservice loading during start up

How to disable @Webservice loading during start up to save some loading time in xfire?



I have a bunch of services with @Webservice annotation. They are all being loaded during the startup and causing a slow startup. I don't want to load these if I test non-services in dev instance. I am wondering if there is a way to disable this by setting system property or something .





How can we count the number of check boxes checked inside a grid view in asp.net using javascript?

I asked this question in an interview. In asp.net how can we check the no. of checked boxes using javascript.





C# Regex - Split and Keep Splitter

Related:
http://stackoverflow.com/a/2910549/194031



I have a string like:



"abc defgh <!inc(C:\my files\abc.txt)!>kdi kdkd<!inc(C:\my files\abc.txt)!>"


and I want to get:



["abc defgh ", "C:\my files\abc.txt", "kdi kdkd", "C:\my files\abc.txt"]


Also, I don't want



"abc <!inc(C:\my files\abc.txt adf" (missing end bracket) 


to get split.



Based on the related question and other similar answers, I need to use look aheads, but I can't figure out how to use them while accomplishing removing the tags and not splitting if part of the tags are missing.





What is the difference between access specifiers and access modifiers?

In Java, are access specifiers and access modifiers the same thing?





MS Build copy a list of directories stored in Item

I have a text file which contains some locations of the files which I want to copy to a temp directory



---- List.txt ----
Build\Java
Build\Classes


Now, I am fetching this list into an Item



<ReadLinesFromFile File="List.txt" >
<Output TaskParameter="Lines"
ItemName="DirectoryList" />
</ReadLinesFromFile>


Now, In order to append the full path, and add some excludes, I am again storing it into another ItemGroup:



<ItemGroup>
<PackageList Include="$(BuildPath)\%(DirectoryList.Identity)\**\*.*"
Exclude="$(BuildPath)\%(DirectoryList.Identity)\**\*.pdb" />
</ItemGroup>

<Copy SourceFiles="%(PackageList.Identity)"
DestinationFolder="$(PackageTemp)\%(RecursiveDir)" />


ISSUE:



Actual Dir -



C:\Work\Build\Java\Debug
C:\Work\Build\Java\Release
C:\Work\Build\Classes\*.class


Content in O/p



C:\temp\Debug
C:\temp\Release
C:\temp\*.class


How to make it copy the corresponding "Java" and "Classes" folder also?





FileInputStream using package path

I am trying to read a file like :



FileInputStream fileInputStream = new FileInputStream("/com/test/Test.xml");


I am always getting the file not found exception. How can i make it work? Will the inputstream takes the relative path or not?





How to reset auto increment column in mysql table

Here item_category_id is auto increment value in the mysql table. Now i need to delete all the values & next insertion should start from 1. Now if i delete all values & try to insert a new row then it starts with some 30's & 40's.



  item_category_id  item_category_name
1 qqq
25 ccc
32 vvv
29 bb
4 bbb
31 hhh
34 mmm
33 rrr




Change the author name of power point file

Can I change the author of a power point file using PHP or any other language ?





Redirecting to previous URL after login - Rails

For the app that I am writing, I am using simple hand made authentication (as described on Railscast.com). Using some code from Ryan Bates' NiftyGenerators gem, I have an authentication model that has some useful methods for authentication. This module is included into application_controller.rb.



One of the methods that I want to use is called redirect_to_target_or_default. I know this is what I need to redirect a user to the page that they were on once they have authenticated but I don't know where I should call this method? If someone could give me an idea on how to use this method, I would greatly appreciate it.



ControllerAuthenticaion Module Code



enter image description here





Method not found using DigestUtils in Android

I am trying to use the library DigestUtils in Android 2.3.1 using JDK 1.6, however I get the following error when executing the app:



Could not find method org.apache.commons.codec.binary.Hex.encodeHexString, referenced from method org.apache.commons.codec.digest.DigestUtils.shaHex



Here you have the stacktrace:



02-03 10:25:45.153: I/dalvikvm(1230): Could not find method org.apache.commons.codec.binary.Hex.encodeHexString, referenced from method org.apache.commons.codec.digest.DigestUtils.shaHex
02-03 10:25:45.153: W/dalvikvm(1230): VFY: unable to resolve static method 329: Lorg/apache/commons/codec/binary/Hex;.encodeHexString ([B)Ljava/lang/String;
02-03 10:25:45.153: D/dalvikvm(1230): VFY: replacing opcode 0x71 at 0x0004
02-03 10:25:45.153: D/dalvikvm(1230): VFY: dead code 0x0007-0008 in Lorg/apache/commons/codec/digest/DigestUtils;.shaHex ([B)Ljava/lang/String;
02-03 10:25:45.163: D/AndroidRuntime(1230): Shutting down VM
02-03 10:25:45.163: W/dalvikvm(1230): threadid=1: thread exiting with uncaught exception (group=0x40015560)
02-03 10:25:45.173: E/AndroidRuntime(1230): FATAL EXCEPTION: main
02-03 10:25:45.173: E/AndroidRuntime(1230): java.lang.NoSuchMethodError: org.apache.commons.codec.binary.Hex.encodeHexString
02-03 10:25:45.173: E/AndroidRuntime(1230): at org.apache.commons.codec.digest.DigestUtils.md5Hex(DigestUtils.java:226)
02-03 10:25:45.173: E/AndroidRuntime(1230): at com.caumons.trainingdininghall.ConnectionProfileActivity.onCreate(ConnectionProfileActivity.java:20)
02-03 10:25:45.173: E/AndroidRuntime(1230): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
02-03 10:25:45.173: E/AndroidRuntime(1230): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1586)
02-03 10:25:45.173: E/AndroidRuntime(1230): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1638)
02-03 10:25:45.173: E/AndroidRuntime(1230): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
02-03 10:25:45.173: E/AndroidRuntime(1230): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:928)
02-03 10:25:45.173: E/AndroidRuntime(1230): at android.os.Handler.dispatchMessage(Handler.java:99)
02-03 10:25:45.173: E/AndroidRuntime(1230): at android.os.Looper.loop(Looper.java:123)
02-03 10:25:45.173: E/AndroidRuntime(1230): at android.app.ActivityThread.main(ActivityThread.java:3647)
02-03 10:25:45.173: E/AndroidRuntime(1230): at java.lang.reflect.Method.invokeNative(Native Method)
02-03 10:25:45.173: E/AndroidRuntime(1230): at java.lang.reflect.Method.invoke(Method.java:507)
02-03 10:25:45.173: E/AndroidRuntime(1230): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
02-03 10:25:45.173: E/AndroidRuntime(1230): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
02-03 10:25:45.173: E/AndroidRuntime(1230): at dalvik.system.NativeStart.main(Native Method)


The line of code which causes the exception is:



String hash = DigestUtils.shaHex("textToHash");



I have executed the same code in a Java class outside Android and it works! So, I do not know why when working with Android it does not work... I put the libraty inside a new libs/ folder in my app and updated the BuildPath to use it. If I try to use md5 instead of sha1 I get the same exception. Any help would be appreciated! Thank you.





Create long types in JPA entities

I'm trying to generate JPA entities from tables using eclipse plugins, I defined some BIG INT and Date columns. I would like to have long properties in Entity class for those BIGINT columns, But It generates as String. Please help me how to resolve it?





OpenCV frame capture from AVI

I am working on a project with openCV 2.2. I need to do processing on each frame of an AVI file but when I run my code it only grabs the first frame of the file. The CV_CAP_PROP_POS_FRAMES does not seem to be working. Any ideas why not?



    CvCapture* capture = cvCaptureFromAVI("test1.avi");

IplImage *img = 0;

if (!cvGrabFrame(capture)) {
printf("Error: Couldn't open the image file.\n");
return 1;
}

int numFrames = (int) cvGetCaptureProperty(capture, CV_CAP_PROP_FRAME_COUNT);
int posFrame = 1;
for(int i =0; i <= numFrames; i++){
cvSetCaptureProperty(capture, CV_CAP_PROP_POS_FRAMES, i);
posFrame = cvGetCaptureProperty(capture, CV_CAP_PROP_POS_FRAMES);

img = cvGrabFrame(capture);
cvNamedWindow("Image:", CV_WINDOW_AUTOSIZE);
cvShowImage("Image:", img);
printf("%i\n",posFrame);

cvWaitKey(0);

cvDestroyWindow("Image:");
}




Inspecting a .exe to understand with what version of Visual C++ it was built

Is there a way to check what version of Visual C++ was used to build a given .exe?



I know that if the .exe uses dynamic link with CRT that is easy: I can just use Dependency Walker and read the MSVCRxx.DLL version, e.g. a dependency on MSVCR90.DLL means that the .exe is built with Visual C++ 2008 i.e. VC9; but what about the case of static linking with CRT?





How to decide between abstract and interface?

What is difference between following interface and abstract class:



 public interface MyInterface
{
public int get1();
public int get2();
public int get3();
}

public abstract class MyAbstract
{
public abstract int get1();
public abstract int get2();
public abstract int get3();
}


Interviewer was not convinced with following answers, he wanted to hear something else:




  1. I have to extend MyAbstract and then I cannot have more extends, whereas in case of implementing MyInterface I am open to have inheritance.


  2. I have to provide implementation of all three methods if used "implements MyInterface", whereas in case of "extends MyAbstract" I am open to carry forward abstractness.


  3. Design perspective: All libraries work on interfaces not on abstract classes, it is good design practice to use interfaces so that at any time in future I can create any class (implements MyInterface) that can be used in some method of library. (basically same as point one)




What else there could be? I am not concerned with the variables in interface/abstract class etc. How to decide which one to use?





filling a array with random numbers between 0-9 in c# [closed]


Possible Duplicate:

filling a array with uniqe random numbers between 0-9 in c#






I have a array like "page[100]" and i want to fill it with random numbers between 0-9 in c#...
how i can do this?
i used :



IEnumerable<int> UniqueRandom(int minInclusive, int maxInclusive)
{
List<int> candidates = new List<int>();
for (int i = minInclusive; i <= maxInclusive; i++)
{
candidates.Add(i);
}
Random rnd = new Random();
while (candidates.Count > 1)
{
int index = rnd.Next(candidates.Count);
yield return candidates[index];
candidates.RemoveAt(index);
}
}


this way :



int[] page = UniqueRandom(0,9).Take(array size).ToArray();


but it just gave me 9 unique random numbers but i need more.
how i can have a array with random numbers that are not all the same?





how to accept file or path as arguments to method in python

I am trying to write a method that will accept either an opened file



myFile = open("myFile.txt")
obj.writeTo(myFile)
myFile.close()


or a string with a path



obj.writeTo("myFile.txt")


The method is implemented as follows:



def writeTo(self, hessianFile):
if isinstance(hessianFile,file):
print("File type")
elif isinstance(hessianFile,str):
print("String type")
else:
pass


But this raises an error



NameError: global name 'file' is not defined


why is file type not defined? Shouldn't file be defined all the time? How should the implementation be corrected to properly handel both path an file as valid argument types