Why can't I get anything returned when I access the self.model.get('name') or this.model.get('name') or change the value when I use this.model.set('name', 'example')?

I am trying to access self.model.get('name'), but it doesn't console.log anything.  It does console log my console.log(this).

I am ultimately trying to change the name of the value fields.

Here is what I have

<code>

({

    extendsFrom: 'RecordView',

    initialize: function (options) {
        this._super('initialize', [options]);
        
        //add listener for custom button
        this.context.on('button:print_agreement:click'this.print_agreementthis);
        this.context.on('button:billing_memo:click'this.billing_memothis);

    },
    _render: function() {
        
        this._super("_render");
        console.log(this);
        console.log(self.model.get('name'));
//        console.log(this.model.attributes['name']);
//        console.log(this.model.attributes);
    },
    print_agreement: function(){
        
    },
    billing_memo: function(){
            window.open('index.php?entryPoint=billMemo&from_portfolio_id=' + this.model.get('id'));

    },
    
})

</code>

Parents
  • Hi Amy,

    this is the variable that you should use all the time.

    There are often scenarios where when you're running code inside a callback, this referes to a different object, so people define another variable called "self" to refer to the previous object - So self is just a variable, and doesn't represent any in-built variable. In your case, you haven't defined self, so it's a null object, which is why your code stops at that line. You can just use this.model.get('name') instead.

Reply
  • Hi Amy,

    this is the variable that you should use all the time.

    There are often scenarios where when you're running code inside a callback, this referes to a different object, so people define another variable called "self" to refer to the previous object - So self is just a variable, and doesn't represent any in-built variable. In your case, you haven't defined self, so it's a null object, which is why your code stops at that line. You can just use this.model.get('name') instead.

Children
No Data