How to write test cases for a function calling another function in qunit? - qunit

I have a function A which looks like this in controller1.
This function calls another function B in controller 2.
Can someone help me write qunit for this ?
A: function(){
//This function calls function B in controller2.
controller2.B(a);
}

Related

how do i show paginate links when i have called it from another function laravel

I have this function from where i call another function in services. I have paginated in that function. now the thing is the number of data is as I have decaled but I can't just use links in view.
function example (){
$a = $this->exampleService->paginationfunction($all,12);
return view->(['a'=>$a])
}
in example service:
function paginationfunction($all,$paginate){
$x = Users::where('address',$all->name);
return $x->paginate($paginate);
}
now in view i have used $a->links() which shows error of
Method Illuminate\Database\Eloquent\Collection::links does not exist.

Model function call in a Template

I have model called Page and view called view.blade.php
//this is a model
public function Test()
{
return 'test';
}
//this is the template
<h1>{{$Test}}</h1>
how can I do this? please help me?
As I understood, you want to call model function inside your view? Do it like this:
{{Page::Test()}}
Edit:
If you need to use $this in your function to pass some data for your function (if that was what you were asking in comments below), you can do something like this. First define your static function:
public static function getPages()
{
return [
//some logic (get all pages)
];
}
Now, let's say this function will return multiple pages. If you want to filter them, and to display only one page on your view, you can pass the id of that view as a parameter to a next function which you will then pass to your view:
public function getSinglePage()
{
return self::getPages()[$this->id];
}
Lastly, in order to display the output of that function, use the same method as above, with new function name:
{{Page::getSinglePage()}}

How to have a dynamic controller in Laravel

I'm trying to call dynamic popup views in which I need to pass the data through the controller, I want the controller to be dynamic which will access the particular function and make the view accordingly. Basically i'm looking for something like this:
Route::post('/popup/{id}', 'PopupController#{$id}');
So basically suppose when it is called like this: mydomain.com/popup/id1, it should call PopupController#id1.
Help me out with this.
You need a method that will fire the appropriate function
Route::post('/popup/{id}', 'PopupController#dispatch');
In PopupController
public function dispatch($id)
{
return $this->$id()
}
then if your $id is someFunction you need to make sure your controller has function someFunction() method
I suggest instead of writing a dynamic route or controller use switch case in controller action.
e.g.
Route::post('/popup/{id}', 'PopupController#action');
In Controller
public function action($id)
{
switch($id)
{
case 1: ...
case 2: ...
}
}

Problems in throttle function in js

below 'throttle' code works good. But my question is that why we are using 'this' keyword in throttle function.
what is it actually?
please describe it.
thank you very much
$("document").ready(function(){
$("input").keypress(throttle(function(e){
$(".div1").html($("#ip").val());
},1000))
function throttle(fn,dly){
var timer=null;
return function(){
clearTimeout(timer);
timer=setTimeout(function(){
fn.apply(this,arguments);
},dly);
}
}
});
fn is a function. You can call the function in the standard way - fn() or you can call the call method or apply method on it.
When calling call or apply you have the option with the first argument to set the scope the function can be called in. The scope can also be set to null for no scope.

define simple function to use in all view (laravel4.2)

i have a getCategory function in my controller:
function getCategory($id){
$category=UserBlogCategory::find($id);
return $category->name;
}
and i want to use to my view:
echo getCategory($user_blog->category_id);
and i get this error:
Call to undefined function getCategory()
i already put getCategory in view and it was worked, but i want to use myfunction in any part of my project.
That function must be in your model as static function, then write use of your model in the view and call it with mymodel::

Resources