Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do something when Ember component is instantiated?

Tags:

ember.js

I call a component like this:

  {{Gd-text-input label="Specify" name="Specify" key="entry.810220554" triggerKey="tada" hideIf="Client"}}

I would like to run some javascript-code that sets an additional property to this component.

What I'm trying to run is something like this

//Convert string ot array.
GdRadioInput = Em.Component.extend({
    init: function(){
        var cs = this.get('contentString');
        console.log('doesthiswork?');
        if(cs){
            this.set('content', eval(cs));
        }
    }
});

But it doesn't run. If someone could just provide a sample that console.logs a the value of a property of a component whenever that component is created, that would be very helpful.

like image 411
Himmators Avatar asked May 14 '26 22:05

Himmators


2 Answers

You can run this code in the init method

init:function(){
    this._super();
    hideIf = this.get('hideIf');
    key = this.get('key')
        if(hideIf === key){
              this.set('class', 'hide');
        }
    }

Good luck

PD: now this method is private: http://emberjs.com/api/classes/Ember.Component.html#method_init

like image 155
Edu Avatar answered May 18 '26 04:05

Edu


I know this is an old question, but I wanted to update it with the new way to do things. Instead of overriding the init function, you can now cause a function to run on initialization with .on('init'). Example:

GdRadioInput = Em.Component.extend({
    setupFunc: function(){
        var cs = this.get('contentString');
        console.log('doesthiswork?');
        if(cs){
            this.set('content', eval(cs));
        }
    }.on('init')
});
like image 27
lsowen Avatar answered May 18 '26 02:05

lsowen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!