开发者

setup webos list selector choices with ajax json response dynamically

I'm trying to develop an application which gets the the response from the MySQL database using ajax post and update in list selector, but the list is displaying empty, can some one help me out from this please.....

code for .js:

SecondAssistant.prototype.setup = function() {
this.selectorChanged = this.selectorChanged.bindEventListener(this);
Mojo.Event.listen(this.controller.get('firstselector'), Mojo.Event.propertyChange,     this.selectorChanged);

this.names = [];

try {
    new Ajax.Request('http://localhost/projects/testingasdf.php', {
        method: 'post',
        parameters: {
        'recs': getallrecords,
        'q': q
        },
        evalJSON: 'true',
        onSuccess: function(response){
            var json = response.responseJSON;
            var count = json.count - 1;

            for(i=0; i<count; i++){
                this.names.push({
                    label: json[i].name,
                    value: '0'
                });
            }
                       this.controller.modelChanged(this.model);
        }.bind(this),
        onFailure: function(){
            Mojo.Controller.errorDialog('Failed to get ajax response');

        }

    });

}
catch (e){
    Mojo.Controller.errorDialog(e);
}

this.controller.setupWidget("firstselector",
          this.attributes = {
              label: $L('Name'),
              modelProperty: 'currentName'
          },
          this.model = {
              choices: this.names
          }
        ); 

};

code for php:

<?php
header('Content-type: application/json');  // this is the magic that sets responseJSON


$conn = mysql_connect('localhost', 'root', '')// creating a connection



mysql_select_db("test", $conn)开发者_JS百科 or die('could not select the database');//selecting database from connected database connection

switch($_POST['recs'])
{
    case'getallRecords':{
        $q = $_POST['q'];

        //performing sql operations
        $query = sprintf("SELECT * FROM user WHERE name= $q");
        $result = mysql_query($query) or die('Query failed:' .mysql_error());
        $all_recs = array();
        while ($line = mysql_fetch_array($result,MYSQL_ASSOC)) {
        $all_recs[] = $line;
        }
        break;
        }   
        }

echo json_encode($all_recs);

// Free resultset
mysql_free_result($result);


// closing connection
mysql_close($conn);
?>


I would move the model updating code out of the SecondAssistant.prototype.setup method and have it fire somewhere in SecondAssistant.prototoype.activate.

Also call modelChanged

this.controller.modelChanged(this.model);

There is a typo on bindEventListener - should be bindAsEventListener and the return of the bind should be a different object:

this.selectorChangedBind = this.selectorChanged.bindAsEventListener(this);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜