How to trigger action on route from controller in ember - ember.js

Considering this example: http://emberjs.jsbin.com/hecewi/1/edit?html,js,output
The docs state that an action will be searched for on the controller first (which works in the example e.g. by pasting the actions hash into the IndexController), then on the current route and then along its parent routes until it hits ApplicationRoute. I'd expect the testCamel action in the example to be triggered then, but instead there is an error about the route did not get handled. How to do it right?

The code to trigger an action is indeed correct. It's just an unfortunate chosen example. Since your route will initialize the controller, the route itself is probably not completely initialized by the time the action is sent. If you, for example, schedule the action to be triggered in the following run loop, it works perfect:
http://emberjs.jsbin.com/yaseva/1/edit

Related

Aws lambda + api gateway: only trigger lambda by button click

I have an aws lambda function which runs some python code (which is calling an external api with some extra functions). I attached a trigger, an api gateway, which if I go to the url, the lambda functions runs correctly.
However, I want the lambda function only to be run if they click on a button somewhere on the 'website' of the api-url. Rephrased otherwise, I want the api-url page to have a button, which on click executes the lambda function.
Im think that should be quite easy, however I can't figure out how. Due to the information overload I can't seem to find the right video, document example on how to do it.
Is the possible? (running the lambda function on click of a button). If so, is there some good documentation of example of this?
I tried to add a button on the api-url page by, on 'method-execution' on the 'api-gateway resource' page, changing the mapping template to text/html instead of json. (a bit like in https://blog.it-playground.eu/display-html-page-using-only-api-gateway/) But then i can't figure out how to run the lambda function onClick of the button. ==> Is this the right start?
(Because its not really a coding issue, I can't really provide any code).
Also, is this question so simply that it shows I simply don't understand the basics enough (and should subsequently go over them again)?
Of course this is possible. API Gateway exposes REST API. All you need to do is to create some resource and method in API Gateway such as
GET /posts
attach your lambda function to it, and hit that API endpoint with some ajax request from your front end (via fetch, axios, ...) that would be executed when the button is clicked. Something like:
button.addEventListener("click", () => {
fetch("https://my-api-gateway-url/posts").then() ...
}
You can process the response data in .then part but don't forget that this is asynchronous coding so you need to handle it as such.

Item updated via CSOM will not fire remote event receiver

We have a remote event receiver associated to a list and hooked on all events there. When you update any list item using OOB SharePoint page, the event receiver is executed; a web service which is taking care of the afterward actions works nicely. However when you update item use CSOM code e.g. in simple console application, nothing happens. The event receiver is not called at all. I found this issue on both SP 2013 and 2016.
I will not post any code while it is irrelevant: item is updated using standard approach and values are actually changed in the list item, only the event receiver is not fired. To put it simply:
item updated manually from site -> event receiver fired
item updated via CSOM -> event receiver not fired.
I remember similar issue on SharePoint 2010 when using server side code and system account. Could it be that behind the scene web service called by CSOM (e.g. list.asmx) is using system account to make changes as well? It's just hypothesis...
So after deeper investigation and many try/fails we found out it was indeed issue with code in our event receiver. For some strange reason original developers were checking Title field in after properties and cancelling code if not present. I guess it was probably an attempt to prevent looping calls.
One lesson learned: When using CSOM after event properties contains only those fields which were altered by CSOM code. Keep it in a mind in case you need to use other values than those you want to update. You may need to stupidly copy and assign them again just because of this.

Call an action only once when it's called from two places

I have an app we have connected to Pubnub for a live socket service to keep data on the page fresh for the user.
I have an ajax call that will do something with our API, and when it is successful I call an action on the application controller. At or around the same time, as long as Pubnub is still connected it receives a message with the action handler name and it attempts to call the same action.
Ideally I want to make sure this code only runs once weather it was first called by Pubnub or by my ajax success callback. How can I do this maybe using the ember run loop? It seems viable here I'm just not able to wrap my head around how I would actually do this.
Well, I would only use the web socket.
But for your question:
There is not build-in functionality in the runloop to do that. You will need some kind of uniq message id, and then have a list of processed messages and check there before you run your code.

Transition to previous route on error action ember.js

I have the following situation to handle in ember :-
I have to transition to a route which has a model hook. The model hook returns a promise. (Say route A -> route B, Route B has a model hook returning a promise)
While the model hook in B is running, the loading route is entered(for showing "please wait" type msg to user).
In case the model hook in B fails, I need to transition back to route A. I handle the error action in route B.
The problem is, while handling error action in route B, the previous route is not always route A. It can be from other another route too.
I tried the possible workarounds, but they didn't work out for me:-
1) Using window.history.back() - This fails because Route B isn't entered yet, because the model hook promise failed. So I get the previous route of Route A.
2) Using this.controllerFor('application').get('currentRouteName') - This gives the loading route (While transitioning from Route A to Route B, intermediate route 'loading' is rendered).
I can use a conditional check with query Params, but I feel this is not efficient as I have to check for many conditions.
I simply wish to return to the route that invoked Route B.
Pardon me if I missed out on anything.
Sample project : Twiddle
I reopened Ember.Route to catch all transitions and save the current route name in a application controller property. When error action is called, i transition to the route saved in that property. Routes with dynamic segments will need extra handling.
Another Method :
In your router.js,
var Router = Ember.Router.extend({
location: config.locationType,
didTransition : function(infos){
this._super(infos);
var appController = this.container.lookup('controller:application');
var crn = infos[infos.length - 1].name;
if(appController && crn!=='loading' && crn!=='error'){
Ember.set(appController,'myRouteName',crn);
}
}
});
I am not sure if there is any in built solution.

Fire action in Backbone View after two events have been triggered

I understand how to bind an action in a Backbone View to an event trigger but, how can you fire an action watching the condition that two precise envets have been triggered?
For that you can use jquery functionality like:
$.when(func1, func2).then(func3);
It acts like deffered and promise here.