Keeping data in-sync with server - ajax

This is my stack : Ember.js + Express/Node.js
Say i have an Endpoint as \posts, it will return an array of objects.
and i have following template named allPosts :
{{#each post in content}}
<p>{{post.body}} </p>
{{/each}}
Route:
App.AllPosts =Ember.Object.extend({
body : null
})
App.AllPostsRoute = Ember.Route.extend({
setupController : function(controller,model){
controller.set('content',model);
}
});
And controller as
App.AllPostsController = Ember.Controller.extend({
actions: {
save : fucntion(){
// Get And update data from server via ajax
}
}
});
I want to keep data in sync with data on server, for this i planned to use setInterval and call the save action above every 1000ms to update the data. But it doesn't work. i used setInterval like this
setInterval(App.AllPostsController.actions.save,3000);
I DONT want to use Ember Data. As the data is dependent on another Node app which runs server side.

You're trying to run an action on a type, not an instance of the controller. Instead you should start saving when you actually hit the route and controller, setupController is a good place to accomplish this.
App.AllPostsRoute = Ember.Route.extend({
setupController : function(controller,model){
controller.set('content',model); // in this code model would be blank, I'm assuming you're leaving out code
this.startSaving(controller);
},
willTransition: function(transition){
//if I'm leaving, this.stopSaving();
},
isSaving: false,
startSaving: function(controller){
this.set('isSaving', true);
this.realSave(controller);
},
realSave: function(controller){
if(!this.get('isSaving')) return;
Em.run.later(function(){
controller.send('save');
}
},
stopSaving: function(){
this.set('isSaving', false);
}
});

Related

How to pass variables from Controller to View without refreshing the Web Page in Laravel?

Whenever there is a call to Controller inside View, I want the Controller to return some variables to the View and do not refresh the View.
Already know:
return view("webpage" , compact('variable1', 'variable2') );
What I expect to achieve:
return compact('variable1', 'variable2');
This will not return the Webpage but only some of the variables.
Edit:
Thinking it from a complete different perspective, The question maybe rephrased as such
Is there a way to manipulate REQUEST variables of a web page from the controller? This way, i would be able to get new variables from the Controller without the web page being Refreshed.
With out much to work with, here is an example:
Ajax call:
$.ajax({
url: '/your/route/',
type: 'GET',
data: {getVariable: formInputValue},
})
.done(function (data) {
console.log(data);
})
.fail(function () {
console.log('Failed');
});
Controller Function:
public function getVariable(Request $request){
$resault = $request->getVariable;
return response()->json($results);
}
Update: as per comments on this answer try this and see if it works for you.
In your controller where you fetch records instead of using a ->get();
change it to paginate(5); this will return 5 records per page. and on your view at the bottom of your </table> add {{ $variable->links() }}

View returning in popup laravel 5.4

I am returning a view from controller, but instead of opening new page, the view is opening in popup which i assume is error message popup,. i am new in laravel.
Controller Code
public function postRegister () {
return view('front.member.registerpayment')->with('amountUSD', $data['btc_withcom']);
}
You can do this using AJAX request.
In your controller…
public function postRegister () {
// do something to get your data
return response()->json(
[
'data'=>$your_data
]
);
}
Then make a ajax request
First import jquery.
Then
$.ajax({
url: ‘your url’,
method: ‘POST’
data: ‘pass any data to controller’
success: function(data){
// invoke popup with data
// you can easily do this with jquery UI library
}
})
Here is the complete example to open dialog box.
Hint: append your data to html before invoking

Change view after make ajax call AngularJS

I am developing a single page application, with help of AngularJS and I'm new to it
maybe my question is so primitive but at the moment I don't know how to handle it
what I need to do id to open up a pop-up menu after user enters his/her username and password then make a ajax call to Server and if the user is authenticated another view(profile) is displayed.
This is my ajax call
formApp.controller('mainController',function FormCtrl($scope,$http){
$scope.processForm = function() {
$http({
method : 'POST',
url : 'login',
data : $.param($scope.formData),
headers : { 'Content-Type': 'application/x-www-form-urlencoded'}
}).success(function(data){
console.log(data);
});
};
And this is the routers and views config :
var formApp = angular.module('formApp', ['ngRoute']);
formApp.config(function($routeProvider) {
$routeProvider
.when('/', {
templateUrl : 'main',
controller : 'mainController'
})
.when('/profile', {
templateUrl : 'profile',
controller : 'profileController'
})
});
Thanks in advance,
So now in success method call route-change: return $location.path('/profile');
Also, I guess, you need some service where you will keep user's data.
Also, consider to use ngResource (or similar things) to work with REST backend (and don't keep this logic in controller).
Use modal window and inject controller into it and in resolve make the service call which contains your $http request.
see the link for modal window.
Angular-UI

Joomla 3.2, get works, post doesn't

So I have this JS code:
myClass = new Class({
initialize: function() {
this.btnSubmit = document.id('btnSubmit');
this.sendData = new Request({
"url":"/",
"method":"post",
"data": {"option":"com_my4quiz", "controller":"conduit", "task":"save", "hrdata":"foo"},
"onSuccess": this.handleResult.bind(this)
});
this.btnSubmitObserver = function() { this.sendData.send(); }.bind(this);
this.btnSubmit.addEvent("click", this.btnSubmitObserver);
},
handleResult: function(stuff) {
//do stuff
}
});
If I'm posting this to my Joomla 3.2.0 component it returns the home page. As soon as I switch to get, it sends the data to the correct place and I get what I expect.
I think its due to your controller page loads the entire view.
This may happen due to inside your controller save(). function not rendering any specific view .
So the solution is after the Ajax result just render the proper layout or just put an exit();
At the end of your save()
exit();
or
$view->setLayout($layoutName);
$view->display();
Hope its helps..

Patterns for caching/lookup of MVC partial views on client

Here's my scenario. I'm calling a url which points to a controller method that returns a partial view:
Controller method:
public ActionResult UserProfile() { return View("UserProfile"); }
Ajax request to get the view:
$.get('/Home/UserProfile', function (data) { $('.content').html(data); });
I would like to cache my "UserProfile" view so that each time the user clicks it, it doesn't have to go back to the server to fetch the view from the controller again.
I would also like to be able to determine if the view's been cached on the client before fetching it from the server, and if it has, get it out of cache and simply inject it into a div on my layout.
Has anyone done anything like this?
You could use the [OutputCache] attribute. It allows you to specify the duration for which you want the view to be cached as well as the location of the cache. For example let's suppose that you wanted to cache the results of the controller action for 30 seconds on the client browser:
[OutputCache(Duration = 30, Location = OutputCacheLocation.Client)]
public ActionResult UserProfile()
{
return PartialView();
}
Now you could trigger many AJAX requests but only one in 30 seconds will be sent to the server:
window.setInterval(function () {
$.get('/Home/UserProfile', function (data) {
$('.content').html(data);
});
}, 4000);

Resources