Setting the href of the anchor in a callback

172 views
Skip to first unread message

Spike

unread,
Oct 21, 2009, 12:10:23 AM10/21/09
to jsTree
Hi there, I'm just wondering how to set the href attribute of a node
from a callback. I tried this but it didn't work:

NODE.children("a:eq(0)").href = data.href;

the data variable is just what gets returned to the server. I've
implemented the double click functionality to send the user to the
link when it's double clicked on.

Any help would be greatly appreciated :)

Cheers,

Brendon

vakata

unread,
Oct 21, 2009, 1:36:34 AM10/21/09
to jsTree
Hi,

First of all - use the jQuery attr function or the DOM setAttribute
method.

Secondly, did you place the code correctly? It should be something
like:

onsomething: function (NODE) {
$.get("something", function (data) {
NODE.children("a:eq(0)").attr("href",data.href);
})
}

I hope this helps,
Regards,
Ivan

Brendon Muir

unread,
Oct 21, 2009, 5:59:32 AM10/21/09
to jst...@googlegroups.com
Hi Ivan, yep I have tried that code (though thank you for pointing out the attribute thing. I'm a prototype boy so I'm still getting used to what's different in jquery, I had a feeling there was a 'good' way to set attributes.

The ajax call returns some json: 
{"href":"4-spike/eportfolios/8-new-eportfolio","id":112}

and this code is executed on a successful return:

success : function (data, textStatus) {
NODE.id = data.id;
NODE.children("a:eq(0)").attr("href", data.href);
TREE_OBJ.rename(NODE);
}

I call the rename because I custom-set the name of the node when it's created. When the href line is there the rename function never gets called. 

Actually I keep forgetting Safari doesn't tell me the JS errors, I tried in Firefox and got:

NODE.children is not a function

also TREE_OBJ.rollback is not a function

That's for the rollback problem. I thought that perhaps it's because I use the prototype libraries too so I disabled those and it makes no difference. 

Anyway, I hope i've given you enough information :) Please let me know if you require any more :D

Cheers,

Brendon

2009/10/21 vakata <ivan.b...@gmail.com>

vakata

unread,
Oct 21, 2009, 6:36:33 AM10/21/09
to jsTree
1.0-beta will return jQuery extended nodes in all callbacks. But until
then - DOM nodes are always returned. So to use .children you need to:

$(NODE).children(...

As for TREE_OBJ - let me see the beginning of the callback - you need
to have TREE_OBJ as an argument in the callback function.

Regards,
Ivan

On 21 Окт, 12:59, Brendon Muir <bren...@spikeinsights.co.nz> wrote:
> Hi Ivan, yep I have tried that code (though thank you for pointing out the
> attribute thing. I'm a prototype boy so I'm still getting used to what's
> different in jquery, I had a feeling there was a 'good' way to set
> attributes.
> The ajax call returns some json:
> {"href":"4-spike/eportfolios/8-new-eportfolio","id":112}
>
> and this code is executed on a successful return:
>
> success : function (data, textStatus) {
> NODE.id = data.id;
> NODE.children("a:eq(0)").attr("href", data.href);
> TREE_OBJ.rename(NODE);
>
> }
>
> I call the rename because I custom-set the name of the node when it's
> created. When the href line is there the rename function never gets called.
>
> Actually I keep forgetting Safari doesn't tell me the JS errors, I tried in
> Firefox and got:
>
> NODE.children is not a function
>
> also TREE_OBJ.rollback is not a function
>
> That's for the rollback problem. I thought that perhaps it's because I use
> the prototype libraries too so I disabled those and it makes no difference.
>
> Anyway, I hope i've given you enough information :) Please let me know if
> you require any more :D
>
> Cheers,
>
> Brendon
>
> 2009/10/21 vakata <ivan.bozha...@gmail.com>

Brendon Muir

unread,
Oct 22, 2009, 12:04:30 AM10/22/09
to jst...@googlegroups.com
Thanks Ivan, sorry I just assumed they would be extended :) It works great now.

The rollback still doesn't work however, here's the whole callback:

oncreate : function (NODE, REF_NODE, TYPE, TREE_OBJ, RB) {
NODE = jQuery(NODE);
REF_NODE = jQuery(REF_NODE);
jQuery.ajax({ type : "POST",
 url   : "<%= polymorphic_path [@parent, @eportfolio_area, @member] %>/" + TREE_OBJ.get_type(NODE) + "s",
 data : { "eportfolio_area_member_node[name]" : TREE_OBJ.get_text(NODE),
    'reference_node_id' : REF_NODE.attr("id"),
    'reference_node_type' : TYPE },
 success : function (data, textStatus) {
NODE.attr("id", data.id);
NODE.children("a:eq(0)").attr("href", data.href);
TREE_OBJ.rename(NODE);
 },
 error : function (XMLHttpRequest, textStatus, errorThrown) {
  TREE_OBJ.rollback(RB);
   },
 dataType : "json"
});
},

Any critiques of my javascript stylz would be greatly appreciated :D

Cheers,

Brendon


2009/10/21 vakata <ivan.b...@gmail.com>

vakata

unread,
Oct 22, 2009, 8:56:37 AM10/22/09
to jsTree
As of 0.9.9 rollback is invoked using:

jQuery.tree.rollback(RB);

Cheers,
Ivan
> 2009/10/21 vakata <ivan.bozha...@gmail.com>
Reply all
Reply to author
Forward
0 new messages