[Cosmo-dev] Re: [commits-cosmo] (br) [4567] this is a better way
not to clobber namespaces.
Travis Vachon
travis at osafoundation.org
Mon Jun 4 19:03:12 PDT 2007
Hmm, no, as I understand it that's kind of backwards:
In the previous solution Dojo created an object named cosmo.view.cal.
We then came along and mixed an object created with new function(){}
into this object.
During the constructor you do:
var self = this;
This creates a variable in the scope of the newly created object
pointing at that object.
Unfortunately, after the mixin happens we really want self to point
to the cosmo.view.cal object Dojo created, but it is still pointing
to the object created by the anonymous constructor.
The new solution is to mix the cosmo.view.cal object Dojo created
(with anything else we've added to it like cosmo.view.cal.dialog)
into the object created with the anonymous constructor. This leaves
self pointing at the right thing.
The careful reader might point out that we could still run into
problems if the original cosmo.view.cal was created with a
constructor and has an internally scoped variable pointing at it.
(That is, if the original cosmo.view.cal is doing the same thing our
anonymous-constructor created object is doing with self). This,
however, didn't make any sense in the old system (cosmo.view.cal =
new function(){blah}) since we would be assigning two different
objects to the same variable name, so we can be reasonably sure this
won't cause any problems with the current code base.
This _does_ mean that this trick will work exactly _once_. After
doing something like:
dojo.lang.mixin(new function constructorOne(){
var self =
this;
this.x = 1;
this.getX =
function(){ return self.x};
},
cosmo.view.cal);
dojo.lang.mixin(new function constructorTwo(){
var self =
this;
this.setX =
function(xval){ self.x = xval}
},
cosmo.view.cal);
cosmo.view.cal.setX(6);
cosmo.view.cal.getX() will still return 1 and the two "self"
variables will not be pointing at the same thing.
Let me know if anything still needs clarifying, and thanks for making
sure we're all on the same page!
-Travis
On Jun 4, 2007, at 5:48 PM, Matthew Eernisse wrote:
> Travis,
>
> Not being familiar with dojo.lang.mixin, I just want to confirm my
> understanding of this.
>
> Obviously what it was doing before was mixing all that stuff into
> cosmo.view.cal (but doing it in some other scope -- window scope,
> I'm assuming), so 'this' was not pointing correctly at cosmo.view.cal.
>
> Is what it's doing now mixing the cosmo.view.cal namespace object
> into window-level scope and then returning it? If so, is it
> possible to pass window explicitly so it's a little more obvious
> what it's doing there?
>
> Thanks for coming up with solutions for this irritating stuff.
>
>
> Matthew
>
> svncheckin at osafoundation.org wrote:
>> Revision
>> 4567 <http://cvs.osafoundation.org/viewcvs.cgi?rev=4567&view=rev>
>> Author
>> br
>> Date
>> 2007-06-04 12:50:07 -0700 (Mon, 04 Jun 2007)
>> Log Message
>> this is a better way not to clobber namespaces.
>> Modified Paths
>> * cosmo/trunk/cosmo/src/main/webapp/js/cosmo/view/cal.js
>> <#cosmotrunkcosmosrcmainwebappjscosmoviewcaljs>
>> Diff
>> Modified: cosmo/trunk/cosmo/src/main/webapp/js/cosmo/view/
>> cal.js
>> (4566 => 4567)
>> --- cosmo/trunk/cosmo/src/main/webapp/js/cosmo/view/cal.js
>> 2007-06-04 19:49:51 UTC (rev 4566)
>> +++ cosmo/trunk/cosmo/src/main/webapp/js/cosmo/view/cal.js
>> 2007-06-04 19:50:07 UTC (rev 4567)
>> @@ -25,7 +25,7 @@
>> dojo.require("cosmo.datetime.util");
>> dojo.require('cosmo.view.cal.dialog');
>> -dojo.lang.mixin(cosmo.view.cal, new function(){
>> +cosmo.view.cal = dojo.lang.mixin(new function(){
>> var self = this;
>> var ranges = {
>> @@ -79,10 +79,9 @@
>> delta.applyChangeType(change);
>> dojo.event.topic.publish('/calEvent', {action:'save',
>> data:ev});
>> } else {
>> + dojo.debug("ELSE!");
>> cosmo.app.showDialog(cosmo.view.cal.dialog.getProps
>> ('saveRecurConfirm', opts));
>> }
>> - - }
>> /**
>> @@ -1001,6 +1000,7 @@
>> };
>> this.triggerLoadEvents = function (o) {
>> + dojo.debug("trigger!");
>> var opts = {};
>> var collection = opts.collection;
>> var goTo = o.goTo;
>> @@ -1015,6 +1015,7 @@
>> // Changing dates
>> // --------
>> if (goTo) {
>> + dojo.debug("goto");
>> // param is 'back' or 'next'
>> if (typeof goTo == 'string') {
>> var key = goTo.toLowerCase();
>> @@ -1069,6 +1070,8 @@
>> // Load the array of events
>> // ======================
>> try {
>> + dojo.debug("start:" + start);
>> + dojo.debug("end:" + end);
>> var deferred = cosmo.app.pim.serv.getItems
>> (collection, { start: start, end: end }, { sync:
>> true });
>> eventLoadList = deferred.results[0];
>> @@ -1103,8 +1106,13 @@
>> * Eventually this will change depending on what type of view
>> is selected
>> */
>> this.setQuerySpan = function (dt) {
>> + dojo.debug("this? " + this == self);
>> + xxx = this;
>> + yyy = self;
>> this.viewStart = cosmo.datetime.util.getWeekStart(dt);
>> + dojo.debug("viewSTart: " + this.viewStart)
>> this.viewEnd = cosmo.datetime.util.getWeekEnd(dt);
>> + dojo.debug("viewEnd: " + this.viewEnd)
>> return true;
>> };
>> /**
>> @@ -1125,5 +1133,5 @@
>> dojo.date.dateParts.WEEK, incr);
>> return queryDate;
>> };
>> -});
>> +}, cosmo.view.cal);
>>
>> ---------------------------------------------------------------------
>> ---
>> _______________________________________________
>> Commits-Cosmo mailing list
>> Commits-Cosmo at osafoundation.org
>> http://lists.osafoundation.org/cgi-bin/mailman/listinfo/commits-cosmo
>
> _______________________________________________
> cosmo-dev mailing list
> cosmo-dev at lists.osafoundation.org
> http://lists.osafoundation.org/mailman/listinfo/cosmo-dev
More information about the cosmo-dev
mailing list