【问题标题】:ext js - create custom function with passed parametersext js - 使用传递的参数创建自定义函数
【发布时间】:2016-08-03 07:35:34
【问题描述】:

只是一个警告,这是我的第一个 ExtJS 项目。 我从网络服务器成功加载了两个商店。

  1. 包含职位的商店
  2. 包含 marketData 的商店

我创建了第三家商店来保存我的所有结果。 现在我想遍历每个仓位,找到相关的市场数据记录,然后进行简单的计算。

我在点击按钮的情况下成功完成了这一切,但我想分离出进行实际计算的功能......传入参数。

现在只是为了让这个概念发挥作用,我创建了一个名为“sayHello”的函数,但我收到一条错误消息... ReferenceError: sayHello is not defined。

谁能指出我在创建这个自定义函数时做错了什么?

谢谢!

我的控制器...

Ext.define('ExtApplication1.view.clientdetails.clientdetailsController', {
extend: 'Ext.app.ViewController',
alias: 'controller.clientdetails-clientdetails',

onClickCalculate: function () {
    console.log('calculation button was hit');

    var targetGrid = Ext.getCmp('positionsGridID');
    var positionsStore = targetGrid.store;

    var marketDataGrid = Ext.getCmp('marketsGridID');
    var marketDataStore = marketDataGrid.store;

    var calculatedPositionsDataGrid = Ext.getCmp('calculatedPositionsGridID');
    var calculatedPositionsDataStore = calculatedPositionsDataGrid.store;
    console.log(calculatedPositionsDataStore);

    positionsStore.each(function (record) {

        console.log('the details for the whole position');
        console.log(record);

        var bbSymbol = record.get('BBSymbol');

        var singleRecord;
        marketDataStore.each(function (record) {
            var cycleBBSymbol = record.get('BBSymbol');
            if (cycleBBSymbol === bbSymbol){
                singleRecord = record;
                return false;
            }
        });

        console.log('position I am evaluateing is ' + bbSymbol);
        console.log('market data found for  ' + singleRecord.get('BBSymbol'));
        console.log(singleRecord);

        //debugger;
        var lastPrice = singleRecord.get('Last_Price');
        var settle = singleRecord.get('Px_Settle');
        var qty = record.get('Quantity');
        var marketName = record.get('Description');
        var pnl = (lastPrice - settle) * qty;
        console.log(pnl);

        calculatedPositionsDataStore.add({
            BBSymbol: bbSymbol,
            Description: marketName,
            Quantity: qty,
            CalcPLSett: pnl
        });

        sayHello(singleRecord);

    }, this);



},

sayHello: function (singleRecord) {
    alert('hello');
    alert(singleRecord);
}

});

【问题讨论】:

    标签: javascript extjs mvvm


    【解决方案1】:

    您收到此错误是因为您超出了 ViewController 的范围。

    positionsStore.each(function (record) { ...}
    

    你在 store 范围内,但是 sayHello 函数在 ViewController 范围内。

    将 ViewController 的作用域分配给一个变量,应该可以解决您的问题:

    onClickCalculate: function () {
        console.log('calculation button was hit');
    
        var me = this; //NEW LINE
    
        var targetGrid = Ext.getCmp('positionsGridID');
        var positionsStore = targetGrid.store;
    

    然后在positionsStore.each函数中使用:

    me.sayHello(singleRecord)
    

    【讨论】:

    • 不确定我是否应该在这里问这个.. 但我的 store1/store2 概念是创建一个 store3。这是一个很好的方法吗,还是有我遗漏的最佳实践?
    猜你喜欢
    • 2015-08-17
    • 1970-01-01
    • 2013-01-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-28
    相关资源
    最近更新 更多