build API upon existing API

时间:2015-05-12 23:17:43

标签: javascript node.js mongodb

I have several controllers in my application that the only purpose is to send specific data that is taken from the MongoDB for my views, and I created a little plugin to not have to use every time that ugly and unreadable mongoose code in all controllers that i need access the db and take something:

function TForm1.FindControlAtPoint(aParent: TControl; aPos: TPointF): TControl;
var
  I: Integer;
  Control, ChildControl: TControl;
  S: String;
begin
  Result := nil;

  // Check all the child controls and find the one at the coordinates
  for I := aParent.Controls.Count – 1 downto 0 do
  begin
    Control := aParent.Controls[I];
    S := Control.ClassName;
    if Control.PointInObject(aPos.X, aPos.Y) then
    begin
      ChildControl := FindControlAtPoint(Control, aPos);
      if Assigned(ChildControl) and ChildControl.HitTest then
        Exit(ChildControl)
      else if Control.HitTest then
        Exit(Control);
    end;
  end;
end;

So I created this plugin that uses Promises. This is an example code:

MyModel.find (_id: req.params.id, etc ....

My question about this is simple: Is this stupid? I made this thinking about organization and not performance..

I've done courses of all kinds, and most teachers always taught to care for the readability of the code ..

1 个答案:

答案 0 :(得分:0)

这是在您的应用程序中使事情变得更简单的完全有效的方法。如果你正在使用一个体面的承诺库(我知道bluebird是一个顶级的竞争者),你不应该在性能方面失去任何东西。

所有API都是根据其他API实现的。它的API一路向下......

(例外情况并不特别值得注意。)