Categories
discuss

How can I delay page transition in jQuery Mobile until page data is ready?

I have a mobile single-page web application that is built using jquery-mobile (jqm) and knockout. The application itself has multiple pages but they are all contained within a single HTML document.

Problem: after changing my “create view model for page” from sync to async behavior, I have the problem that jquery-mobile fires its events before the data is ready.

Background: up until recently I had been working with sample data, basically a huge JSON blob, and everything worked smoothly. With the new async composition of view models from various sources, data is not ready immediately and my “buildViewModel” method takes a continuation callback instead of just synchronously returning data.

I’m subscribing to the pagebeforecreate and pagebeforechange events, and fire off the code to populate the viewmodel here. The problem is that after returning from the event handler, jqm triggers the remaining chain of events before the data is available. This causes a page transition to an unprepared page, which is undesirable.

I have tried to call event.preventDefault in all of the before-events and manually calling $.mobile.changePage once the page is ready to be a) enhanced and b) the page transition to occur, but without any luck.

I’ve scanned the jquery-mobile source, but couldn’t spot anything that looked like it would allow me to delay the pagebeforeshow event, which is essentially what I need in order to be able to render the page properly.

How can I ensure that 1) data is available and 2) knockout has been applied to perform initial DOM manipulations, before jquery-mobile attempts to enhance the page and before it executes the in-transition to the page?

I also considered using synchronous ajax to fetch resources, but this will (I think) not work for resources loaded from the device (using PhoneGap/Cordova), and has other negative consequences that I’d like to avoid.

FWIW, I’d like to avoid having to manually handle all navigation events by wiring up click-handlers everywhere, but I’m open to all solutions if need be.

Apologies if this is a duplicate; I’ve searched and read a ton of questions, but not found an answer or question that was quite the same. It just sounds incredible that I would be the first to hit this problem, as I imagine it is a common scenario..

Update: clarified problem scenario description.

Answer

I had this exact same problem.

The only solution I’ve been able to come up with is to write a custom transition handler that defers starting the transition until the Ajax request completes.

Here’s a fiddle showing the technique. The fiddle doesn’t use Knockout, but does show how to defer the transition.

Basically, since $.ajax() returns a promise, I can pipe that into the promise returned by the default transition handler and return it from my new handler.

In my pagebeforeshow handler, I attach the Ajax promise to the page so that the transition handler has access to it. Not sure if this is the best way, but I liked it better than using a global variable.

The only thing I didn’t like about this is that it delays the start of the transition until the Ajax response arrives so it could feel like the page has “hung” to the user making them click again. Manually showing the loading message makes it feel a bit more responsive.

Hope this helps and please let me know if you find a better solution!

Categories
discuss

Synchronizing LinkedHashmap externally

What is the best way to implement synchronization of a linkedhashmap externally, without using Collections.synchronizedMap

When Collections.synchronizedMap is used entire datastructure is locked, so performance is hugely impacted in a bad way.

What is the best way to lock only required part of datastructure. e.g. If thread is accessing key (K1), it should lock only Key(K1) and Value(v1) part of the datastructure

Answer

You can’t get a fine-grained-locking, FIFO-eviction concurrent map from the built-in Java implementations.

Check out Guava’s Cache or the open-source ConcurrentLinkedHashMap project.

Categories
discuss

Hibernate Criteria for entity with embedded objects

I have an entity “UserDetails” which has the following variables:

  1. String userId
  2. String userName
  3. UserContact userContact (where UserContact is an Embeddable class)

UserContact has the following variables:

  1. String phoneNumber
  2. String email
  3. String city

What will be a Hibernate Criteria for fetching the following list:

Users with userName = ‘sam’ and with city = ‘New York’

I tried the following and got the runtime exception that it doesn’t recognize the variable ‘city’:

List<UserLogin> list = session.createCriteria(UserLogin.class)
    .add(Restrictions.eq("userName","sam"))
    .add(Restrictions.eq("city", "New York"))
    .list();

Answer

Oh I figured it out…

List<UserLogin> list = session.createCriteria(UserLogin.class)
   .add(Restrictions.eq("userName","sam"))
   .add(Restrictions.eq("userContact.city", "New York"))
   .list();

Silly, just needed to add ‘userContact.city’ instead of ‘city’, where userContact is the object of the class UserContact in my entity.

Categories
discuss

How to estimate distance between two android devices? (bluetooth preferred)

Target is not to have the real distance. It is something simpler.
Target is to check whether another device is very very close. True or false. Let’s say 10 or 15 cms is close so our check is true and any device further away fails the check and it is false.

My first approach was to use api’s method fetchUuidsWithSdp() but it failed!
Latency seemed the same whether the devices where a couple of cms away or at the other end of a large room!

Any solution even without bluetooth is acceptable. For instance, I am totally unfamiliar with the wifi direct p2p protocol. Maybe there could be something in there useful to have a rough estimation of distance.

Edit: I am reading about Received Signal Strength Indicator(RSSI). This could be used for our particular case, a rough estimation of distance between devices. But maybe there could be something to combine in order to improve the accuracy.

Edit: I put Ralgha’s answer to the test. I did established a connection and had a minor protocol to ping-pong among devices.
When ping-ponged with 1 byte average latency was 22.6000 msec when very close and 22.6087 msec when on the other corner of the room.
When ping-ponged with 1024 bytes average latency was 40.3173 msec when very close and even smaller, 32.3138 msec, when on the other corner of the room.
After all, indeed, latency does not carry any information about distance!

Thanks!

Answer

You can measure distance with sound.

Categories
discuss

Clojure and Java interop in a real world

I’m thinking about start using (not playing with) Clojure. Are there any useful guides? I’m not asking about lein, javac or any other ‘small’ manual tools. I need to know how to have Java and Clojure sources in eclipse in the same project. How to make them calling each other without compilation errors? How to configure maven? How to set up fully productive development environment? Is it even possible at the moment? What plugins may be useful? Where to start?

Answer

I have a fully working production setup with Eclipse, Maven and Clojure that is working very nicely at the moment. Hopefully it is helpful as an example of a good polyglot setup within a Java IDE.

I don’t use leiningen – Nothing against lein at all – it’s very nice and ideal in a pure Clojure/CLI world. But I’ve found that pure Maven is nicer to work with in a polyglot Java+Clojure environment with an IDE since the tool integration is so much better. Also from an ecosystem / audience / community perspective if you want people from the Java world to be able to build your source you are going to cause a lot less confusion if you just use Maven directly.

Here is my setup:

  • Eclipse 4.2 as main IDE
  • Counterclockwise Eclipse plugin – very good, takes care of REPL, Clojure editing etc.
  • Maven used to manage all projects (I use the built in Eclipse Maven integration mostly, but occasionally use the CLI version as well)
  • cljunit used to enable JUnit tests to run on Clojure parts of the project
  • Github / Travis CI used for SCM and Continuous integration, accessed using the built-in EGit team provider in Eclipse

In terms of actually how I manage / set up the project itself:

  • I configure everything with Maven, using standard Maven directory layout. Polyglot Java+Clojure Projects typically have both src/main/java and src/main/clojure
  • Clojure is just a Maven dependency, like any other Java library.
  • I make the Clojure source directories into resource directories in the Maven setup. This means that the .clj files get bundled in any jars and can be loaded / run dynamically at runtime.
  • I usually make the entry point on the Java side with a public static void main(...) as usual, but call quite quickly into the Clojure code. See this blog post on calling Clojure from Java.

Finally some coding tips for polyglot Java+Clojure

  • I find that Java is better for low level data structures, libraries and algorithms, while Clojure is better for integrating things together and “glue” code.
  • Clojure calling Java is usually easier / more elegant than the other way round. Also it makes sense since you generally want the dependencies to flow that way (higher level code calling lower level code)
  • If you make all your Java classes immutable, they play very nicely in a Clojure world with minimal effort.
  • Sometimes it is worth making one or more of your Java classes implement some of the Clojure interfaces, particularly clojure.lang.IFn for example. This way your Java objects can act as first class functions in Clojure code.

Here’s an example project that mixes Java and Clojure source:

I also wrote a small library (clojure-utils) that includes some example code for calling Clojure from Java, which you may find useful.

Source: stackoverflow
Text is available under the Creative Commons Attribution-ShareAlike License; additional terms may apply. By using this site, you agree to the Privacy Policy, and Copyright Policy. Content is available under CC BY-SA 3.0 unless otherwise noted. The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 © No Copyrights, All Questions are retrived from public domain..