I am able to find much material on articles about private and protected variables in javascript. But what about methods?
May you give an example on how to properly extend the class interface with some protected method?
Related
I am trying to understand decorators which are not java's annotations but more like pre-processors in this article and I also found this SO question on setting info on a function. Let's say I have an interface like so
export default interface UserService {
#Path("/users/create")
#POST
createUser(user: CreateUserRequest): Promise<CreateUserResponse>;
#Path("/users/delete")
#POST
deleteUser(user: DeleteUserRequest): Promise<DeleteUserResponse>;
}
sidenote: It would be great to use this generated client in react UI as well as use in nodejs
I want my #Path and #POST, #GET to save info I can read and I think
class Path(path:string):
def __init__(self, path):
self.path = path
def __call__(self, func):
func.pathAnnotation = self
return func
I read I cannot loop over methods in an interface yet I would like to generate the http client that implements this interface so any API developers create, it automatically creates the implementation. (In java, we use the Proxy.java to generate an implementation). On server side, the controller implements the same exact API and I like to generate the 'scaffolding' meaning the http request to which endpoint to call if possible (not sure I can do that in nodejs just yet).
EDIT: An idea: Perhaps, I create an abstract class or class and every method throws an exception "Use XXXFactory to generate the implementation of this class". How do I loop over method in that class? and can I create classes that extend all these 'apis' at runtime such that I can inject him for everyone to use(like Proxy.java in java world)?
EDIT: perhaps I could do this in javascript with a prototype class somehow... generate all the methods to call a method with signature
Promise<Object> invoke(String method, object[] paramsToMethod);
Then, I can look up the method and #Path and #GET/#POST properties on that method. Can a prototype class extend a class in javascript such that any api defined (perhaps defined in typescript) is implemented by this one class? Then I have every api implemented by the same code for every microservice like we do in java? (This then means the platform team can swap protocols and developers do not care about protocols anymore like http, http2, binary, etc)
As per request, I have took some time to tinker around with reflection. The reflection is mostly needed to make the client automatically conform to what the service expects (type of parameters/return type), and I think it might be possible with reflect-metadata.
Ok so the idea is to have decorators store metadata about the method in a map, where a key is the class and the value is a collection of the methods with metadata.
Then where you get the client, it aggregates all the metadata for each method into one function that can be used.
It's a vague start but I think it can work. I actually might also turn this into a small snippet or library too if I have the time.
But actually, this should be a statically generated client, not a dynamic one. Because then it is far easier to validate and do the code generation.
Playground
I was talking with a coworker about private methods and he came to the conclusion that we should not use private methods inside an angular #Component since it is harder to unit test.
For me, is not a valid argument to not make it private so it will be easier to test. I think that private is to make it clear your intention about that method, that is used only in that class and is not called elsewhere. He said that, for angular components, its clear that you only going to use in that scope, in that page template. So the benefit of making clear (using private) is not as good as make it easier to test (using public).
I google for any recommendations about not using private methods in #Components but I couldn't find any.
So I was wondering if would be a good approach if all methods of a #Component are public (even if not needed to be) to make it easier to test?
I read it on mdn.
The original is:
The static keyword defines a static method for a class. Static methods are called without instantiating their class and are also not callable when the class is instantiated.
I thought static in js is similar to static in java but the sentence in question confuses me.
It is similar. However, Java allows calling static methods on the instance:
p1.distance(p1, p2);
which does the same thing as
Point.distance(p1, p2);
The first one is not allowed in JavaScript.
Here's link from Ember API. Why is private. That's mean, that I shouldn't use it in my production?
You're misinterpreting the meaning of private, the Ember API page is referring to what functions the Ember API can call within your code. The definition of public and private methods is the same in every programming language, and their scope is also similar:
private - These methods can only be accessed within the method's class, for example, you cannot call the private method named transitionTo from the Ember.ArrayProxy class. You can however call transitionTo from a function or procedure inside the Ember.Route class.
public - These methods can be accessed from anywhere in your code, for example you will be able to call the public method named addObserver from any other class, hence the name 'public'
It should also be noted that use of some methods are discouraged; such methods are prefixed with an _ underscore.
These methods may be outdated, or generally be unsafe to use.
Thanks to Daniel for suggesting I add that in.
I am working with asp.net mvc4. I have an action method which is a get method as it returns a an mvc view that is called from JavaScript/Jquery.
The javascript makes a get request to the controller action. I have a json object that I would like to pass to the mvc action. But as its a get request, as I need to return a view from it, it is posing difficult.
Is it possible to pass a json object that maps to an object on the signature of the controller action? I was looking libraries that may help such as http://benalman.com/projects/jquery-bbq-plugin/
Any tips or ideas?
You dont need any external APIs to do that. All you need to do is make an c# object with the same property and name as the JSON Object.
For E.g. Create an Object as
public class MyObject{
public string MyProperty1{get; set;}
public string Property2{get; set;}
}
And creating object in the jquery is all correct i assume and use jquery syntax like
as below
$.get('#Url.Action("MyAction","MyController")',
{myObject: jqueryObject},
function(resp){$("#myDiv").innerHTML(resp);});
And in case of Action use function declaration like
public ActionResult MyAction(MyObject myObject)
{
// your code here
return PartialView("_MyPartialView", myModelForPartialView);
}
BTW this way works for both Get and Post.
you could use something like servicestack http://www.servicestack.net/ this takes care of everything, and does it very well, as an alternative there is the MVC API options, but servicestack is better at this.
I am sure if you look at this project, you will either get great ideas from it, or learn to decouple your data driven code, away from your UI project.