How to inject data into Angular2 component created from a router? - components

I'm currently trying to build an Angular2 prototype (based on alpha44) of our Angular1 app (pretty complex one) and I'm trying to find the best model/data architecture when using routes and child routes.
In my example, from a child component created from a route, I want to access a property of the parent component (hosting the router-outlet).
But when you create a component from a router-outlet, you cannot use #Input and #Output anymore.
So what is the best practice to inject some data/properties, except basic routeParams and static routeData?
How do you communicate with the parent component without too much coupling?

You can use RouteData in order to pass data into routes. See here and here. I'm still missing the part of initialising this data obj from the component (see my question regarding)

A shared service can be used with components added by the router. For details see https://angular.io/docs/ts/latest/cookbook/component-communication.html
data support also was added to the new router in RC.4. For details see How do I pass data in Angular 2 components while using Routing?
Angular 2 - equivalent to router resolve data for new router might also be related.

You can pass (inject) data from child component inside Router Outlet to Parent component with Subject,Observable. You can found my solution in this video.
See the code on GitHub: https://github.com/tabvn/angular-blog

Related

Node.js express app architecture with testing

Creating new project with auto-testing feature.
It uses basic express.
The question is how to orginize the code in order to be able to test it properly. (with mocha)
Almost every controller needs to have access to the database in order to fetch some data to proceed. But while testing - reaching the actual database is unwanted.
There are two ways as I see:
Stubbing a function, which intends to read/write from/to database.
Building two separate controller builders, one of each will be used to reach it from the endpoints, another one from tests.
just like that:
let myController = new TargetController(AuthService, DatabaseService...);
myController.targetMethod()
let myTestController = new TargetController(FakeAuthService, FakeDatabaseService...);
myTestController.targetMethod() // This method will use fake services which doesnt have any remote connection functionality
Every property passed will be set to a private variable inside the constructor of the controller. And by aiming to this private variable we could not care about what type of call it is. Test or Production one.
Is that a good approach of should it be remade?
Alright, It's considered to be a good practice as it is actually a dependency injection pattern

What is a recommended way to get data into your meteor template from the front-end for a famous surface?

I've been following along with the book Discover Meteor from https://www.discovermeteor.com/ and I have built the tutorial project called 'Microscope'
This uses iron-router and Meteor templating system to render out the front-end. I want to redo this project using famo.us for the front-end but I am unclear on how I to do so.
I am aware of a package called famono. mrt add famono. Using this package I can integrate famo.us and draw surface to the screen in a meteor project. It also allows you to render templates to the screen.
But I am confused on how to redo the project so the router - routes to render a famous surface with the data.
Also I am wondering if the templates will still be reactive.
If someone could provide insight on how to redo the 'Microscope' project to use famo.us on the front-end I would greatly appreciate it!
Thanks
UPDATE (to be more specific)
I have been trying to figure out how to integrate famous with templates and routing, and I have no clue how to do it.
I use iron-router to handle my routing which chooses which template and data to render like so:
Router.map ->
#route 'posts',
path: '/',
data: ->
Posts.findOne()
So this will load up the posts template with Posts.findOne() data.
But I know with famous I can generate surfaces from templates on the front end like so:
background = new Surface
template: Template.post
data: ??? (Posts.findOne()) ???
mainContext.add(background)
Because javascript is what is going to load the final template into the view, what is the recommended way for me to get the data for that template, should I query the database from the front-end by setting up special subscriptions?
Typically I render the data into the page from the router on the server but...
with famous, I just have to load the main template and let famous load the rest of the templates. The only thing left is getting the data for the other templates. What is recommended?
I would start by looking at https://github.com/gadicc/meteor-famous-components/. That package will do all the work for you if you want.
I have never used the Surface template argument but I believe that is a one time load and will not update on data invalidation (data change).
Or u can take a look at working examples )
https://github.com/sayawan?tab=repositories

Render mobile version of login in Secure class Play! Framework

Is it possible to somehow override the login method of the Secure.java class of the Secure-Module in Play! Framework, so that another version of the login form is displayed?
In my case, i want to display a mobile version of the login-form if a mobile browser is detected.
I know i should not change the Secure.java class itself, but i don't really see any other solution to this problem.
As discussed in other posts you have the request in your Play! controller. So in this request you could ask which agent is trying to view your website:
String agentInfo = request.headers.get("user-agent");
The you can determine which template will be rendered for this agent:
if (agentType.isWhatEverHeIs) {
renderTemplate("Application\mobileTemplateForBadPractise.html");
} else {
render();
}
But what I would encourage you to do is responsive webdevelopment. Create your templates as smart as possible, let the template and css and javascript do this and keep your business logic in your controller.
You could use the Twitter Bootstrap to achieve this, but there are many more! Like Skeleton.
You even got the request object inside your templates so that you can optionally render things in your template (or not) based on the agent.
Even simpler, simply create/override the secure/login.html template and use responsive design : media queries. No need to change the controller or check agent or whatever.

Kohana children controllers with HMVC

I have a main controller and I want to call child controllers from this controller with HMVC. I setted a rule that routes parameter to a specific action that calls children controllers with "Request::factory" for the main controller but it didn't work because of infinity loop. is there any way to do it?
I must use HMVC because main controller sends some information to children controller so I need a controlling layer.
It will be kind of a plugin for crud applications for a CMS.
The main controller: http://pastebin.com/nt2fhMEy
An example of child controller: http://pastebin.com/WqaHZaxf
Route: http://pastebin.com/6JGFf2i2 (I didn't configure caction and cid yet.)
Extra Note: It will be kind of a crud module for my CMS. The main controller will load main template and some configs. Also the main controller includes some ORM functions and children controller must be able to use parent::functionname. The children controllers are in cruds/ directory and the cms creates them automatically.
You have a few mistakes in your idea of the controllers and HMVC.
The best way to do so is to use object oriented controllers. By this I mean the "child controllers" need to extend the main controller.
Let the main controllers extend Controller_Template and the child controllers extend the main controller.
When you send requests to child controllers through routes you will not only have access to the parent properties, but your request will do the following:
Before method of the main controller
Before method of the child controller.
Action of the child controller.
After method of the child controller.
After method of the parent controller.
From what I get from your question you will not need HMVC at all. Actually it's a very bad pattern for passing data. Keep in mind that when you perform an internal request through HMVC this is actually a completely new request. It will go through the routes and this list again. You will not have access to all of the previous request properties.
Another tip: don't put ORM functions in the main controller. Use the actual ORM models instead.

Kohana: Adapting Mixu's Auth Useradmin Template

I'm looking at Mixu's Auth module for Kohana 3.1 but want to implement the UI into my own site templates. At the moment my site runs properly using its own template until it gets to a restricted page. At that point it loads the useradmin module's template for logins. I'd like to just load the page components into my own template and navigation.
What is the best way to go about this please? I had imagined I would be able to arrest the flow at some point within my 'application' environment without editing the 'module' environment.
EDIT:
I'm a little further along now. I've created two Controller classes:
application/classes/controller/app.php
application/classes/controller/user.php
Each extends the module class and replaces the template reference. Eg:
<?php defined('SYSPATH') or die('No direct access allowed.');
class Controller_User extends Useradmin_Controller_User {
public $template = 'smarty:maintemplate';
}
I guess this is the right approach. I'm using Smarty Templates which is compounding the issues as I need to merge different templates. I'll keep plugging away and see how I go.
I don't know about the best way, but I had a similar situation.
In the end, I copied part of the code from the module that I needed, and rolled my own implementation of the module.
Btw. Smarty is ok, but Kostache (Mustache for Kohana) rocks. More flexible and you can use the same templates for php and javascript.

Resources