-1

While i am trying to create object like this

new Ext.TitleCheckbox () 

I am getting "not a constructor error"

my Object is

Ext.TitleCheckbox = {

    checked:false,
    constructor : function() {
    },
    getHtml : function (config) {
        var prop = (!config.checked)?'checkbox-checked':'checkbox-unchecked';
        var html = config.title+'<div class="'+prop+'" onclick="Ext.TitleCheckbox.toggleCheck(this)">&#160;</div>';

        return html;
    },

    toggleCheck : function (ele){
        if(ele.className == 'checkbox-checked') {
            ele.className = 'checkbox-unchecked';
        }
        else if(ele.className == 'checkbox-unchecked') {
            ele.className = 'checkbox-checked';
        }

    },

     setValue : function(v){
        this.value = v;
     },

     getValue : function(){
        return this.value;
     }

};

whats the mistake in here?

2 Answers 2

2

Ext.TitleCheckbox is not a function, you cannot make a function call to an object literal.

If you want to use the new operator, you should re-structure your code to make TitleCheckbox a constructor function.

Something like this (assumming that the Ext object exists):

Ext.TitleCheckbox = function () {
  // Constructor logic 
  this.checked = false;
};

 // Method implementations
Ext.TitleCheckbox.prototype.getHtml = function (config) {
  //...
};

Ext.TitleCheckbox.prototype.toggleCheck = function (ele) {
  //...
};

Ext.TitleCheckbox.prototype.setValue = function (v) {
  //...
};

Ext.TitleCheckbox.prototype.getValue = function () {
  //...
};
Sign up to request clarification or add additional context in comments.

Comments

0

See CMS's answer for why. As a work-around, if you really need to do this, you can do it via inheritence. In javascript Constructors inherit from objects (a constructor is just a function). So:

function MyCheckbox () {} ; /* all we really need is a function,
                             * it doesn't actually need to do anything ;-)
                             */

// now make the constructor above inherit from the object you desire:

MyCheckbox.prototype = Ext.TitleCheckbox;

// now create a new object:

var x = new MyCheckbox();

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.