Skip to content

Instantly share code, notes, and snippets.

View k1r0s's full-sized avatar

Ciro k1r0s

View GitHub Profile
@k1r0s
k1r0s / Invoice.js
Created March 21, 2017 14:57
This is an example of what we normaly do in OOP
class Invoice {
...
doCheckout(shippingDetails){
if(this.validate(shippingDetails)){
this.shippingDetails = shippingDetails
var serializedInstance = JSON.stringify(this)
$OurAjaxService.post({
url: this.url,
data: serializedInstance
}).then((response) => {
@k1r0s
k1r0s / kaop-showcase.js
Last active March 22, 2017 07:42
kaop showcase about advice placements
var Dummy = Class({
someMethod: [ //decoratedMethod
"subscribe","$inject", //befores
function($$dep1){
//method body
},
"trigger: 'action'", //afters (advice with an argument)
],
anotherMethod: function(){
/* method without advices */
var Advices = require("kaop").Advices;
Advices.add(
function log(){
//meta.args contains the arguments {array}
console.log(meta.methodName + " called");
console.log("with arguments: " + meta.args);
console.log("returned: " + meta.result);
}
)
Controller = Class({
constructor: function(app){
app.get('/', this.home);
app.get('/login', this.login);
app.use(this.notFound);
},
login: [function(req, res){
//what ever
}, "log"],
home: [function(req, res){
@k1r0s
k1r0s / Root.main.tsx
Created June 3, 2017 07:19
Handle exceptions in React components
import * as React from "react";
import Sidebar from "../Sidebar/Sidebar.main";
import Content from "../Content/Content.main";
import { Advices } from "../../advices/Advices"
import { onException, afterMethod } from "kaop-ts"
export default class Root extends React.Component<null, null> {
@onException(Advices.blameCovfefe)
@k1r0s
k1r0s / Root.main.tsx
Created June 3, 2017 14:45
kaop-ts onException show case
import * as React from "react";
import Sidebar from "../Sidebar/Sidebar.main";
import Content from "../Content/Content.main";
import { Advices } from "../../advices/Advices"
import { onException, afterMethod } from "kaop-ts"
export default class Root extends React.Component<null, null> {
@k1r0s
k1r0s / Advices.ts
Created June 3, 2017 14:51
capturing exceptions with kaop-ts
import { AdvicePool, adviceMetadata, IMetadata } from 'kaop-ts'
export class Advices extends AdvicePool {
static blameCovfefe (@adviceMetadata meta: IMetadata) {
meta.exception.message += " despite the constant negative press covfefe"
}
static throwOnError (@adviceMetadata meta: IMetadata) {
if(meta.exception) {
throw meta.exception
@k1r0s
k1r0s / Covfefe.component.tsx
Created June 3, 2017 14:52
Return a React component within an advice
import { AdvicePool, adviceMetadata, IMetadata } from 'kaop-ts'
import { Covfefe } from './covfefe-components'
export class Advices extends AdvicePool {
static blameRussia (@adviceMetadata meta: IMetadata) {
if(meta.exception) {
meta.result = <Covfefe/>
}
}
}
@k1r0s
k1r0s / metadata-show-case.ts
Created June 3, 2017 14:54
kaop-ts IMetadata property
export class Registry extends AdvicePool {
 static log (@adviceMetadata meta: IMetadata) {
meta.args // Arguments to be received by decorated method
meta.propertyKey // Name of the decorated method as string
meta.scope // Instance or the context of the call stack
meta.rawMethod // Original method (contains metadata)
meta.target // Class definition
meta.result // The returned value by the method
 }
}
@k1r0s
k1r0s / viewAsync.ts
Created June 3, 2017 14:56
kaop-ts Async operations within the same call-stack
import { beforeMethod } from 'kaop-ts'
import { PersistanceAdvices } from './persistance-advices'
import { FlowAdvices } from './flow-advices'
import { OrderModel } from './order-model'
class View {
@beforeMethod(PersistanceAdvices.read, OrderModel)
@beforeMethod(FlowAdvices.validate)
update (data?) { ... }
}