How to prevent hard-coding in angular application - angularjs

I am currently working on a angular project where I find myself injecting hard-coded values of string, numbers, etc into html template.I have tried to store hard-coded values in object fields and inject the object into whichever component needs it. However it does not seem very intuitive. Is there way to get values from some properties file like we do in Java. I would also like to know what are some of the best practices that you developers are using to prevent that?

You can do it with a properties file as well. The best way would be to create a json file, e.g., example.json and calling in the json and then just making an API call like:
$http.get('your_web_address/example.json')
.success(function(data){
// assign the data to a var or write next logic directly
})
and then use the content of json as per choice
You can make this as a service call as well, if you want

Related

SuiteCommerce Advanced - Show a custom record on the PDP

I am looking to create a feature whereby a User can download any available documents related to the item from a tab on the PDP.
So far I have created a custom record called Documentation (customrecord_documentation) containing the following fields:
Related item : custrecord_documentation_related_item
Type : custrecord_documentation_type
Document : custrecord_documentation_document
Description : custrecord_documentation_description
Related Item ID : custrecord_documentation_related_item_id
The functionality works fine on the backend of NetSuite where I can assign documents to an Inventory item. The stumbling block is trying to fetch the data to the front end of the SCA webstore.
Any help on the above would be much appreciated.
I've come at this a number of ways.
One way is to create a Suitelet that returns JSON of the document names and urls. The urls can be the real Netsuite urls or they can be the urls of your suitelet where you set up the suitelet to return the doc when accessed with action=doc&id=_docid_ query params.
Add a target <div id="relatedDocs"></div> to the item_details.tpl
In your ItemDetailsView's init_Plugins add
$.getJSON('app/site/hosting/scriptlet.nl...?action=availabledoc').
then(function(data){
var asHtml = format(data); //however you like
$("#relatedDocs").html(asHtml);
});
You can also go the whole module route. If you created a third party module DocsView then you would add DocsView as a child view to ItemDetailsView.
That's a little more involved so try the option above first to see if it fits your needs. The nice thing is you can just about ignore Backbone with this approach. You can make this a little more portable by using a service.ss instead of the suitelet. You can create your own ssp app for the function so you don't have to deal with SCAs url structure.
It's been a while, but you should be able to access the JSON data from within the related Backbone View class. From there, within the return context, output the value you're wanting to the PDP. Hopefully you're extending the original class and not overwriting / altering the core code :P.
The model associated with the PDP should hold all the JSON data you're looking for. Model.get('...') sort of syntax.
I'd recommend against Suitelets for this, as that's extra execution time, and is a bit slower.
I'm sure you know, but you need to set the documents to be available as public as well.
Hope this helps, thanks.

How to call Data Editor from Code, C1 CMS?

Under definition tree, I can attach event to open Data Editor for certain element.
However, I have no idea how to trigger the workflow to open the Editor for a given id.
I have a custom action URL which point to a custom ASPX (List of businesses about 50,000) in the console.
Now I want to trigger call the default data editor from the custom ASPX. Is that possible.
Yes you can. There is a few steps you need to take, but then you can basically mimic all console behaviors from your own aspx-page.
First of all you need to include the necessary C1 Console javascript frameworks on your page. Do that by calling ScriptLoader.Render("sub") and write the result on your page. You would also need to include this javascript on your page https://github.com/burningice2866/CompositeC1Contrib/blob/master/Teasers/Package/content/Composite/InstalledPackages/CompositeC1Contrib.Teasers/teaserConsoleFunctions.js - its some wrapper methods which are able to construct the javascript objects needed to call C1's webservice methods.
When that is setup you can trigger opening a Edit Workflow by calling the executeAction javascript method via a link containing the necessary ActionToken and EntityToken like this https://github.com/burningice2866/CompositeC1Contrib/blob/master/Teasers/Web/UI/TeaserHtmlHelper.cs#L65.
Its important to understand that everything in the C1 Console in based on these tokens. You don't edit something based on its id but always based on the EntityToken the something is represented by.
And what you want to do, whether its edit something or delete it, that thing you want to do is represented by an ActionToken. So to be able to edit something, you need an EntityToken for that something and a ActionToken for the Data Editor.

What is the best way of querying data with JSON object (through MongoDB) from AngularJS

I have generated a Mean stack app with the angular fullstack generator. In the generated parts of the project, querying data from angular controllers is achieved through a REST api. A typical get endpoint looks like this:
GET /api/things
And this api can be used directly from controller like this:
$http.get('/api/things', { params : {'name': 'xxx'}})
With this approach, i can query data just with string parameters. When i want to query data using a JSON object like this one {'age': {"$gte": 18}}, it does not work since get only support strings as parameters. Now i am not sure what is the best way of querying data with JSON objects. I now have these options in my mind, but i am not sure which may work, or which way i should choose:
1- $http.get('/api/things', { params : encodeURIComponent(JSON.stringify{'age': {"$gte": 18}})})
using this encoding i can pass the JSON object as string. But on the server side i am not sure how i can handle this string. req.query or JSON.parse(req.query) did not work.. This cannot be the right thing to do anyway, since it looks like a hack.
2- Creating another GET api like this:
/api/things/older
calling this api:
$http.get('/api/things/older', { params : {'age': '18'}})
and at the server side implementing the Things.find({'age': {"$gte": req.query.age}})
This is of course not a REST api then.. I don't know if it works at all
3- I have seen some projects where some factories/services are implemented (i have not used one yet). They are injected to controllers, and can be used like this:
var things = Things.query();
However i also don't know whether what i want can be achieved with this approach or this is the right way to do, since as i heard we should only be using our REST api(?).
So the question: What is the best way of querying data with JSON object (through MongoDB) from AngularJS?
Edit: For the 1. option: when i pack my encoded object in another key like this:
$http.get('/api/things', { params : {myQuery : encodeURIComponent(JSON.stringify({'date': {"$gte": new Date().setHours(0,0,0,0)}}))}})
and unpack on server like this:
Thing.find(JSON.parse(decodeURIComponent(req.query.myQuery)))
it works. But it somehow still does not sound right to me.

ng-repeat convert resource objects to simple objects

Iam getting my data with help of the Angular's $resource service as array. Each element of that array is an Resource-Object. So i can use methods like $save and $update of these Objects. In a view i represent my array with the help of the ng-repeat directive like:
<div ng-repeat="appointment in object.appointments" ng-click="editAppointment(appointment)">
And here i get in trouble. The appointment-Object i get in the editAppointment-Method is a simple Object. No Resource Object anymore. So i cant use the helpfull methods like i mentioned above.
$scope.editAppointment= function(appointment){
console.log(appointment); // > Object
console.log(object.appointments); // > Array of Resource
}
Have somebody noticed that problem too? May its a bug, but i cant imagine that.
Assuming your resource class is called Appointment, you should just be able to do:
$scope.editAppointment= function(appointment){
new Appointment(appointment).save();
}
Presumably your Appointment resource looks something like the following (i.e. it correctly maps some sort of id property from existing objects to the URL parameters):
var Appointment = $resource('/appointment/:appointmentId', {appointmentId:'#id'});
This would be the case if your appointment objects (i.e. the underlying JSON objects handled by your API) have an ID property called id. If it's called something else (or if there are multiple path variables in your URL) you'll just need to change the second argument to map all of the properties of the objects being saved (the values starting with '#') to the URL path variables (the things starting with ':' in your URL).
See where they save a new card in the credit card example here: http://docs.angularjs.org/api/ngResource.$resource. The fact that they're dealing with a totally new object and that you're trying to save an existing one is irrelevant. Angular doesn't know the difference.

Grab a variable from the URL and then pass it to a controller CakePHP 2.0

I'm trying to build a sort of Wordpress-esque CMS system to a project I'm building. I want the user to be able to create pages on the fly, and for them to appear in certain areas of the website.
I have made something similar in Symfony2, where the controller grabs a specific variable from the URL (as definded in the route.yml file, usually $id etc) and I then use the variable in the controller to display whatever content it relates to in the database.
However, I'm not used to CakePHP 2.0, and struggling to find what I need. I know it's possible, but I don't know the best way to achieve it. Especially as CakePHP uses a different routes file than Symfony.
How would I grab a variable from the URL and pass it for use inside a controller?
By variable do you mean GET query string parameters, like in /foo?key=value? You can access them in the controller through the request object: $this->request->query['key'].
If you are looking for something more integrated you can use CakePHP's default routes or make your own.
The default routes work with URLs like /controller/action/param1/param2 and pass the parameters to the action by position. For instance /posts/view/521 maps to a call to view(521) in PostsController, and /posts/byMonth/2012/02 maps to a call to byMonth("2012","02").
You can also use named parameters and the URLs look like /controller/action/key1:value1/key2:value2. In controller actions you would read them with $this->params['named']['key1'].
With custom routes you can make your URLs anything you want. You're not forced to the /controller/action pattern; you can make /archives/2012-02 map to PostsController::byMonth(2012,2), or have /512-post-title map to PostsController::view(512).
Typically you would start out with the default routes and add custom routes when you decide you need them. You can read all about the default and custom routes in http://book.cakephp.org/2.0/en/development/routing.html
Short answer: it depends.
If you're looking to pass parameters to a function, you don't need to mess with routes at all; every non-named URL path segment is processed in order and handed to the action as method parameters (so /controller/action/1234 passes "1234" as the first parameter to the action method in the controller controller class).
Named parameters allow you to pass parameters anywhere in the URL string, and to make them optional. The form is just key:value and they're accessed via $this->params['named'] in the controller.
The last option is prefix routing. The best place to get to speed on that is naturally the CakePHP Cookbook, but the general gist is that in a route definition, you can name a path component in the URL by prefixing it with a colon, identically to how the default routes show :controller, :plugin and :action. You can define any name you like, even optionally applying regex pattern requirements and then access it through $this->params['variablename'] in the controller.

Resources