Discussion:
[qooxdoo-devel] Is it possible to add another object to the converter for a singlevalue-binding?
LoneSurvivor
2016-04-15 18:58:49 UTC
Permalink
Hi everybody,

is it possible, to pass or use another object from outside inside of the
converter-method used in a singlevalue-binding?

For e.g. I have this code:

var someObjectFromOutside = new CoolObject();

var lblInfo = new qx.ui.basic.Label();
this.bind("propertyObject.intValue", lblInfo, "value", {
converter: function (data) {
return someObjectFromOutside.method(data);
}
});

I'd like to manipulate the data-value by using the method of an an external
object from outside instead of creating and destoying it every time inside
the converter is called.

Is that possible and if, how can it be done?

Thank you all and greetings
LoneSurvivor



--
View this message in context: http://qooxdoo.678.n2.nabble.com/Is-it-possible-to-add-another-object-to-the-converter-for-a-singlevalue-binding-tp7588266.html
Sent from the qooxdoo mailing list archive at Nabble.com.
LoneSurvivor
2016-04-15 19:30:53 UTC
Permalink
While writing the post, I got an idea: would it be a proper way, to create a
wrapping class for the value to bind which contains the method so it can be
called in converter?

My Real-World-example which is working (with Date instead of int, sorry for
the confusion):

qx.Class.define("mkss.data.DateWrapper", {
extend: qx.core.Object,
properties: {
date: {
check: "Date"
}
},
construct: function (date, format) {
this.base(arguments);
this.setDate(date);
this.dateFormat = new qx.util.format.DateFormat(format);
},
members: {
getFormattedDate: function () {
return this.dateFormat.format(this.getDate());
}
}
});

The gameData-object I'm using next has a date-property like this:

qx.Class.define("mkss.data.GameData",
{
extend: qx.core.Object,
properties: {
date: {
check: "Date",
init: new mkss.data.DateWrapper(new Date("1980-01-01"),
"d.M.y"),
event: "changeDate"
}, ...
}
});

And in the displaying widget the binding happens like this:

var lblDate = new qx.ui.basic.Label();
this.bind("gameData.date", lblDate, "value", {
converter: function (data) {
return data.getFormattedDate();
}
});
this.add(lblDate);

Would you think this is a way to go?



--
View this message in context: http://qooxdoo.678.n2.nabble.com/Is-it-possible-to-add-another-object-to-the-converter-for-a-singlevalue-binding-tp7588266p7588267.html
Sent from the qooxdoo mailing list archive at Nabble.com.
LoneSurvivor
2016-04-15 20:37:21 UTC
Permalink
Okay, there is at least one more fallacy. If I add a method like the
following to the DateWrapper to increase the date by x days, the
label-binding wouldn't call the converter because the wrong key is listened.
But if I am setting the binding-key to gameData.date.date, the converter
would run but fail because the data-value is the date-property itself which
has no getFormattedDate-function...

The changed DateWrapper-class for completeness:

qx.Class.define("mkss.data.DateWrapper", {
extend: qx.core.Object,
properties: {
date: {
check: "Date",
event: "changeDate"
}
},
construct: function (date, format) {
this.base(arguments);
this.setDate(date);
this.dateFormat = new qx.util.format.DateFormat(format);
this._intDateFormat = new qx.util.format.DateFormat("yyyy-MM-dd");
},
members: {
getFormattedDate: function () {
return this.dateFormat.format(this.getDate());
},
addDays: function (incDays) {
// found here: http://jsfiddle.net/sparebytes/XrWzq/

var tmpDate = new Date(this.getDate());
tmpDate.setDate(this.getDate().getDate() + incDays);
this.setDate(tmpDate);
}
}
});



--
View this message in context: http://qooxdoo.678.n2.nabble.com/Is-it-possible-to-add-another-object-to-the-converter-for-a-singlevalue-binding-tp7588266p7588269.html
Sent from the qooxdoo mailing list archive at Nabble.com.
John Spackman
2016-04-15 20:41:56 UTC
Permalink
I think that would work (although it's hard to be sure without a working example) but you could solve the initial problem with a closure.

John
Post by LoneSurvivor
While writing the post, I got an idea: would it be a proper way, to create a
wrapping class for the value to bind which contains the method so it can be
called in converter?
My Real-World-example which is working (with Date instead of int, sorry for
qx.Class.define("mkss.data.DateWrapper", {
extend: qx.core.Object,
properties: {
date: {
check: "Date"
}
},
construct: function (date, format) {
this.base(arguments);
this.setDate(date);
this.dateFormat = new qx.util.format.DateFormat(format);
},
members: {
getFormattedDate: function () {
return this.dateFormat.format(this.getDate());
}
}
});
qx.Class.define("mkss.data.GameData",
{
extend: qx.core.Object,
properties: {
date: {
check: "Date",
init: new mkss.data.DateWrapper(new Date("1980-01-01"),
"d.M.y"),
event: "changeDate"
}, ...
}
});
var lblDate = new qx.ui.basic.Label();
this.bind("gameData.date", lblDate, "value", {
converter: function (data) {
return data.getFormattedDate();
}
});
this.add(lblDate);
Would you think this is a way to go?
--
View this message in context: http://qooxdoo.678.n2.nabble.com/Is-it-possible-to-add-another-object-to-the-converter-for-a-singlevalue-binding-tp7588266p7588267.html
Sent from the qooxdoo mailing list archive at Nabble.com.
------------------------------------------------------------------------------
Find and fix application performance issues faster with Applications Manager
Applications Manager provides deep performance insights into multiple tiers of
your business applications. It resolves application problems quickly and
reduces your MTTR. Get your free trial!
https://ad.doubleclick.net/ddm/clk/302982198;130105516;z
_______________________________________________
qooxdoo-devel mailing list
https://lists.sourceforge.net/lists/listinfo/qooxdoo-devel
LoneSurvivor
2016-04-15 23:12:46 UTC
Permalink
Finally I found a working solution. I'm not sure if I like that way but it
works and its not that much more code.

Following is the wrapper-class. It contains a new property which is set as
the formatted date-string:

qx.Class.define("mkss.data.DateWrapper", {
extend: qx.core.Object,
properties: {
date: {
check: "Date",
apply: "_applyDate"
},
formattedDateString: {
check: "String",
event: "changeFormattedString"
}
},
construct: function (date, format) {
this.base(arguments);
this.dateFormat = new qx.util.format.DateFormat(format);
this.setDate(date);
},
members: {
_applyDate : function() {
this.setFormattedDateString(this._getFormattedDate());
},
_getFormattedDate: function () {
return this.dateFormat.format(this.getDate());
},
addDays: function (incDays) {
// found here: http://jsfiddle.net/sparebytes/XrWzq/
var tmpDate = new Date(this.getDate());
tmpDate.setDate(this.getDate().getDate() + incDays);
this.setDate(tmpDate);
}
}
});

This new property now is bound to the label which no longer needs to access
the format-method because everything is done in the wrapper.

var lblDate = new qx.ui.basic.Label();
this.bind("gameData.date.formattedDateString", lblDate, "value", {
converter: function (data) {
return "Datum: " + data;
}
});
this.add(lblDate);

Now in a loop by my manager-object I did not show here, the addDays-method
is called incrementally which updates the formatted value in the bound
label.

If anybody knows a better way, please let me know.

Thank you again and have a good night
LoneSurvivor



--
View this message in context: http://qooxdoo.678.n2.nabble.com/Is-it-possible-to-add-another-object-to-the-converter-for-a-singlevalue-binding-tp7588266p7588271.html
Sent from the qooxdoo mailing list archive at Nabble.com.

Loading...