[mochikit] 'this' gets overridden on every function call

2007-06-20 Thread TiNo

With the following code (shortend):
--
var EditGrid = function (base_url, target_id) {
bindMethods(this);
this.base_url = base_url;
this.target_id = target_id;
this.editing = false;
this.direction = 6; //forward, see your numpad
this.editcell = null;

this.render();
}

EditGrid.prototype = {

render : function(params) {
params = params || {};
params = eval(params);
params['tg_random'] = new Date().getTime();
params['tg_format'] = json;
var d = loadJSONDoc(this.base_url + '/get_data', params);
d.addCallback(this.createGrid);
return d;
},

createGrid : function(response) {
var grid = Widget.editgrid.render(this.target_id, response);
swapDOM(this.target_id, grid);
connect(window,'onkeydown',this.handleKey);
},

editCell : function(e) {
[...]
},

handleKey : function(e) {
[...]
if(e.key()['string'] == 'KEY_ESCAPE')
{
this.abort()
e.stop()
}
[...]
},

abort : function(e){
var editcell = this.editcell;
if(!this.editing) return;

swapDOM(editcell.firstChild,document.createTextNode(this.original));
this.editing = false;
this.original = null;
this.editcell = null;
if(e)e.stop();

}
[etc...]
}
-

I have the following problem: the 'this' variable is overwritten by
the EditGrid function every time one of its methods is called.
Example: editCell is called on onclick on a tablecell, it thereby sets
this.editcell to the target cell, and this.editing to true. But when
the handleKey method is called by a onkeydown, this.editing is set to
false, and this.editcell to null, like the values they had initially.

What am I doing wrong?


--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
MochiKit group.
To post to this group, send email to mochikit@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/mochikit?hl=en
-~--~~~~--~~--~--~---



[mochikit] Re: 'this' gets overridden on every function call

2007-06-20 Thread Bob Ippolito

On 6/20/07, TiNo [EMAIL PROTECTED] wrote:

 With the following code (shortend):
 --
 var EditGrid = function (base_url, target_id) {
 bindMethods(this);
 this.base_url = base_url;
 this.target_id = target_id;
 this.editing = false;
 this.direction = 6; //forward, see your numpad
 this.editcell = null;

 this.render();
 }

 EditGrid.prototype = {

 render : function(params) {
 params = params || {};
 params = eval(params);
 params['tg_random'] = new Date().getTime();
 params['tg_format'] = json;
 var d = loadJSONDoc(this.base_url + '/get_data', params);
 d.addCallback(this.createGrid);
 return d;
 },

 createGrid : function(response) {
 var grid = Widget.editgrid.render(this.target_id, response);
 swapDOM(this.target_id, grid);
 connect(window,'onkeydown',this.handleKey);
 },

 editCell : function(e) {
 [...]
 },

 handleKey : function(e) {
 [...]
 if(e.key()['string'] == 'KEY_ESCAPE')
 {
 this.abort()
 e.stop()
 }
 [...]
 },

 abort : function(e){
 var editcell = this.editcell;
 if(!this.editing) return;
 
 swapDOM(editcell.firstChild,document.createTextNode(this.original));
 this.editing = false;
 this.original = null;
 this.editcell = null;
 if(e)e.stop();

 }
 [etc...]
 }
 -

 I have the following problem: the 'this' variable is overwritten by
 the EditGrid function every time one of its methods is called.
 Example: editCell is called on onclick on a tablecell, it thereby sets
 this.editcell to the target cell, and this.editing to true. But when
 the handleKey method is called by a onkeydown, this.editing is set to
 false, and this.editcell to null, like the values they had initially.

 What am I doing wrong?

There was a bug in connect that interfered with bind(). You can either
upgrade to the latest svn trunk, or change this:
connect(window,'onkeydown',this.handleKey);

to this:
connect(window,'onkeydown',this, 'handleKey');

-bob

--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
MochiKit group.
To post to this group, send email to mochikit@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/mochikit?hl=en
-~--~~~~--~~--~--~---



[mochikit] Re: Different search result for different browser

2007-06-20 Thread tired

Hi Bob,
Thanks for your reply. My site is trick-soft.com/searchable (instant
search in the bottom of header).
Here is the js code which use the sortable.js (changed),

function search_result()
{
document.getElementById(search-box-div).style.visibility  
=
visible;
document.getElementById(search-box-div).style.display 
=
block;
document.getElementById(bodyDiv).style.visibility 
= hidden;
sortableManager.my_d.addCallback(sortableManager.initWithData);

if ( document.forms['form1'].elements['search_item'].value != '' ) {
search_item = 
document.forms['form1'].elements['search_item'].value;
}
search_item = search_item.toLowerCase();
//else search_item = 'all';
//else alert('No keyword is given');


var domains = [];
if ( document.forms['form1'].elements['search_ingredients'].checked
== true ) {
ind = 6;
} else {
ind = 1;
}
for (var i = 0; i  sortableManager.rows.length; i++) {
var row = sortableManager.rows[i];
var domain = {};
//if ( search_item == 'all' || search_item ==  row[ind] )
json_item = row[ind].toLowerCase();
if ( json_item.indexOf(search_item) = 0 
( json_item.indexOf(search_item) == 0 ||
json_item[json_item.indexOf(search_item)-1] == ' ') ) {

for (var j = 0; j  sortableManager.cols.length; j++) {

if ( j == 1 ) {
domain[sortableManager.cols[j]] = 'a 
href=drink-details.php?
drink_id='+row[0]+''+row[j]+'/a';
} else  {
domain[sortableManager.cols[j]] = 
row[j];
}
}
domains.push(domain);
}
}
sortableManager.data.domains = domains;
   var order = sortableManager.sortState[sortableManager.sortkey];
if (typeof(order) == 'undefined') {
order = true;
}
sortableManager.drawSortedRows(sortableManager.sortkey, order,
false);

// if anything goes wrong, except for a simple cancellation,
// then log the error and show the logger
/*alert(sortableManager.my_d);
sortableManager.my_d.addErrback(function (err) {
if (err instanceof CancelledError) {
return;
}
logError(err);
logger.debuggingBookmarklet();
});*/

//  changeLink();
}

function changeLink()
{
table = getElement('sortable_table');
this.tbody = table.getElementsByTagName('tbody')[0];
// every row
var rows = this.tbody.getElementsByTagName('tr');
for (var i = 0; i  rows.length; i++) {
// every cell
var row = rows[i];
var cols = row.getElementsByTagName('td');
var obj = scrapeText(cols[0]);
obj = obj.replace(/lt;/g, ).replace(/gt;/g, );
cols[0].innerHTML = obj;
}
}

*  End of js code
**


here is the sortable.js code,

processMochiTAL = function (dom, data) {
/***

A TAL-esque template attribute language processor,
including content replacement and repeat

***/

// nodeType == 1 is an element, we're leaving
// text nodes alone.
if (dom.nodeType != 1) {
return;
}
var attr;
// duplicate this element for each item in the
// given list, and then process the duplicated
// element again (sans mochi:repeat tag)
attr = getAttribute(dom, mochi:repeat);
if (attr) {
dom.removeAttribute(mochi:repeat);
var parent = dom.parentNode;
attr = attr.split( );
var name = attr[0];
var lst = valueForKeyPath(data, attr[1]);
if (!lst) {
return;
}
for (var i = 0; i  lst.length; i++) {
data[name] = lst[i];
var newDOM = dom.cloneNode(true);
processMochiTAL(newDOM, data);
parent.insertBefore(newDOM, dom);
}
parent.removeChild(dom);
return;
}
// do content replacement if there's a mochi:content attribute
// on the element
attr = getAttribute(dom, mochi:content);
if (attr) {
dom.removeAttribute(mochi:content);
replaceChildNodes(dom, valueForKeyPath(data, attr));
return;
}
// we make a shallow copy of the current list of child nodes
// because it *will* change if there's a mochi:repeat in there!
var nodes = list(dom.childNodes);
for (var i = 0; i  nodes.length; i++) {
processMochiTAL(nodes[i], data);
}
};

mouseOverFunc = function () {