English 中文(简体)
正确的方式从 Javacampnit 回调中获取数据
原标题:Proper way to get data from javascript callback

我将Api服务器从一个普通的 nodejs脚本转换成一个使用明确框架来改善稳定性的服务器。

我的问题之一是 你不能从回调中返回价值 因为原来的代码还在继续...

var _json = api.query();

不知怎的,我们更喜欢.query () 返回最终结果对象, 所以我们可以把它拼凑成 JSON, 然后把它传递给 变形的 enging 如此 :

// Render the display
res.render( posts-resp , {
    title: res.app.settings.app_title,
    _use_Callback: _use_Callback,
    json: _json
});

但是, db 查询使用回调来处理查询结果, 因此无法返回这样的值。 在原始版本中, < em> db 脚本 处理完成最后视图, 但我不希望它们像这样被绑在一起 。

query: function(){

    db.query( this._buildQuery(), function(err, res){
        if(err){ throw err; }
        res.fetchAll(function(err, rows){
            if(err){ throw err; }
            db.query( SELECT FOUND_ROWS() as num, UNIX_TIMESTAMP() as query_timestamp; , function(err, cnt_res){
                if(err){ throw err; }
                cnt_res.fetchAll(function(err, cnt_rows){
                    var obj = {
                        results:rows,
                        total_results:cnt_rows[0].num,
                        current_offset:self.offset,
                        query_timestamp:cnt_rows[0].query_timestamp
                    };
                    // ... ?
                });
            });
        });
    });
}

我怎样才能妥善地获得数据并将其传送到转化系统?

问题回答

JavaScript 是一个相当灵活的语言。 您不需要直接将您的观点与 DB 代码连接到 em < / em>, 因为您可以使用一个函数作为折叠符来将视图代码从 DB 逻辑中抽取, 并保持它与 DB 逻辑的分离 。

需要非同步逻辑时, 回调可以是一个非常强大的工具。 我不确定这里是否需要回调, 但是您仍然可以使用包装器功能将视图代码与 DB 代码分开, 以包装视图逻辑 :

< 坚固 > 功能环绕视图代码:

function delegateToView(data) {

    // process data here before sending to view

    // Render the display
    res.render( posts-resp , {
        title: res.app.settings.app_title,
        _use_Callback: _use_Callback,
        json: _json
    });
}

< 加强> DB 代码, 函数调回:

 db.query( SELECT FOUND_ROWS() as num, UNIX_TIMESTAMP() as query_timestamp; , function(err, cnt_res){
                if(err){ throw err; }
                cnt_res.fetchAll(function(err, cnt_rows){
                    var obj = {
                        results:rows,
                        total_results:cnt_rows[0].num,
                        current_offset:self.offset,
                        query_timestamp:cnt_rows[0].query_timestamp
                    };
                    // ... ?

                    //call your delegator here and pass in any needed data.
                     // this keeps the actual view code separate.
                    delegateToView(obj);


                });
            });

现在,如果你的视野代码需要改变, 你不需要触及 DB 代码中的任何逻辑或回溯功能中的任何逻辑。 祝你好运!





相关问题
How to make Sequelize use singular table names

I have an model called User but Sequelize looks for the table USERS whenever I am trying to save in the DB. Does anyone know how to set Sequelize to use singular table names? Thanks.

What is Node.js? [closed]

I don t fully get what Node.js is all about. Maybe it s because I am mainly a web based business application developer. What is it and what is the use of it? My understanding so far is that: The ...

Clientside going serverside with node.js

I`ve been looking for a serverside language for some time, and python got my attention somewhat. But as I already know and love javascript, I now want learn to code on the server with js and node.js. ...

Can I use jQuery with Node.js?

Is it possible to use jQuery selectors/DOM manipulation on the server-side using Node.js?

How do I escape a string for a shell command in node?

In nodejs, the only way to execute external commands is via sys.exec(cmd). I d like to call an external command and give it data via stdin. In nodejs there does yet not appear to be a way to open a ...

热门标签