summaryrefslogtreecommitdiffstats
path: root/src/declarative/qml/qdeclarativeobjectscriptclass.cpp
diff options
context:
space:
mode:
authorBea Lam <bea.lam@nokia.com>2011-01-27 05:34:34 (GMT)
committerBea Lam <bea.lam@nokia.com>2011-01-27 05:37:10 (GMT)
commit43b8305367156c1ceb09eb4a056bdae3f325b5eb (patch)
tree8d792838920476495cba22c802939b719619a55d /src/declarative/qml/qdeclarativeobjectscriptclass.cpp
parent357f1163f75e4b23a5b87dd6b3d742d167cd9c10 (diff)
downloadQt-43b8305367156c1ceb09eb4a056bdae3f325b5eb.zip
Qt-43b8305367156c1ceb09eb4a056bdae3f325b5eb.tar.gz
Qt-43b8305367156c1ceb09eb4a056bdae3f325b5eb.tar.bz2
Allow property bindings to be easily created from JavaScript
Properties can now be assigned a function that returns the binding value. Task-number: QTBUG-14964 Reviewed-by: Aaron Kennedy
Diffstat (limited to 'src/declarative/qml/qdeclarativeobjectscriptclass.cpp')
-rw-r--r--src/declarative/qml/qdeclarativeobjectscriptclass.cpp19
1 files changed, 14 insertions, 5 deletions
diff --git a/src/declarative/qml/qdeclarativeobjectscriptclass.cpp b/src/declarative/qml/qdeclarativeobjectscriptclass.cpp
index cbbf2b9..7701a23 100644
--- a/src/declarative/qml/qdeclarativeobjectscriptclass.cpp
+++ b/src/declarative/qml/qdeclarativeobjectscriptclass.cpp
@@ -359,8 +359,20 @@ void QDeclarativeObjectScriptClass::setProperty(QObject *obj,
}
}
+ QDeclarativeBinding *newBinding = 0;
+ if (value.isFunction() && !value.isRegExp()) {
+ QScriptContextInfo ctxtInfo(context);
+ QDeclarativePropertyCache::ValueTypeData valueTypeData;
+
+ newBinding = new QDeclarativeBinding(value, obj, evalContext);
+ newBinding->setSourceLocation(ctxtInfo.fileName(), ctxtInfo.functionStartLineNumber());
+ newBinding->setTarget(QDeclarativePropertyPrivate::restore(*lastData, valueTypeData, obj, evalContext));
+ if (newBinding->expression().contains("this"))
+ newBinding->setEvaluateFlags(newBinding->evaluateFlags() | QDeclarativeBinding::RequiresThisObject);
+ }
+
QDeclarativeAbstractBinding *delBinding =
- QDeclarativePropertyPrivate::setBinding(obj, lastData->coreIndex, -1, 0);
+ QDeclarativePropertyPrivate::setBinding(obj, lastData->coreIndex, -1, newBinding);
if (delBinding)
delBinding->destroy();
@@ -379,10 +391,7 @@ void QDeclarativeObjectScriptClass::setProperty(QObject *obj,
QString error = QLatin1String("Cannot assign [undefined] to ") +
QLatin1String(QMetaType::typeName(lastData->propType));
context->throwError(error);
- } else if (!value.isRegExp() && value.isFunction()) {
- QString error = QLatin1String("Cannot assign a function to a property.");
- context->throwError(error);
- } else {
+ } else if (!value.isFunction()) {
QVariant v;
if (lastData->flags & QDeclarativePropertyCache::Data::IsQList)
v = enginePriv->scriptValueToVariant(value, qMetaTypeId<QList<QObject *> >());