Tuesday, November 8, 2011

High speeding technique for Ajax post

When you use this technique, your user feels as fast as client app.

The conventional method 
  1. Set onClick = 'update(this);' to element
  2. Post with update()
  3. Wait for callback function
  4. Update UI with callback function
→User has to wait for callback function.(S)he feels slow.

This technique
  1. Set onClick = 'update(this);' to element
  2. Before post with update(),update UI
  3. Wait for callback function
  4. If post is failed,reset UI and handle error with callback function
→User doesn't have to wait for callback function.(S)he feels fast.
#If app's updateing often fails and a user overlook errors and resetting UI, (s)he has discontent.

Best practice
Callback function works only for resetting UI and handling error so you can commonize it.

Note
The conventional method is popular method.Why?I think the reason is callback function is too convinient.
'Language defines thinking'--This is Sapir-Whorf hypothesis.I suggest that callback function defines programmer's thinking. 
In Fuzzy system,for example SNS,Game, you might want to use this technique.
Especially in Game,fast response is essential. 

Thursday, March 24, 2011

tweepy memo

Last login: Thu Mar 24 16:25:31 on ttys000
/Users/prk29% easy_install tweepy
Searching for tweepy
Reading http://pypi.python.org/simple/tweepy/
Reading http://github.com/joshthecoder/tweepy
Best match: tweepy 1.7.1
Downloading http://pypi.python.org/packages/2.6/t/tweepy/tweepy-1.7.1-py2.6.egg#md5=920800078a8820cbc75c0ca2527dbe60
Processing tweepy-1.7.1-py2.6.egg
Moving tweepy-1.7.1-py2.6.egg to /Library/Python/2.6/site-packages
Adding tweepy 1.7.1 to easy-install.pth file

Installed /Library/Python/2.6/site-packages/tweepy-1.7.1-py2.6.egg
Processing dependencies for tweepy
Finished processing dependencies for tweepy
/Users/prk29%

Monday, February 7, 2011

How to use Goole App Engine Channel API

Overview:
Channel API is the API to  synchronize clients in real time.
The procedure is following.
1.Client side:request a Channel API token and client ID from GAE.
2.GAE side:create the token and return it to the client.
3.Client side:open the client's channel with the token.
4.GAE side:send message to the client with the client ID, not the token.
5.Client side:get message.
1.Client side:request a Channel API token and client ID from GAE.
html:
<span id="token" style="display:none;">none-token</span>
javascipt:
function take_token() {
    console.debug('taking a token');
    $.get("/ChannelAPI", {}, function(results){
   $("#token").text(results.token)//save token to "#token".
   make_channel(results.token)
    },"json");
}
2.GAE side:create the token and return it to the client.
def get(self):
client_id = "user1"
token = channel.create_channel(client_id)
self.response.headers.add_header('Content-Type', 'application/json; charset=utf-8')
self.response.out.write(simplejson.dumps({'token': token}))
3.Client side:open the client's channel with the token.
function make_channel(token){
    channel = new goog.appengine.Channel(token);
    socket = channel.open();
    socket.onopen = onOpened;
    socket.onmessage = onMessage;
    socket.onerror = onError;
    socket.onclose = onClose;
}
4.GAE side:send message to the client with the client ID, not the token.
def post(self):
channel.send_message("user1", simplejson.dumps({"tokens":tokens}))
5.Client side:get message.
function onMessage(message){
a = new Array()
    console.debug('received a message: ', message);
    console.debug("receive a message.data:",message.data);
    var obj = JSON.parse(message.data);
    console.debug(obj.tokens)
    console.debug(obj.tokens[0])
}

Thursday, February 3, 2011

How to post data with jQuery on PhoneGap

I can not post data to GAE with XMLHttpRequest on iPhone.However,I can with jQuery and JSONP on Safari and iPhone,but I can not on Chrome.
That code is following.
HTML side:
<head>
<title>puriketu memo</title>
<meta name="viewport" content="user-scalable=no, width=device-width" />
<link rel="stylesheet" type="text/css" href="iphone.css" media="only screen and (max-width: 480px)" />
<!--[if IE]> 
<link rel="stylesheet" type="text/css" href="explorer.css" media="all" /> 
<![endif]-->
<script type="text/javascript" src="http://localhost:8082/js/jquery.js"></script>
<script type="text/javascript" src="http://localhost:8082/js/iphone.js"></script>
  </head>
javascript side:
$.post(
request,
{"content":"content a"},
function(data, status) {
alert("post success");
}
);
          );
reference:
As soon as you sent the code, I have tested it and it did not work in my Chrome, Firefox or Android device. So, I came home last night and tested your code on Safari and my iPhone app, it works without any issues, actually my original code also worked without any issues in Safari and iPhone.  

How to get data from Google App Engine with XMLHttpRequest on PhoneGap

There is a cross domain issue when we implement this program.
Follow the code disolves it.

javascript side:
var url = request; // ex)"http://localhost:8080"
var xhr = new XMLHttpRequest();
xhr.open("GET", url, true);
xhr.onreadystatechange = function(){
if ( xhr.readyState == 4 ) {
if ( xhr.status == 200 ) {
var response = JSON.parse(xhr.responseText);
$.each(response,function(i,r){
alert(r.content);
});
alert("get success!");
} else {
alert(xhr.responseText);
alert("lose");
}
}
};
xhr.send(null);
Google App Engine side:
class MainHandler(webapp.RequestHandler):
def get(self):
self.response.headers['Access-Control-Allow-Origin'] = '*'
self.response.headers['Access-Control-Allow-Headers'] = '*'
self.response.headers['Access-Control-Allow-Methods'] = 'GET'
self.response.headers['Content-Type'] = 'text/plain;charset=UTF-8'
tweets = db.GqlQuery('SELECT * FROM Tweet')
results = []
for tweet in tweets:
result = {}
result['content'] = tweet.content
results.append(result)
self.response.out.write(simplejson.dumps(results))

reference:
XMLHttpRequest Cross-domain issue
As soon as you sent the code, I have tested it and it did not work in my Chrome, Firefox or Android device. So, I came home last night and tested your code on Safari and my iPhone app, it works without any issues, actually my original code also worked without any issues in Safari and iPhone. 

Saturday, January 29, 2011

How to get data from Google App Engine for python with jQuery

GAE side:
import simplejson
...
class HistoryHandler(webapp.RequestHandler):
def get(self):
viewerid = self.request.get('viewerid')
query = "SELECT * FROM dbmodel where viewerid = '" + viewerid + "'"
datas = db.GqlQuery(query)
result = []
for data in datas:
r = {}
r["key"] = data.key().id()
r["unreadflag"] = data.unreadflag
result.append(r)
self.response.out.write(simplejson.dumps(result))
jQuery side:
    $.get('http://xxxxxxxx.appspot.com/historydata signed', {viewerid : viewerid}, function(results) {
      if(results.length == 0){
      ...
      }
      else{
              //when results is an object, this "i" is a hash key.
              //when results is an array, this "i" is an index.
     $.each(results, function(i, r) {
     alert(r.unreadflag);
     });
      }
    }, 'json');
P.S.
My English is not good enough.
If there is a mistake and an anxious point,teach me softly.

How to post data to Google App Engine for python with jQuery

jQuery side:
$.post(
"http://xxxxxxxxxxxxxxxxx.appspot.com/introdata",
{"viewerid": viewerid, "fromid": from, "toid": to, "introreason": reason},
function(data, status) {
$("#oktext").css('font-size', '12px').css("font-weight", "bold").css("color", "red").fadeIn(2000);
$("#oktext").fadeOut(5000);
}
);
GAE side:

from django.utils import simplejson
...

class AddHandler(webapp.RequestHandler):
       ...
def post(self):
viewerid = self.request.get('viewerid')
data = introData(viewerid=viewerid)
data.fromid = self.request.get('fromid')
data.toid = self.request.get('toid')
data.introreason = self.request.get('introreason')
data.unreadflag = '1'
data.put()
...
def main():
application = webapp.WSGIApplication(
[
('/introdata',AddHandler),
...
],
debug=True)
wsgiref.handlers.CGIHandler().run(application)

if __name__ == '__main__':
main()