Chaplinjs Composition with two different Site View - chaplinjs

I have to two different Site Views with different regions.
How can I in proper way write composition in beforeAction method ?
What I want
beforeAction: function(params, route) {
if (route === 'sessions#new') {
this.reuse('site', OutsideSiteView);
this.reuse('header', OutsideHeaderView);
this.reuse('footer', OutsideFooterView);
} else {
this.reuse('site', SiteView);
this.reuse('header', HeaderView);
this.reuse('footer', FooterView);
}
}

My solution was
beforeAction: function(params, route) {
if (route === 'sessions#new') {
this.reuse('site', OutsideSiteView, { outside: true });
this.reuse('header', OutsideHeaderView, { outside: true });
this.reuse('footer', OutsideFooterView, { outside: true });
} else {
this.reuse('site', SiteView);
this.reuse('header', HeaderView);
this.reuse('footer', FooterView);
}
}
But I am not sure that it is proper solution

Related

Simplified difference between Strategy and State pattern

i am learning patterns and i got confused when i came across state and startegy pattern. A simple googling took me to this stact overflow blog which made to get confused even more.
My simple quesiton is,
strategy pattern
public interface Istrategy {
void algorithm();
}
public class add : Istrategy
{
public void algorithm()
{
Console.WriteLine("addition");
}
}
public class multiply : Istrategy
{
public void algorithm()
{
Console.WriteLine("multiply");
}
}
public class subtract : Istrategy
{
public void algorithm()
{
Console.WriteLine("subtract");
}
}
public class context {
public context(Istrategy strategy) {
strategy.algorithm();
}
}
public class client {
static void Main() {
new context(new add());
Console.ReadLine();
}
}
state pattern
public interface Istate {
void algorithm();
}
public class add : Istate
{
public void algorithm()
{
Console.WriteLine("addition");
}
}
public class multiply : Istate
{
public void algorithm()
{
Console.WriteLine("multiply");
}
}
public class subtract : Istate
{
public void algorithm()
{
Console.WriteLine("subtract");
}
}
public class context {
Istate state;
public context(Istate state) {
state.algorithm();
}
}
public class client {
static void Main() {
new context(new add());
Console.ReadLine();
}
}
in the above example, as per my understanding, i would take this as, passing a object to the context without any state saved is the strategy pattern and keeping the sate in the context is the state pattern.
is my understanding right?
UPDATE 1
interface Istate
{
void action();
}
class walk : Istate
{
public void action()
{
Console.WriteLine("walking !!!");
}
}
class run : Istate
{
public void action()
{
Console.WriteLine("running !!!");
}
}
class fly : Istate
{
public void action()
{
Console.WriteLine("flying !!!");
}
}
class context
{
Istate state;
public void statePattern()
{
for (var i = 0; i < 3; i++)
{
if (i == 0)
{
state = new walk();
state.action();
}
if (i == 1)
{
state = new run();
state.action();
}
if (i == 2)
{
state = new fly();
state.action();
}
}
}
}
public class client
{
static void Main()
{
new context().statePattern();
Console.ReadLine();
}
}
so as per my understanding, is this approach a state pattern and the approach below a strategy pattern?
interface Istate
{
void action();
}
class walk : Istate
{
public void action()
{
Console.WriteLine("walking !!!");
}
}
class run : Istate
{
public void action()
{
Console.WriteLine("running !!!");
}
}
class fly : Istate
{
public void action()
{
Console.WriteLine("flying !!!");
}
}
class context
{
public void statePattern(Istate state)
{
state.action();
}
public void startegy()
{
for (var i = 0; i < 3; i++)
{
if (i == 0)
{
statePattern(new walk());
}
if (i == 1)
{
statePattern(new run());
}
if (i == 2)
{
statePattern(new fly());
}
}
}
}
public class client
{
static void Main()
{
new context().startegy();
Console.ReadLine();
}
}
please let me know, thank you
This technical difference is not wrong but I think that you don't use the correct approach to capture the difference between these two patterns.
State and strategy are classed as behavioral design patterns.
Which brings them together but conceptually these are really two distinct things.
In your example and generally speaking, choosing an algorithm type is a way of proceeding and should be so considered as a strategy.
Trying to convert it into a state makes no sense.
A state pattern allows an object to alter its behavior when its internal state changes.
And your algorithms don't have any internal state changes.
These are distinct strategies. And your prove it as you never change from a state to another.
Trying to understand the difference between the two design patterns with the same use case can only finish as trying to put a circle in a square and does not allow a good understanding.
If you want really to stay in the algorithm domain, you could use a related use case.
For example in a family of algorithms, you can have simple of them (as which one defined in your question) and more complex that contain an internal state and change their behavior according to this state.
The state pattern could be useful to implement these algorithms.
But you can also rely on a completely different example to understand the state pattern.
Here is a question about state pattern I answered where the matter is modeling the washing machine, a good candidate to understand the state pattern.

is there a way to use dependencies injection using typescript

i'm relative new to this, so i want to implement dependency injection using typescript (is the first time I use this pattern), I'm more that using language programming like java or c# for OOP, so there is more easy to apply this pattern,
I found an example on internet and I can use it without problems on eclipse and visual studio, but when i use it on typescript the IDE raise an error like this:
Supplied parameters do not match any signature of call target
and is just at the end of implement it when this error appears
my base class:
class Motor {
Acelerar(): void {
}
GetRevoluciones(): number {
let currentRPM: number = 0;
return currentRPM;
}
}
export {Motor};
my class that uses motor
import { Motor } from "./1";
class Vehiculo {
private m: Motor;
public Vehiculo(motorVehiculo: Motor) {
this.m = motorVehiculo;
}
public GetRevolucionesMotor(): number {
if (this.m != null) {
return this.m.GetRevoluciones();
}
else {
return -1;
}
}
}
export { Vehiculo };
my interface and the type of motor
interface IMotor {
Acelerar(): void;
GetRevoluciones(): number;
}
class MotorGasoline implements IMotor {
private DoAdmission() { }
private DoCompression() { }
private DoExplosion() { }
private DoEscape() { }
Acelerar() {
this.DoAdmission();
this.DoCompression();
this.DoExplosion();
this.DoEscape();
}
GetRevoluciones() {
let currentRPM: number = 0;
return currentRPM;
}
}
class MotorDiesel implements IMotor {
Acelerar() {
this.DoAdmission();
this.DoCompression();
this.DoCombustion();
this.DoEscape();
}
GetRevoluciones() {
let currentRPM: number = 0;
return currentRPM;
}
DoAdmission() { }
DoCompression() { }
DoCombustion() { }
DoEscape() { }
}
and here is where the error appears:
import { Vehiculo } from "./2";
enum TypeMotor {
MOTOR_GASOLINE = 0,
MOTOR_DIESEL = 1
}
class VehiculoFactory {
public static VehiculoCreate(tipo: TypeMotor) {
let v: Vehiculo = null;
switch (tipo) {
case TypeMotor.MOTOR_DIESEL:
v = new Vehiculo(new MotorDiesel()); break;
case TypeMotor.MOTOR_GASOLINE:
v = new Vehiculo(new MotorGasoline()); break;
default: break;
}
return v;
}
}
I don't wanna use any library or module like SIMPLE-DIJS or D4js or any other for the moment, I just wanna know how to implement without them
You have this error because you don't specify a constructor on the Vehiculo type.
To declare a constructor you should use use the constructor keyword and not the name of the class.
class Vehiculo {
private m: Motor;
constructor(motorVehiculo: Motor) {
this.m = motorVehiculo;
}
public GetRevolucionesMotor(): number {
if (this.m != null) {
return this.m.GetRevoluciones();
}
else {
return -1;
}
}
}

Angular2 injected service going undefined

I was following this tutorial and reached the below code with searches wikipedia for a given term. The below code works fine and fetches the search result from wikipedia.
export class WikiAppComponent {
items: Array<string>;
term = new Control();
constructor(public wikiService: WikiService) { }
ngOnInit() {
this.term.valueChanges.debounceTime(400).subscribe(term => {
this.wikiService.search(term).then(res => {
this.items = res;
})
});
}
}
But when I refactored the and moved the code for search to a separate function it is not working. this.wikiService inside the search function is going undefined. Can you throw some light on why it is going undefined?
export class WikiAppComponent {
items: Array<string>;
term = new Control();
constructor(public wikiService: WikiService) { }
search(term) {
this.wikiService.search(term).then(res => {
this.items = res;
});
}
ngOnInit() {
this.term.valueChanges.debounceTime(400).subscribe(this.search);
}
}
You are having a scope issue, "this" inside your callback is not refering to your page. Change your function callback like this:
this.term.valueChanges.debounceTime(400).subscribe(
(term) => {
this.search(term);
});

How to use dynamic Type Hints in Laravel method

Basically I want to make code generic and use different Services upon given Route parameters.
What is the proper and working way to achieve this?
The following works:
Routes:
Route::get('socialmediaAccount/authorize/twitter', function(TwitterApi $client){ return ['Works'];});
That works as well:
Routes
Route::get('socialmediaAccount/authorize/twitter', ['uses' => 'SocialmediaController#authorizeAccount']);
Controller
class SocialmediaController extends Controller
{
public function authorizeAccount(TwitterApi $client)
{
return ['Works as well'];
}
}
Now I want to add facebook, the idea is this:
Routes
Route::get('socialmediaAccount/authorize/{type}', ['uses' => 'SocialmediaController#authorizeAccount']);
Controller
class SocialmediaController extends Controller
{
public function authorizeAccount($type)
{
if($type == 'twitter') {
$client->call TwitterApi-method(); //????????????
return ['???'];
}
if($type == 'facebook') {
$client->call FacebookApi-method(); //????????????
return ['???'];
}
}
}
Since this didn't work I tried and failed with following:
Now in the definition of my Controller method I can't use the Type Hint anymore, and if I try to create separate methods ala authorizeTwitter I can't call it with Type Hint. I tried following:
Routes - is the same
Controller
class SocialmediaController extends Controller
{
public function authorizeAccount($type)
{
if($type == 'twitter') {
$this->authorizeTwitter();
return ['???'];
}
if($type == 'facebook') {
$this->authorizeFacebook();
return ['???'];
}
}
private function authorizeTwitter(TwitterApi $client) //????????????
{
call TwitterApi-method();
}
private function authorizeFacebook(FacebookApi $client) //????????????
{
call TwitterApi-method();
}
}
The error here is 'Argument 1 passed to ... must be an instance of ... TwitterApi, none given.
You can't use dependency injection on methods that you call manually. Only on route methods, because route methods will be resolved automatically.
This said the following should work:
class SocialmediaController extends Controller
{
public function authorizeAccount($type, TwitterApi $twitterClient, FacebookApi $facebookClient)
{
if($type == 'twitter') {
// do Twitter authorization using $twitterClient here
}
if($type == 'facebook') {
// do Facebook authorization using $facebookClient here
}
}
}

how to correctly extends the class from the library Otto?

I am use Otto library in my project. And me need any functionality from this library wherein not.I want to do so:
Bus.post(MessageType.USER_SIGN_UP_SUCCESS, user);
and in my method realise do so:
#Subscribe({MessageType.USER_LOGGED_IN_SUCCESS, MessageType.USER_SIGN_UP_SUCCESS})
public void getUserFromServer(User user) {
,,,,,,,,,,,,,,
}
for this I had to copy all Otto classes from githab, and change them. I could not implement from Otto because some variables private.
and changed the access modifiers in Bus class and extends from it.
public class SkipBus extends Bus {
public void post(MessageType messageType, Object event) {
if (event == null) {
throw new NullPointerException("Event to post must not be null.");
}
enforcer.enforce(this);
Set<Class<?>> dispatchTypes = flattenHierarchy(event.getClass());
boolean dispatched = false;
for (Class<?> eventType : dispatchTypes) {
Set<EventHandler> wrappers = getHandlersForEventType(eventType);
if (null == wrappers || wrappers.isEmpty()) {
continue;
}
dispatched = true;
for (EventHandler wrapper : wrappers) {
Subscribe annotation = wrapper.method.getAnnotation(Subscribe.class);
boolean isFounded = false;
MessageType messageTypes[] = annotation.value();
for (MessageType type : messageTypes) {
if (type == messageType) {
isFounded = true;
break;
}
}
if (isFounded) {
enqueueEvent(event, wrapper);
}
}
}
if (!dispatched && !(event instanceof DeadEvent)) {
post(new DeadEvent(this, event));
}
dispatchQueuedEvents();
}
}
but for this I had to copy all the classes in my project.
tell me how can I make it easier? or tell me another library that can do what I want
Actually Otto was designed to separate the message type using Object's type.
#Subscribe
public void eventReceived(UserSignUpEvent user) {
}
#Subscribe
public void eventReceived(UserLoginEvent user) {
}
or
MainActivity.java
#Subscribe
public void eventReceived(User user) {
if (user.getMessageType() == MessageType.USER_SIGN_UP_SUCCESS) {
}
}
SecondActivity.java
#Subscribe
public void eventReceived(User user) {
if (user.getMessageType() == MessageType.USER_LOGIN_SUCCESS) {
}
}
You have no need to separate the MessageType like this (and you should not). I suggest you to change the code design pattern to what that Otto is designed for. Otherwise, you have to copy the whole Otto source code and edit like you are currently doing.
Otto itself is pretty simple so there's no need to extend it. You just create event and event handler for it. Event can be any object thus you can do something like:
public enum LoginType {
SUCCESS,
FAILED
}
In your login processor:
private void login(...) {
// process login
// if everything is ok
if(...) {
bus.post(LoginType.SUCCESS);
} else {
bus.post(LoginType.FAILED);
}
}
And handle this event wherever you need:
#Subscribe
public void onLogin(LoginType loginType) {
switch(loginType) {
case SUCCESS:
// do what you need
break;
case FAILED:
// show an error or something
break;
}
}
So no need to extend library, you just have to design your events in a proper way.

Resources