开发者

how to access a function inside the javascript file?

I'm trying to access a function inside the js file wen a button is clicked how to access it?

Here is my html and also the code of the js file.

HTML:

<html>

<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>Picker</title>
    <link rel="stylesheet" href="resources\css\sencha-touch-debug.css" type="text/css">
    <script type="text/javascript" src="phonegap.js"></script>
    <script type="text/javascript" src="scripts\ext-touch.js"></script>
    <script type="text/javascript" src="scripts\Stocking.js"></script>
    <script type="text/javascript" src="scripts\Stockslot.js"></script>
    <script type="text/javascript" src="scripts\Datafile.js"></script>
    <script type="text/javascript" src="scripts\index.js"></script>
</head>

<body style="margin:0px; padding:0px;">
    <script>
        function xyz() {
            alert("1");
            Stocking.onShakeTap();
            alert("2");
        }
    </script>
    <button onClick="xyz();">
    <table height="1000px" width=&开发者_运维问答quot;2000px" bgcolor="#000000" cellpadding="0px" cellspacing="0px">
    <tr>
    <td>
    </td>
    </tr>
    </table>
    </button>
</body>

</html>

Stocking.js:

var addres,
    yem,
    bro,
    pro = 0;

Ext.Stocker = Ext.extend(Ext.Sheet, {
    cmpCls: 'x-picker',
    centered: false,
    floating: true,
    modal: false,
    hideOnMaskTap: false,
    draggable: false,
    monitorOrientation: true,
    hidden: true,
    arrive: 'center',
    depart: 'bottom',
    arrivalEffect: 'pop',
    departEffect: 'slide',
    height: window.innerHeight / 2,
    width: window.innerWidth,
    stretchX: true,
    stretchY: false,
    hideOnMaskTap: false,
    showDoneButton: false,
    useTitles: false,
    defaultType: 'stockslot',
    initComponent: function() {
        this.layout = {
            type: 'hbox',
            align: 'stretch',
            pack: 'start'
        };

        if (this.slots) {
            this.items = this.items ? (Ext.isArray(this.items) ? this.items : [this.items]) : [];
            this.items = this.items.concat(this.slots);

        }
        if (this.useTitles) {
            this.defaults = Ext.applyIf(this.defaults || {}, {
                title: ''
            });
        }
        this.on('slotpick', this.onSlotPick, this);
        this.addEvents('pick', 'change');

        Ext.Stocker.superclass.initComponent.call(this);

    },

    onSlotPick: function(slot, value, node) {
        this.fireEvent('pick', this, this.getValue(), slot);
        return false;
    },

    onShakeTap: function() {
        alert();
        var rand = 0;
        addres = 0;
        rand = Math.round(Math.random(4) * 5);
        this.fireEvent('pick', this, this.getValue(rand));
    },

    setValue: function(values, animated) {
        var key, slot,
            items = this.items.items,
            ln = items.length;

        if (!values) {
            for (i = 0; i < ln; i++) {
                items[i].setValue(0);
            }
            return this;
        }

        for (key in values) {
            slot = this.child('[name=' + key + ']');
            if (slot) {
                slot.setValue(values[key], animated);
            }
        }

        return this;
    },

    orient: function(orientation, w, h) {
        if (!this.container || this.centered || !this.floating) {
            return this;
        }

        var me = this,
            cfg = me.initialConfig || {},
            size = {
                width: cfg.width,
                height: cfg.height
            },
            pos = {
                x: cfg.x,
                y: cfg.y
            },
            box = me.el.getPageBox(),
            pageBox, scrollTop = 0;


        if (me.container.dom == document.body) {
            pageBox = {

                width: window.innerWidth,
                height: window.innerHeight
            };

            scrollTop = document.body.scrollTop;
        } else {
            pageBox = me.container.getPageBox();
        }

        pageBox.centerY = pageBox.height / 2;
        pageBox.centerX = pageBox.width / 2;


        if (pos.x != undefined || pos.y != undefined) {
            pos.x = pos.x || 0;
            pos.y = pos.y || 0;

        } else {

            if (/^(bottom|top)/i.test(me.arrive)) {
                size.width = me.stretchX ? pageBox.width : Math.min(200, Math.max(size.width || box.width || pageBox.width, pageBox.width));
                size.height = Math.min(size.height || 0, pageBox.height) || undefined;
                size = me.setSize(size).getSize();
                pos.x = pageBox.centerX - size.width / 2;
                pos.y = me.arrive == 'top' ? 0 : pageBox.height - size.height + scrollTop;

            } else if (/^(left|right)/i.test(me.arrive)) {

                size.height = me.stretchY ? pageBox.height : Math.min(200, Math.max(size.height || box.height || pageBox.height, pageBox.height));
                size.width = Math.min(size.width || 0, pageBox.width) || undefined;
                size = me.setSize(size).getSize();
                pos.y = 0;
                pos.x = me.arrive == 'left' ? 0 : pageBox.width - size.width;

            } else {
                size.height = pageBox.height / 2;
                size.width = pageBox.width;
                size = me.setSize(size).getSize();
            }
        }
        me.setPosition(pos);
        return this;
    },

    getValue: function(loc) {
        var value = {},
            items = this.items.items,
            ln = items.length,
            itemtest, i;

        for (i = 0; i < ln; i++) {
            itemtest = items[i];
            value[itemtest.name] = itemtest.getValue(loc);
        }
        return value;
    }
});

Ext.regModel('x-textvalue', {
    fields: ['text', 'value']
});

   

I want to access the onShakeTap() function inside the stocking.js file from the index page.


Here is some extra data:

I'm actually extending the Ext.stocker to the Ext.stockpicker and here is the code:

Ext.StockPicker = Ext.extend(Ext.Stocker, {
    slotOrder: ['valuess', 'pointss', 'stockss'],
    initComponent: function() {
        this.slots = [];
        Ext.each(this.slotOrder, function(item) {
            this.slots.push(this.createSlot(item, values, stocks, points));
        }, this);
        Ext.StockPicker.superclass.initComponent.call(this);
    },
    stretchX: true,
    createSlot: function(name, values, stocks, points) {
        switch (name) {
            case 'pointss':
                return {
                    name: 'Points',
                    align: 'left',
                    layout: 'fit',
                    data: points,
                    title: '<span style="font-weight: bold;font-size:20px;">Events</b>',
                    flex: 2
                };

            case 'stockss':
                return {
                    name: 'Stocks Available',
                    align: 'left',
                    data: stocks,
                    layout: 'fit',
                    title: '<span style="font-weight: bold;font-size:20px;">Price</b>',
                    flex: 1
                };
            case 'valuess':
                return {
                    name: 'Value',
                    align: 'left',
                    data: values,
                    layout: 'fit',
                    title: '<span style="font-weight: bold;font-size:20px;">Trading style</b>',
                    flex: 2
                };
        }
    }
});

I created an instance here like

var stockpicker = new Ext.StockPicker({});

When I use stockpicker.onShakeTap(); I'm able to manipulate the data as desired but wen I use the same stockpicker.onShakeTap(); in my html page it's not working.

Please help.


I would try something like

var myStock = new Ext.Stocker();
myStock.onShakeTap();

onShakeTap() is defined as a method of the Ext.Stocker class. I'm assuming its supposed to be a member method, not static, and so you first need to create an instance of Ext.Stocker.


Piggy-backing off of Matt's answer / comments:

When the page first loads, simply declaring what a class is does not create an instance of it. At the very start, there is no instance; it's simply a set of rules for nothing to follow. Once you create the object Ext.Stocker with mystock = new Ext.Stocker();, the instance is created and you have an object to follow the rules defined within Ext.Stocker.

function xyz(){
alert(1);
var myStock = new Ext.Stocker();
myStock.onShakeTap();
alert(2);
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜