1

So, I am building a pretty complicated plugin and ran into a snag. (This is how I learn though, so snags are good...) My code looks similar to the following:

Document 1

if( !window.TextEdit ){
  var TextEdit = {"version": "1.0"};
}

TextEdit.edit = function(context, options) {
    var self = this;
    self.context = context;

    self.buttonDef = {
        bold: {
            class: 'bold',
            command: 'bold',
            icon: 'bold',
            type: 'checkbox',
            label: ''
        },
        italic: {
            class: 'italic',
            command: 'italic',
            icon: 'italic',
            type: 'checkbox',
            label: ''
        },
        underline: {
            class: 'underline',
            command: 'underline',
            icon: 'underline',
            type: 'checkbox',
            label: ''
        }
    }

    self.init();
}

Document 2

if( !window.TextEdit.imageload ){
  TextEdit.imageload = {"version": "1.0"};
}

TextEdit.imageload = function() {
    var self = this;
    self.editor = TextEdit;

    self.init();
}

TextEdit.imageload.prototype = {
    init: function() {
        var self = this;

        console.log(self.buttonDef);

        $('.tdt-btn-addimage').click(function() {



        });


    },
    create: function() {

    },
    destroy: function() {

    }   
}

new TextEdit.imageload();

So, using Document 2 I want to access the variable self.buttonDef in Document 1. I am able to access the functions in Document 1 just fine, but not the variables.

What I am looking for is how to make buttonDef a property of TextEdit.

Aaron Kurtzhals
  • 2,036
  • 3
  • 17
  • 21
watzon
  • 2,401
  • 2
  • 33
  • 65

2 Answers2

1

You can make buttonDef a property of TextEdit using the below code.

TextEdit.buttonDef = {
        bold: {
            class: 'bold',
            command: 'bold',
            icon: 'bold',
            type: 'checkbox',
            label: ''
        },
        italic: {
            class: 'italic',
            command: 'italic',
            icon: 'italic',
            type: 'checkbox',
            label: ''
        },
    underline: {
        class: 'underline',
        command: 'underline',
        icon: 'underline',
        type: 'checkbox',
        label: ''
    }
};
Aaron Kurtzhals
  • 2,036
  • 3
  • 17
  • 21
-1

I think that the problem is thatself is inside of the function edit, and so cannot be accessed at the scope of your second document. Consider having your edit function return the object self that it creates, store that as a variable, and then call var.buttonDef.

MattSmith
  • 1
  • 1
  • I'm trying to return self and access it, but I'm not getting any results. Could you give me an example? – watzon May 16 '13 at 20:17