I'm having trouble finding an elegant solution to Enums and especially searching in Enums with AngularJS.
Let me describe a situation and how I handled it: Say I have an object called Event
This event has 2 properties : Severity and Status. Both of which are Enums, defined by ID (1, 2,3) and Title (for Severity : "Light", "Normal", "Important" and for Status : "Open", "Closed", "Pending Approval")
The Event objects that come from the Service have the ID's of the Enums, and when I want to display the object I bind with {{ severityIdToTitle(Event.Severity) }}
SeverityIdtoTitle is a method on my controller which calls a method on my service which returns the value of the Enum based on the id recieved
The problem arises when I want the user to be able to search the object through text, the AngularJS filter doesn't know the actual "string" value of the Enum, and so I reach my problem.
I know many workarounds around this, and have a few options, but I wonder what would be anelegant and clean solution to this? Did what I do complicate things and there's a better way?
Thanks guys!
Interesting question. I would create a custom filter instead of using the severityIdToTitle function. Filters are designed for formatting data to present to the user, so converting an id to a string is a good use case for one. The filter should depend on a service that knows the two-way mapping between the enum identifiers and their values. As for how to do that mapping, that is a general JavaScript question. There is one good thread about this here.
Related
I am currently looking to build out an event driven EventBridge event module (as opposed to a scheduled one, for example) for usage in our IaaC portfolio. The example code below shows how the normal event JSON pattern would look. I can obviously add a normal single variable for the "detail-type" and the "source", but can someone advise on a way to create some kind of single mapped variable that I could pass one or more parameters for the "detail" map?
event_pattern = <<PATTERN
{
"detail-type": ["Glue Job State Change"],
"source": ["aws.glue"],
"detail": {
"jobName": ["${var.env}_${var.department}_gluejob_${var.pipeline}_publish-small", "${var.env}_${var.department}_gluejob_${var.pipeline}_catalog", "${var.env}_${var.department}_gluejob_${var.pipeline}_publish-large", "${var.env}_${var.department}_gluejob_${var.pipeline}_notify"],
"state": ["FAILED"]
}
}
The need for this is that across a number of different services we may need any number of key and value parameters in "details" so some way to pass in a map which can have any number of key value pairs would be ideal. Now if this wasn't json I could use a dynamic value in TF but I am unsure what to do here with it being an inline JSON pattern.
I should also mention before someone suggests it, that I have currently just done the work around of having the event pattern passed as variable and using data and a template json.tpl file that can pass the pattern for each resource in individually for each usage of the module, but would like to know if anyone can advise on the solution I was hoping was possible above?
Any advice would be greatly appreciated!
I have some data on a model that comes in the form of a code such as "US60" and "US70".
I need to take that value and show a display value such as "US 7day/60hour" and "US 8day/70hour". I'm not sure if there is any best practices way to do this in Angular, and I'm not having much luck googling it.
What I would do is have a service that I pass in type and value, and it would return a display value, but as with many things in Angular, since this is my first Angular project, I don't know if it's a good way to do it or not.
I'm just needing to use the display value in html such as {{settings.cycle}} I am already able to access the variable, but I want to show the display value, not the actual value.
If I am getting the gist of your question correctly, you have the value available but want to alter how it is displayed on screen right?
There are two main approaches to do this in Angular, using a directive or a filter.
A filter is basically like a pipe in Unix. You can alter a value before it is being displayed. For example:
{ username | uppercase } will transform the username into an all-caps username. Naturally, you can define your own filters for your use case. Filters are mostly used to transform single values. So for your case, a filter sounds best.
A directive is commonly used to create entire components on a page. For example: <user-profile-card></user-profile-card> would be transformed, using the directive, into the appropriate html/css/logic. So these are used often for larger transformations which involve logic, like server requests. Still these directives could also be used for very small components.
So for your case, although what you are actually want to do is not completely clear to me honestly, a filter seems to be your best shot ;)
New to backbone/marionette, but I believe that I understand how to use backbone when dealing with CRUD/REST; however, consider something like results from a search query. How should one model this? Of course the results likely relate to some model(s), but they are not meant to be tied to said model(s).
Part of me thinks that I should use a collection using a model that doesn't actually sync with a data store through the server, but instead just exists as a means of a modeling a search result object.
Another solution could be to have a collection with no models and just override parse.
I assume that the former is preferred, but again I have no experience with the framework. If there's an alternative/better solution than those listed above, please advise.
I prefer having one object which is responsible for both request and response parsing. It can parse the response to appropriate models and nothing more. I mean - if some of those parsed models are required somewhere in your page, there is something that keeps reference to this wrapper object and takes models from response it requires via wrapper methods.
Another option is to have Radio (https://github.com/marionettejs/backbone.radio) in this wrapper - you will not have to keep wrapper object in different places but call for data via Radio.
I am designing a web service accepting POST requests with JSON in the message body. I want the requestor to be able to specify multiple values for a parameter, but also single values.
So, for simple cases I support JSON like:
{
"name" : "value"
}
And, in more complex cases, I also support JSON like:
{
"name" : [
"value one",
"value two",
"value three"
]
}
My question is: is this an abnormal interface for a Web Service? Am I overcomplicating things here?
The alternative would be, as I want to support arrays of values, to require arrays of values, even for the simple case:
{
"name" : [
"value"
]
}
I don't like this but want to get the community's input before making a decision.
EDIT:
I removed the word REST from the conversation as that factor isn't really important here.
It is typical of a web service to accept an array of values for a POST request. However, the number of elements may be zero or many. Keeping it as an array of values, even if only one value is going in, is going to keep things simple and consistent.
If a user of the web service needs to submit a single element for creation or modification (upsert), then perhaps a PUT request would be more appropriate.
I will prefer a single structure for both cases and then there will be only one processing logic to code and no need for special case to decide on.
Also this is not much of REST related. It is more of on trading off on the the processing or handling of the data v.s. whatever reasons that drive you to think of splitting the data into two forms in the first place.
Hope this help.
REST is about resources and their representations. So ask yourself:
What resource does the JSON represent?
Can you answer this question?
From the JSON in your question I would say, the resource is an object that has names
. It is not an object that has one name.
So I recommend to use your second approach.
I wish to generate automated messages that are associated with a finite (>20, < 100) number of types:
class Message(models.Model):
MSG_CHOICES = (
('MEETING', 'Meeting Reminder'),
('STATUS', 'Status Change Reminder'),
...
)
message = models.CharField(max_length=100)
msg_type = models.CharField(max_length=10, choices=MSG_CHOICES)
The actual message of each will depend on the type and arguments I must feed it at some point. For example, a 'MEETING' message will basically be:
"Hi, you have a meeting at %s with %s." % (time, person_to_meet)
while a 'STATUS' message will be something like
"Hi, this is a reminder that you changed your %s status to %s." % (status_type, new_status)
Now, the complexity here is that we have to render the message differently for the different types. Here's my attempt at brainstorming some different approaches:
revamp the model to have a base message model and a derived one for each, with its own type of constructor, saving the "string templates" inside the constructors for each model. This fits the pattern of separating models with different logic (and I'd usually do this for a model of different "types"), but feels clunky because besides the logic of creating the messages, these guys are basically the same. The only difference in logic comes from creating the messages themselves, and it seems like a waste to split just because of it.
keep the model as is, create class methods in the code to make a factory for each type, saving the "string templates" inside the class (or even inside the database). This is the easiest, but it feels dirty.
this is the creative/crazy one (hence the title): keep the model as is, and save the string templates as actual template files. In the constructors, use the Django template library to render those files and return the strings. This seems good because it separates out hard-coded data from code-logic, but it feels wonky since I'm using templates at two different levels of the code (one for making models and one for the views). It "feels" wrong but I can't really pinpoint why.
So the main questions:
What is the best-practice for this situation? Is it one of these or is it some other approach?
is there a "model" example of this practice somewhere? I feel lots of systems generate automated alerts/messages, so if one has particularly good code I'd like to look at it.
Thanks!
-Yan
IMHO, you shouldn't be storing the message in the db at all. You are storing the msg_type. That should really be enough. Your logic to display the information to the user (via the template) should handle this. This would allow you to localize the message if you needed to at some point in the future based on the Accept-Language header. In my experience, it's a bad idea to store user messages in the db. And, at least to my way of thinking, it's not really true business logic. It seems to belong in the UI layer. Ok. Just my opinion on it. This question is pretty old. I would be curious to read what you ultimately ended up doing here.