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

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';

Related

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.

Get edited data of OData Binding

Does anyone know the best approach of getting the changed data of an odata model property after binding it to a form / set of input controls via bindElement?
Currently I am doing something like this.getView().bindElement('/SomeEntitySet(0815)');. But I struggle with getting back the edited data from my view. The only methods the binding is offering to me is getting back the odata model itself, which contains the original, non edited data.
Does anybody know what I'm doing wrong?
In the odataModel there's an Property called refreshAfterChange this should be set to true by default.
Are you sure your changes applies to your backend data? The oDataModel from sapui5 should update the binding by itself(checkout: mParameters.refreshAfterChange).
If the Change method work debug your oData service and check if the change method trigger a GET request to your backend(The odatamodel should trigger a get request if the data has been changed => see refreshAfterChange in link above, the refresh of the model will do a GET request like it does if you bind data to an element). If not theres something wrong with your model confoiguration.
Could you add the code where and how you initiate the model?

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 }}

can I pass data when routing in angular

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.

Backbone doing POST request instead of PUT request

I am trying to set a model's attribute to be something different. When I save the model, Backbone issues a POST request instead of a PUT request.
I read on another Stackoverflow post that Backbone uses a model's id to determine if a model is new or not. When I console.log(model), the model has an id attribute. When I console.log(model.id), it prints out undefined. How come when I do a console.log(model), the model has a id attribute, yet when I do console.log(model.id), I get back undefined?
Which way does Backbone use to determine if a model is new or not? And, how can I fix the problem? I am using backbone-tastypie.
How did you create the model in the first place? The model's id needs to be specified in the model's data. Usually this would be returned in the server response the first time you save a new object. Once the id is set Backbone will save it with PUT instead of POST.

Categories

Resources