0

I want to perform click action on a displayfield in extjs. It doesn't have click listeners so i added <a></a> tags in its html as follows:

obj = {a: 123, b: 'abc' }

html: '<a href="javascript: Ext.ux.classobj.method('+obj+')" ><img src="image.png" /></a>'



The problem is I can't pass object dynamically. Means the above <a></a> doesn't call the method and fires an error as it calls :

javascript: Ext.ux.classobj.method(object Object)



However, if i use static values like

  html: '<a href="javascript: Ext.ux.classobj.method({a:123, b:'abc'})" ><img src="image.png" /></a>'

This method will be called without any error as it calls:

javascript: Ext.ux.classobj.method({a:123, b:'abc'})


Does anyone knows how to do this? Thanks a lot for help

Regards

3
  • Sounds like a scope problem. Your code defining "obj" and an "html" property seem to be from different parts of the code... it would be useful to see the context of these lines. Commented Dec 29, 2010 at 22:44
  • @typeof: It is not a scope problem. Whenever you concatenate an object with a string, the default string value for an object is [object Object] Commented Dec 29, 2010 at 22:46
  • Ah yes... couldn't see the forest for the trees. Commented Dec 29, 2010 at 22:50

4 Answers 4

1

The default toString of an object just returns "[object Object]" which (as you found out) isn't what you want.

I'd step back and ask, if you're using a framework like ExtJS which provides quite rich functionality, why are you falling back on the onclick attribute on an anchor? ExtJS (like most other JavaScript libraries) provides a means of hooking an event on an object in a more modern way, a way in which you could use obj directly.

I haven't used ExtJS in years, so I'm afraid I don't recall the direct way to do this, but I think it's either EventManager.addListener or more likely some shorthand for it like Element#on. So you'd add your anchor (or span, or whatever), and then use addListener to add a click event handler which uses obj directly. Something like:

var obj = {a: 123, b: 'abc' };
Ext.get('theId').on('click', function() {
    Ext.ux.classobj.method(obj);
});

...or, of course, simply:

Ext.get('theId').on('click', function() {
    Ext.ux.classobj.method({a: 123, b: 'abc' });
});

...but again, my ExtJS-fu is very weak these days.

Sign up to request clarification or add additional context in comments.

Comments

0

If obj is global, you have to write:

<a href="javascript: Ext.ux.classobj.method(obj)" ><img src="image.png" /></a>

If you make string concatenation, then you are getting the string value of the object which by default is [object Object].

4 Comments

What is obj changes by the time user clicks the anchor?
@Cybernate: What do you mean? obj will have the value that it has when the button is clicked.
I think he's worried about obj having changed between when he defined it and when the button is clicked.
@TJCrowder: I see.... well that depends on what the OP actually wants. Of course it can change over time.
0

Change your html assignment to:

Using Ext.util.JSON.encode:

html: '<a href="javascript: Ext.ux.classobj.method(' + Ext.util.JSON.encode
(obj) + ')" ><img src="image.png" /></a>'

Using JSON.stringify:

html: '<a href="javascript: Ext.ux.classobj.method(' + JSON.stringify(obj) + ')" ><img src="image.png" /></a>'

6 Comments

If they have a JSON library, yeah.
Agreed.Updated the post with an option using Extjs native function to stringify
Still doesn't give a reference to the object, only a JSON representation of the object at the time encode was called. Of course, the is a great answher if that is what is wanted... hard to imagine, but possible.
You can use JSON representation of the object to use the object, unless I misunderstood your question. Can you pls explain?
@Cybernate me? Sure. If you encode obj = {a:1} and then modify it with obj.b = 2; the encoded string will not contain b. When the event fires the event will receive an object like {a:1} not {a:1,b:2} since the argument was encoded to a string before b was added.
|
0

I would do something like:

var obj = { /* ...  */ };

//  ...
{
  xtype: 'displayfield',
  html: '<img src="image.png" />', // notice no <a> needed
  listeners: {
    render: function() {
      this.el.on('click', function() {
        Ext.ux.classobj.method(obj);
      });
    }
  }
}

Basically, wait for the DisplayField to render and then add the click listener to the el backing the component. As long as obj is available via closure, you're fine.

You could also use createDelegate to bind obj as the lone argument for the event handler:

this.el.on('click', 
   Ext.ux.classobj.method.createDelegate(Ext.ux.classobj, [obj]));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.