I didn't get any replies so far, so I kluged it. In the spirit of sharing in case someone else is looking to get this working with minimal effort:
1. Create some public slots on a QObject you intend to add to the javascript object, or add one for only this purpose.
public slots:
...
void setPost(const QMap<QString,QVariant>& object);
QMap<QString, QVariant> getPost();
void setGet(const QMap<QString,QVariant>& object);
QMap<QString, QVariant> getGet();
...
private:
QMap<QString, QVariant> _post;
QMap<QString, QVariant> _get;
public slots:
...
void setPost(const QMap<QString,QVariant>& object);
QMap<QString, QVariant> getPost();
void setGet(const QMap<QString,QVariant>& object);
QMap<QString, QVariant> getGet();
...
private:
QMap<QString, QVariant> _post;
QMap<QString, QVariant> _get;
To copy to clipboard, switch view to plain text mode
2. Define these functions along these lines:
...
void MyQObject::setPost(const QMap<QString,QVariant>& object) {
_post = object;
}
QMap<QString, QVariant> MyQObject::getPost() {
return _post;
}
void MyQObject::setGet(const QMap<QString,QVariant>& object) {
_get = object;
}
QMap<QString, QVariant> MyQObject::getGet() {
return _get;
}
...
...
void MyQObject::setPost(const QMap<QString,QVariant>& object) {
_post = object;
}
QMap<QString, QVariant> MyQObject::getPost() {
return _post;
}
void MyQObject::setGet(const QMap<QString,QVariant>& object) {
_get = object;
}
QMap<QString, QVariant> MyQObject::getGet() {
return _get;
}
...
To copy to clipboard, switch view to plain text mode
3. Created a required js include file:
/* Code based on that written by Tobias Cohen, tobiascohen.com*/
$.fn.serializeObject = function(){
var object = {};
var sArray = this.serializeArray();
$.each(sArray , function() {
if (object[this.name]) {
if (!object[this.name].push) {
object[this.name] = [object[this.name]];
}
object[this.name].push(this.value || '');
} else {
object[this.name] = this.value || '';
}
});
return object;
};
var POST = MyQObject.getPost();
var GET = MyQObject.getGet();
function MyLibSubmit(form_selector) {
var ob = $(form_selector).serializeObject();
if ($(form_selector).attr('method').toLowerCase() == 'post') {
MyQObject.setPost(ob);
} else {
MyQObject.setGet(ob);
}
return true;
}
/* Code based on that written by Tobias Cohen, tobiascohen.com*/
$.fn.serializeObject = function(){
var object = {};
var sArray = this.serializeArray();
$.each(sArray , function() {
if (object[this.name]) {
if (!object[this.name].push) {
object[this.name] = [object[this.name]];
}
object[this.name].push(this.value || '');
} else {
object[this.name] = this.value || '';
}
});
return object;
};
var POST = MyQObject.getPost();
var GET = MyQObject.getGet();
function MyLibSubmit(form_selector) {
var ob = $(form_selector).serializeObject();
if ($(form_selector).attr('method').toLowerCase() == 'post') {
MyQObject.setPost(ob);
} else {
MyQObject.setGet(ob);
}
return true;
}
To copy to clipboard, switch view to plain text mode
4. define your form thusly:
<form id="testForm" method="post" action="index.html" onsubmit="return MyLibSubmit('#testForm')" >
<input type="text" name="val" value="Some value" />
<input type="submit" name="submit" value="Send"/>
</form>
<form id="testForm" method="post" action="index.html" onsubmit="return MyLibSubmit('#testForm')" >
<input type="text" name="val" value="Some value" />
<input type="submit" name="submit" value="Send"/>
</form>
To copy to clipboard, switch view to plain text mode
You will now have access to JS variables named POST and GET in your JS Scripts.
Cheers
Bookmarks