can I pass data when routing in angular - javascript

I want to pass a lot of data (json) when routing to a new controller in angular.
In controller A I call $location.path('/B'); which in turn will route to controller B.
I know I can pass parameter in the url itself, but I have a lot of data.
Can angular do something similar to 'POST' method and pass data in this way?

No need to bother with POST like behavior with angular.
You have several ways to do this :
use a service that will preserve data across page loads
pass real GET argument (when the page is specifically linked to this argument, for example an object ID used to display this object details)
store data in local storage / session storage
use controller 'resolve' functionnality to fetch new data before displaying page (not what you want to do though...)

Remember you're not actually changing the page, so you don't need to 'POST' data anywhere or do anything similar.
Instead you should create a service that makes that data available through dependency injection, then specify the dependency when you instantiate the controller that handles the new route.

Related

Error in calling Filter URL odata service sapui5

I have to pass two filter parameters to ODATA service. I am trying it as follows:
new ODataModel("proxy/http/FIORI-DEV.abc.com:8000/sap/opu/odata/sap/Z_FI_PAY_D_SRV/PdetailSet?$filter=Laufi eq '"+spayid+"' and Laufd eq '"+spaydt+"'?$sap-client=100",
It is giving following error.
GET http://localhost:63655/Payment1/proxy/http/FIORI-DEV.abc.com:8000/sap/opu/odata/sap/Z_FI_PAY_D_SRV/PdetailSet/$metadata?$filter=Laufi%20eq%20%27U2-28%27 400 (Bad Request)
Log-dbg.js:456 2020-07-21 16:40:39.956774 [ODataMetadata] initial loading of metadata failed -
Can anyone suggest correct way to add filter parameters on ODATA service. Thanks.
When you create a new OData model, then you can not provide any filters. You have to provide the base URI of the service.
Filtering by value happens automatically by the UI5 framework based on a context binding.
You create an OData model with new ODataModel("proxy/http/FIORI-DEV.abc.com:8000/sap/opu/odata/sap/Z_FI_PAY_D_SRV/") (or even better: you define your model using your manifest.json).
You assign your model to your view with view.setModel
Then you create property bindings on controls of your view (which you would usually do in the XML definition, but you can also do it in code)
You bind an element on your view. This is where the filtering happens. The UI5 framework looks at the binding path you provided to controlWhichShowsTheThing.bindObject and will perform an HTTP request to the OData model to retrieve that object when required.
Now you might ask "OK, so how does that binding path look"? This actually depends on the oData model itself and how it addresses individual objects.

ember adapter pass id

I want to be able to pass an id to rest point while using ember data. My end-point looks like v3/enterprise/inventory/items/{id}/links. I want to inject the id while making the request such as this.store.findAll('each-item-links', { id: itemId }). However, it does not work. I extended the Ember REST adapter and override the namespace but nothing seems to be working.
If you're trying to request a single record through Ember Data, then you want to use findRecord instead of findAll.
Also, if you need control over how the URL is built (what you have there looks like it might not map to the RESTAdapter too cleanly) you can override the _buildURL method to change the URL that the request is sent to. It is given the ID from findRecord so you can generate the URL whatever you want. Technically this is "private API" but I wouldn't worry too much about overwriting that.
Edit: To avoid using private API, there is also a public buildURL method that can be used instead.

How to pass data to one module to another module in angularJS

In my project I have two ng-app's one is for login page and another for home page.
Once login is successful for the user, in ajax success callback I got the some response related to that user and in that callback I am used the window.location="home.html".
As of now I used session storage feature to pass the data to home page.
The best way to do it is by creating a service. The service can be injected into your controllers and other services as necessary.
Create a service and use getter and setters.When you get a response in your first module set the object using setter and get the data using getter in another module.
The best way to communicate between two controllers or say two modules is using services.
According To angularjs examples only one module stands for one project though outof the box you can you can use more than one
but To Achieve this .This Is how i Normally model myproject
**MyProjectModule** (only **one** module)
---> loginController.js (initial page)
(If LOGIN IS Authorised Then Redirect To Desktop->desktopController.js)
---> desktopController.js
---> customerController.js
---> salesController.js

How can I change query params without a controller in Ember?

I have a callback from a library which returns data that I need to use to update query params. The problem is that this callback has no reference to any Ember structure or data. Is there a way to get access to the current controller to do controller.set(param, value) or perhaps a way of doing Ember.transitionTo({param: value})?
You don't need Ember to update the URL, you can do it from anywhere and Ember will detect the change and update the query parameters on the corresponding controller automatically. So in your case, just update the URL and Ember will know what to do. I was able to do this and it worked fine:
window.location.search = '?key=newvalue';

How to make ember component fetch data from server. put AJAX call inside the component seems not a good practise to handle this

An Ember Component needs to fetch data from serve, however i think put AJAX call inside the component is not a good practise.
Or use the Route to fetch data, then pass data to component.But the route's method can't share easily between different routes.
In general, you are right, it is not a good idea to put ajax calls in components. However, in a case where the data to be retrieved and displayed is intimately connected to the view--auto-completion could be one example--it should not be considered an anti-pattern.
If you think it is important to segregate the ajax call, you could consider using a {{render}} helper inside the component's template, and do the ajax work in a separate controller with an associated view where the results are displayed. Routes are not really relevant here because they are related to navigation and URLs.
A component should depend only on the input that are passed to it.
If component has some internal dependency (AJAX / Other data) for input it is an anti pattern.
Ember way will be
1) Create a Route : (Get data to you application)
Sometime you dont have a route for such data (like in your case). Here you can use application route or any other parent route if have. Use the setupController to inject this data to relevant controller
2) Pass down data to component
Now your data should be a controller. Pass this data like any other to required component
{{my-comp-here data="here" ajaxData=fromRoute }}

Categories

Resources