How to call livewire method in template? - laravel-livewire

In the component of Laravel 8 / livewire 2 app I selected state(code_id) and I need to preview state and I try to make it with
calling method of the component :
{{ getStateLabel($form['state_id']) }}
But I got error :
Call to undefined function getStateLabel() (View: myproject/resources/views/livewire/personal/new-item.blade.php)
In the component I have defined :
class NewItem extends Component
{
public $statesSelectionArray = [];
public function getStateLabel($state_id= 9)
{
foreach( $this->statesSelectionArray as $next_key=>$nextStateSelection ) {
if($nextStateSelection['id'] == $state_id) {
return $nextStateSelection['name'];
}
}
}
...
}
uSIALLY WHEN IN TEMPLATE i CALL COMPONENT METHOD i USE SYNTAX :
wire:click="METHODnAME(2);
But which syntax have I to use in my case ?
Thanks in advance!

Change your code like this in your blade file
{{ $this->getStateLabel($form['state_id']) }}

Related

Opencart :: is it possible to create event like change header without VQMOD or OCMOD?

I want to add one script inside the head, however using event I could not find way how it works.
I had install one event which added one script but it displays before HTML tag. But i want to display inside head tag.
extention/module/shop.php
class ControllerExtensionModuleShop extends Controller {
public function index() {
//$this->document->addStyle('catalog/view/javascript/css/test.css');
//$this->document->addScript('catalog/view/javascript/js/test.js');
echo "<script src="catalog/view/javascript/js/test.js" type="text/javascript"></script>";
}
}
Admin
class ControllerExtensionModuleShop extends Controller {
public function install() {
$this->model_setting_event->addEvent('shop', 'catalog/view/common/header/before', 'extension/module/shop/index');
}
public function uninstall() {
$this->model_setting_event->deleteEventByCode('shop');
}
Add your event with this trigger:
$this->model_setting_event->addEvent('shop', 'catalog/controller/common/header/before', 'extension/module/shop/index');
And your shop.php should be:
<?php
class ControllerExtensionModuleShop extends Controller {
public function index(&$route = '', &$data = array(), &$output = '') {
$this->document->addScript('catalog/view/javascript/js/test.js');
}
}

Laravel 5.5 - Helper class works inside view/blade template, but doesn't work inside controllers

I've created myself a helper class called Perm that is meant to return user of current session (I know, I could use default auth/user, but that wouldn't be as much of a fun as creating one from scratch!)
..sadly, the created helper class only works inside a view, but doesn't work at all in controllers.. which kinda misses the point.
Whenever I'm trying to use it inside a controller, it pops:
"Class 'App\Http\Controllers\Perm' not found"
I would most appreciate any help.
HelperServiceProvider.php:
namespace App\Providers;
use Illuminate\Support\Facades\View;
use Illuminate\Support\ServiceProvider;
class HelperServiceProvider extends ServiceProvider
{
public function boot()
{
//
}
public function register()
{
foreach( glob (app_path().'/Helpers/*.php' ) as $filename ) // register all helpers
{
require_once($filename);
}
}
}
Helpers/PermHelper.php:
use App\User;
class Perm
{
public static function user()
{
if(!session('user_id')) return null;
return User::find(session('user_id'));
}
}
Portion of config/app.php, the 'providers' array:
// Custom
App\Providers\HelperServiceProvider::class,
If you are having this issue aswell.
Use proper namespacing.

Angular 2 unit testing: Make ngFor rendered, child components' templates change?

I have something like this in my parent component's template (the parent component is being tested).
<div *ngFor="let data of _dataRows; let i=index">
<child-cmp [data]="data"></child-cmp>
</div>
<button (click)="sortDataRows()"></button>
When I click the button in the test, the data rows are sorted in the parent component (component being tested). But the child components' order on the template is not changed. Without ngFor, the child components do get their templates updated in my code, when a method is called on the parent component being tested.
Here is the sortDataRows() method in my parent component (and yes, everything works in the application!):
private sortDataRows(sortValues: any): void {
this._dataRows.sort(function(a: any, b: any): number{
if (!a[sortValues.fieldName] || !b[sortValues.fieldName]) {
//this will handle sorting of Null or undefined for any type
return this.compareNullvalues(a[sortValues.fieldName], b[sortValues.fieldName], sortValues.ascending);
}
if (a[sortValues.fieldName] instanceof Date || Number.isInteger(a[sortValues.fieldName])) {
return this.compareNumbersOrDates(a[sortValues.fieldName], b[sortValues.fieldName], sortValues.ascending);
} else {
return this.compareStringValues(a[sortValues.fieldName], b[sortValues.fieldName], sortValues.ascending);
}
});
}
This is currently an open bug on Angular - https://github.com/angular/angular/issues/12642

symfony 1.4 reuse variable declared in component.class.php [duplicate]

I want to make some action (php script) before all actions in my frontend app and then pass a result from that script to actions in variable - so I can get variable value from all actions. Where should I declare sth like this?
If the filter solution dont feet your needs, you can also create a base action class with a preExecute function:
// app/frontend/lib/baseActions.class.php
class baseActions extends sfActions
{
public function preExecute()
{
$this->myVar = .... // define your vars...
}
}
Then your module actions class extends your baseActions class:
// app/frontend/modules/myModule/actions/actions.class.php
class myModuleActions extends baseActions
{
public function executeIndex(sfWebRequest $request)
{
// var $this->myVar is available in any action and in your template
...
}
}
if you have to use the preExecute function in your module class action, remember to call parent::preExecute() in it.
What kind of information ?
I would recommend you to use filters.
In your apps/frontend/config/filters.yml:
rendering: ~
myfilter:
class: myCustomFilter
Create the file lib/filter/myCustomFilter.php:
<?php
class myCustomFilter extends sfFilter
{
public function execute ($filterChain)
{
if ($this->isFirstCall())
{
// do what ever you want here.
$config = Doctrine_Core::getTable('Config')->findAll();
sfConfig::set('my_config', $config);
}
$filterChain->execute();
}
}
And then, every where, you can retrieve your data:
sfConfig::get('my_config');

knockoutjs - custom template is binded with parent content

I have a problem using knockoutjs with custom template bindings.
Suppose I have a HTML body like this:
<div id="1">
<div data-bind="template:{name: '2', data: data}"></div>
</div>
<div id="2">
<h3 data-bind="text: caption"></h3>
</div>
JS code looks like this:
var ViewModel2 = function () {
this.caption = ko.observable("Caption");
}
var ViewModel1 = function () {
this.data = new ViewModel2();
}
ko.applyBindings(new ViewModel1(), document.getElementById("1"));
If we test this code, everything will work just fine;
See JSFiddle example: http://jsfiddle.net/4eTWW/33/
Now suppose we want to make our custom template binding. We'll use 'templatex' binding instead of 'template'.
In HTML we need to change just one line:
<div data-bind="templatex:{name: '2', data: data}"></div>
Next, let's add custom template binding to JS:
/*Custom binding*/
ko.bindingHandlers.templatex = {
init: function (element) {
ko.bindingHandlers.template.init.apply(this, arguments);
},
update: ko.bindingHandlers.template.update
}
See: http://jsfiddle.net/4eTWW/35/
But in this case we have an error, saying that it can't find 'caption' in the model.
Now let's add template {} to html bindings:
<div data-bind="template: {}, templatex:{name: '2', data: data}"></div>
See: http://jsfiddle.net/4eTWW/36/
And now everything works just fine.
It seems that while binding parent div it can't determine that child div is a template.
So how can I mark it as a template in my custom template binder?
Thanks.
You have wrong update handler, change to this:
ko.bindingHandlers.templatex= {
init: function(element) {
// do things
return ko.bindingHandlers.template.init.apply(this, arguments);
},
update: function(element) {
return ko.bindingHandlers.template.update.apply(this, arguments);
}
}
Here is working fiddle: http://jsfiddle.net/vyshniakov/4eTWW/39/
I don't think you can use a custom binding to create a new template engine. You need to register your custom engine with ko.setTemplateEngine().
From the knockoutjs source:
If you want to make a custom template engine,
[1] Inherit from the ko.templateEngine class (like ko.nativeTemplateEngine does)
[2] Override 'renderTemplateSource', supplying a function with this signature:
function (templateSource, bindingContext, options) {
// - templateSource.text() is the text of the template you should render
// - bindingContext.$data is the data you should pass into the template
// - you might also want to make bindingContext.$parent, bindingContext.$parents,
// and bindingContext.$root available in the template too
// - options gives you access to any other properties set on "data-bind: { template: options }"
//
// Return value: an array of DOM nodes
}
[3] Override 'createJavaScriptEvaluatorBlock', supplying a function with this signature:
function (script) {
// Return value: Whatever syntax means "Evaluate the JavaScript statement 'script' and output the result"
// For example, the jquery.tmpl template engine converts 'someScript' to '${ someScript }'
}
This is only necessary if you want to allow data-bind attributes to reference arbitrary template variables.
If you don't want to allow that, you can set the property 'allowTemplateRewriting' to false (like ko.nativeTemplateEngine does)
and then you don't need to override 'createJavaScriptEvaluatorBlock'.
Example: http://jsfiddle.net/6pStz/ (see Note 7 on this page)