Is it possible to add another object to the converter for a singlevalue-binding?

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Is it possible to add another object to the converter for a singlevalue-binding?

LoneSurvivor
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
Reply | Threaded
Open this post in threaded view
|

Re: Is it possible to add another object to the converter for a singlevalue-binding?

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 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?
Reply | Threaded
Open this post in threaded view
|

Re: Is it possible to add another object to the converter for a singlevalue-binding?

LoneSurvivor
This post was updated on .
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);
    },
    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);
        }
    }
});
Reply | Threaded
Open this post in threaded view
|

Re: Is it possible to add another object to the converter for a singlevalue-binding?

John Spackman
In reply to this post by LoneSurvivor
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


> On 15 Apr 2016, at 20:44, LoneSurvivor <[hidden email]> wrote:
>
> 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.
>
> ------------------------------------------------------------------------------
> 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
> [hidden email]
> https://lists.sourceforge.net/lists/listinfo/qooxdoo-devel


------------------------------------------------------------------------------
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
[hidden email]
https://lists.sourceforge.net/lists/listinfo/qooxdoo-devel
Reply | Threaded
Open this post in threaded view
|

Re: Is it possible to add another object to the converter for a singlevalue-binding?

LoneSurvivor
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