sounds to me you are referring to AJAX?
how to refresh blade view when a new item is saved
I can put a listener in the controller
Order::saving(function($model)
{
var_dump('order was saved');
});
and if I use postman and save a new order i get string 'order was saved' (length=14)
But i have to go and press reload button to update the view in the browser. How will I get this done programmatically so that the user don't need to hit reload button for showing the newly added order
I am a newb, so bear with me. What would be the js function I put in the view and how does that get called
To clarify further, right now I have the following in the blade file
@foreach ($orders as $order)
<tr >
<td {{$order->body}}</td>
</tr>
@endforeach
My case is not when I am clicking a button, but rather data is posted by someone else and saved to the database, and when that happens it should update my view
So, whenever relevant data is persisted, you want to update every user's list of orders? Is that right? If so, you'll want to use something like Pusher.
Pusher is awesome sauce, but if you want to do your own, ember or angular can also achieve what you are looking for.
If you work with commands and events you could manage with some listener that responds after a saved entity event and then make an ajax response to update the entire page or some part of it.
If user A post's order on one screen and if B logins as A in another screen his list of orders (which is actually A's orders) should get updated as A posts from another connection. I don't know if this is the same scenario as @JeffreyWay pointed out
@codeeatbusiness would you be kind to expand / explain at bit more
Hi @vichu, I didn't need to make it before, but I will try to explain to you.
Did you see the Command and Domain Events Series? If you did it, in this serie Jeffrey commented about Entities, Events, Dispatcher and Listeners, and in your request, you need to respond to an event with this behaviour, refreshing your blade view.
If you need to refresh your blade completely, you only need to redirect in your event listener, as in the series you can find the EmailNotifier listener, you could create a custom RefreshListener that listen your EntityUpdated or EntitySaved event and then refresh your web o web-part.
Using sub-views you could update only it using Ajax, and when you run your custom controller method for save your entity and respond with your event, the listener will be expecting to update your subview.
You can find about Ajax refreshing process here: http://stackoverflow.com/questions/15096241/laravel-how-to-render-only-one-section-of-a-template
Tip. Here you can find Model Observers: http://laravel.com/docs/master/eloquent#model-observers
The problem that I find in your Model Observer that it doesn't need to know about the application response, but if you want to use your EntityObserver into your Entity::observe() function in a Service Provider, you can make it.
Hope it helps you.
ok, could i use something like below, where i wrap the list in a div (funky). What i don't understand is how would i implement the @foreach on the right side of .getElementById
$(document).ready(function(){
setInterval(function(){
document.getElementById("funky").innerHTML =
}, 2000);
});
Do you know about the following jQuery function?
http://api.jquery.com/jquery.each/
But would be useful to know how is your blade structure to give you a better solution about your request.
Hope it helps you.
@codeeatbusinness I did watch that series -- my understanding is that commander cleans up stuff (big deal as code gets larger). However I am trying to make use of Laravel's built in events fire when saving to db(again Laracast pearl!). I will clean up once I get this working. I am not worried at the moment about efficiency from partial refresh. Just refreshing the whole blade view with the updated order would be great! So I had this in the controller.
Order::saving(function($model) { Redirect::route('orders_path');
});
and in routes.php
Route::get('orders', [ 'as' => 'orders_path', 'uses' => 'OrdersController@showOrders' ]);
I thought this would do it, but no luck. (the listening part is just fine, when i put a var_dump in front of it does show up) Redirect does not appear to be the right approach since this is not after a event initiated by the same user. Refer to my comment about users A & B above Updating every 2 seconds would be a poor solution, nonetheless 'a' solution if i could get that to work.
@JeffreyWay any insights, is my situation the same as the one you pointed out and needing Pusher (please refer to comment above with users A and B logging into same account on different screens)
As far as the jQuery .each if i put it like
document.getElementById("funky").innerHTML = $.each($orders, function( index,$order ) {
{{$order->body}};
});
it blows up since it cannot find $order -- that is passed via ->with from controller and is caught in view by the @foreach but how will the jQuery function get it in blade?
Please or to participate in this conversation.