2

嗨 Stackoverflow 人!

我在 sencha touch 框架中编码,我已经定义了对 Ext.data.proxy.Sql 的覆盖......

当应用程序加载时,显示下一个错误:

Uncaught TypeError: Object App.data.proxy.CustomSql has no method 'apply' ...

覆盖是:

Ext.define('app.data.proxy.CustomSql', {    
    override: 'Ext.data.proxy.Sql',
    isSQLProxy: false, 
    setSql: function(sql) {
        this.customSql = sql;
        console.log('definiendo');
    },
    getSql: function() {
        return(this.customSql);
    },
    selectRecords: function(transaction, params, callback, scope) {
        console.log('seles');
        var me = this,
        table = me.getTable(),
        idProperty = me.getModel().getIdProperty(),
        sql = 'SELECT * FROM ' + table,
        records = [],
        filterStatement = ' WHERE ',
        sortStatement = ' ORDER BY ',
        i, ln, data, result, count, rows, filter, sorter, property, value;


        result = new Ext.data.ResultSet({
        records: records,
        success: true
    });


    if (!Ext.isObject(params)) {
        sql += filterStatement + idProperty + ' = ' + params;
    } else {
        ln = params.filters && params.filters.length;
        if (ln) {
            for (i = 0; i < ln; i++) {
                filter = params.filters[i];
                property = filter.getProperty();
                value = filter.getValue();
                if (property !== null) {
                    sql += filterStatement + property + ' ' + (filter.getAnyMatch() ? ('LIKE \'%' + value + '%\'') : ('= \'' + value + '\''));
                    filterStatement = ' AND ';
                }
            }
        }


        ln = params.sorters && params.sorters.length;
        if (ln) {
            for (i = 0; i < ln; i++) {
                sorter = params.sorters[i];
                property = sorter.getProperty();
                if (property !== null) {
                    sql += sortStatement + property + ' ' + sorter.getDirection();
                    sortStatement = ', ';
                }
            }
        }


        // handle start, limit, sort, filter and group params
        if (params.page !== undefined) {
            sql += ' LIMIT ' + parseInt(params.start, 10) + ', ' + parseInt(params.limit, 10);
        }
    }


    console.log(this.customSql);        
    if (this.customSql) {
        sql = this.customSql;
        delete this.customSql; 
    }


    transaction.executeSql(sql, null,
        function(transaction, resultSet) {
            rows = resultSet.rows;
            count = rows.length;


            for (i = 0, ln = count; i < ln; i++) {
                data = rows.item(i);
                records.push({
                    clientId: null,
                    id: data[idProperty],
                    data: data,
                    node: data
                });
            }


            result.setSuccess(true);
            result.setTotal(count);
            result.setCount(count);


            if (typeof callback == 'function') {
                callback.call(scope || me, result);
            }
        },
        function(transaction, errors) {
            result.setSuccess(false);
            result.setTotal(0);
            result.setCount(0);


            if (typeof callback == 'function') {
                callback.call(scope || me, result);
            }
        }
    );
},

});

任何想法?欢迎任何帮助。

提前致谢。;)

4

1 回答 1

1

浏览您的代码:

  1. selectRecords() 函数中,您有很多,而不是;

    selectRecords: function(transaction, params, callback, scope) {
        console.log('seles');
        var me = this,
        table = me.getTable(),
    ...
    

    你正在混合;,
    这是一个函数,你应该总是把它放在;行尾。

    正如@StephenTremaine亲切地指出的那样(不知道我是如何忽略这一点的),上面不是一个问题,因为人们可以简单地使用逗号来生成一个声明列表。

  2. ,在最后一个 Ext.define 右括号之前有一个});

        },
    });
    

    这是错误的。

试图清理和修复您的代码:

Ext.define('app.data.proxy.CustomSql', {
    override: 'Ext.data.proxy.Sql',
    isSQLProxy: false, 
    setSql: function(sql) {
        this.customSql = sql;
        console.log('definiendo');
    },
    getSql: function() {
        return(this.customSql);
    },
    selectRecords: function(transaction, params, callback, scope) {
        console.log('seles');
        var me = this;
        table = me.getTable();
        idProperty = me.getModel().getIdProperty();
        sql = 'SELECT * FROM ' + table;
        records = [];
        filterStatement = ' WHERE ';
        sortStatement = ' ORDER BY ';
        i, ln, data, result, count, rows, filter, sorter, property, value;


        result = new Ext.data.ResultSet({
            records: records,
            success: true
        });


        if (!Ext.isObject(params)) {
            sql += filterStatement + idProperty + ' = ' + params;
        } else {
            ln = params.filters && params.filters.length;
            if (ln) {
                for (i = 0; i < ln; i++) {
                    filter = params.filters[i];
                    property = filter.getProperty();
                    value = filter.getValue();
                    if (property !== null) {
                        sql += filterStatement + property + ' ' + (filter.getAnyMatch() ? ('LIKE \'%' + value + '%\'') : ('= \'' + value + '\''));
                        filterStatement = ' AND ';
                    }
                }
            }

            ln = params.sorters && params.sorters.length;
            if (ln) {
                for (i = 0; i < ln; i++) {
                    sorter = params.sorters[i];
                    property = sorter.getProperty();
                    if (property !== null) {
                        sql += sortStatement + property + ' ' + sorter.getDirection();
                        sortStatement = ', ';
                    }
                }
            }

            // handle start, limit, sort, filter and group params
            if (params.page !== undefined) {
                sql += ' LIMIT ' + parseInt(params.start, 10) + ', ' + parseInt(params.limit, 10);
            }
        }


        console.log(this.customSql);
        if (this.customSql) {
            sql = this.customSql;
            delete this.customSql; 
        }


        transaction.executeSql(sql, null,
            function(transaction, resultSet) {
                rows = resultSet.rows;
                count = rows.length;


                for (i = 0, ln = count; i < ln; i++) {
                    data = rows.item(i);
                    records.push({
                        clientId: null,
                        id: data[idProperty],
                        data: data,
                        node: data
                    });
                }

                result.setSuccess(true);
                result.setTotal(count);
                result.setCount(count);

                if (typeof callback == 'function') {
                    callback.call(scope || me, result);
                }
            },

            function(transaction, errors) {
                result.setSuccess(false);
                result.setTotal(0);
                result.setCount(0);

                if (typeof callback == 'function') {
                    callback.call(scope || me, result);
                }
            }
        );
    }
});
于 2013-11-03T20:04:03.183 回答