将插件选项注入在单独文件中定义的Hapi路由处理程序中的模式

时间:2018-10-17 10:24:11

标签: design-patterns plugins routes hapijs

我正在开发我的Hapi应用程序插件,我的路线保存在与执行插件server.route调用的文件相同的文件中。此外,这些路由的处理程序也位于单独的文件中。例如

- plugin.js
- routes.js
- handlers/
    - route1.js
    - route2.js

plugin.js类似于...。

const routes = require('./routes.js')
...
server.route(routes)

routes.js类似于...

module.exports = [
  { 
    method: 'GET', 
    path: '/route1', 
    handler: require('./handlers/route1')
  },
  ...
]

和带有签名(request, h)的处理程序文件导出功能。

我的路由处理程序(及以后)需要访问插件注册时传递的选项,但他们只能访问(request, h)

我可以使用将options带入范围的函数来包装路由定义和每个路由处理程序,但这就像定义有关项目结构的约定和民间传说,而且我感觉这可能会导致Javascript问题范围/闭合异常。

是否有一个通用的Hapi库/模式,允许我将插件选项注入到路由处理程序中,例如允许以清晰或定义明确的方式签名(request, h, options)

1 个答案:

答案 0 :(得分:0)

因此,我发现工具包对象上有可用的插件选项,如下所示...

public class FeedAdapter extends BaseCardAdapter {
private List<EventObject> ideen;

private Activity context;
@Override
public void onBindData(final int position, View cardview) {
    if (ideen.size() ==0 || ideen == null){
        return;
    }
    ImageView bild = cardview.findViewById(R.id.imageView);
    TextView name = cardview.findViewById(R.id.name);
    final EventObject eo = ideen.get(position);
    name.setText(eo.getName());
    TextView adress = cardview.findViewById(R.id.address);
     Geocoder geocoder;
     List<Address> addresses = null;
     geocoder = new Geocoder(context, Locale.getDefault());

    try {
        addresses = geocoder.getFromLocation(Double.parseDouble(eo.getLat()), Double.parseDouble(eo.getLng()), 1); // Here 1 represent max location result to returned, by documents it recommended 1 to 5
    } catch (IOException e) {
        e.printStackTrace();
    }

    String address = addresses.get(0).getAddressLine(0); // If any additional address line present than only, check with max available address lines by getMaxAddressLineIndex()
    String city = addresses.get(0).getLocality();
    String state = addresses.get(0).getAdminArea();
    String country = addresses.get(0).getCountryName();
    String postalCode = addresses.get(0).getPostalCode();
    String knownName = addresses.get(0).getFeatureName();
    String[] separatedadress = address.split(",");

    adress.setText(separatedadress[0]+" , "+city);

    TextView date = cardview.findViewById(R.id.date);
    date.setText(eo.getDate());

    Button teilnehmen = cardview.findViewById(R.id.teilnehmen);
    teilnehmen.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            String userid = FirebaseAuth.getInstance().getCurrentUser().getUid();
            DatabaseReference ref = FirebaseDatabase.getInstance().getReference().child("Users").child(userid).child("acceptedEvents").child(eo.getId());
            ref.setValue(true);
            getPLZ(eo.getId(), eo, position);

        }
    });

            bild.setImageResource(R.drawable.blau);



}

private void getPLZ(final String id, final EventObject eo, final int position) {
String userid = FirebaseAuth.getInstance().getCurrentUser().getUid();
DatabaseReference ref = FirebaseDatabase.getInstance().getReference().child("Users").child(userid).child("plz");
ref.addListenerForSingleValueEvent(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        String plz = dataSnapshot.getValue().toString();
        setParticipantInEvent(plz, id, eo, position);
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {

    }
});

}

private void setParticipantInEvent(String plz, String id, EventObject eo, int position){
    String userid = FirebaseAuth.getInstance().getCurrentUser().getUid();

    DatabaseReference ref = FirebaseDatabase.getInstance().getReference().child("Events").child(plz).child(id);
    ref.child("participants").child(userid).setValue(true);

    askForCalendar(eo, position);
}

private void askForCalendar(final EventObject eventObject, final int position) {
    AlertDialog.Builder builder = new AlertDialog.Builder(context);
    builder.setMessage("Event in deinem Kalender speichern?")
            .setCancelable(false)
            .setPositiveButton("Ja", new 
             DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {



                    Intent intent = new Intent(Intent.ACTION_EDIT);
                    intent.setType("vnd.android.cursor.item/event");
                    intent.putExtra("beginTime", Long.parseLong(eventObject.getTs()));
                    intent.putExtra("allDay", true);
                    intent.putExtra("endTime", Long.parseLong(eventObject.getTs())+60*60*1000);
                    intent.putExtra("title", eventObject.getName());
                    context.startActivity(intent);

                }
                })
            .setNegativeButton("Nein", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                //Here i have to remove one item:

                    dialog.cancel();

                }
            });
    AlertDialog alert = builder.create();
    alert.show();
        }


      }