[英]Passing a model from PHP/Laravel to ReactJS Fluxible store
In my TaskController.php I have: 在我的TaskController.php中,我有:
namespace Api;
use Repos\EnquiryRepo;
class TaskController extends \BaseController {
protected $repo;
function __construct() {
parent::__construct();
$this->repo = new EnquiryRepo();
}
public function show($enquiryId)
{
if(!$enquiry = $this->repo->findById($enquiryId)) {
return $this->json('That does not exist.', 404);
}
return \View::make('task.index', ['enquiry' => $enquiry]);
}
}
I am totally lost as to how I can pass the $enquiry model into my react store: 我完全迷失了如何将$ enquiry模型传递到我的React商店:
EnquiryStore.js EnquiryStore.js
import { EventEmitter } from 'events';
export default class EnquiryStore extends EventEmitter {
constructor() {
super();
this.enquiries = new Map();
this.loading = false;
}
handleEnquiriesData(payload) {
payload.data.enquiries.forEach((enquiry) => {
this.enquiries.set(enquiry.id, enquiry);
});
this.loading = false;
this.emit('change');
}
handleReceiving() {
this.loading = true;
this.emit('loading');
}
getEnquiries() {
return this.enquiries;
}
dehydrate () {
return this.enquiries;
}
rehydrate (state) {
}
}
EnquiryStore.handlers = {
'RECEIVED_ENQUIRIES_DATA': 'handleEnquiriesData',
'RECEIVING_ENQUIRIES_DATA': 'handleReceiving'
};
EnquiryStore.storeName = 'EnquiryStore';
Would I need to somehow echo it out into a JS variable or something? 我需要以某种方式将其回显到JS变量中吗? How can I get this to work?
我该如何工作? The whole point is so that when the page loads I have all the data already and React/Fluxible doesn't need to make another request for the data.
关键是要使页面加载时,我已经拥有了所有数据,而React / Fluxible不需要再次请求数据。
After a bit of trail and error I got it working: 经过一连串的错误和错误后,我开始工作了:
In my Laravel view I did: 在我的Laravel视图中,我做到了:
@extends('layouts.react')
@section('css')
{{HTML::style('/css/task.css?bust=' . time())}}
@stop
@section('js')
<script>
app_dehydrated.context.dispatcher.stores.EnquiryStore = {{$enquiry}}
</script>
@stop
Then my store: 然后我的商店:
import { EventEmitter } from 'events';
export default class EnquiryStore extends EventEmitter {
constructor() {
super();
this.enquiry = {};
this.loading = false;
}
handleReceiving() {
this.loading = true;
this.emit('loading');
}
getEnquiry() {
return this.enquiry;
}
dehydrate () {
return this.enquiry;
}
rehydrate (state) {
this.enquiry = state;
}
}
EnquiryStore.handlers = {
'RECEIVED_ENQUIRIES_DATA': 'handleEnquiriesData',
'RECEIVING_ENQUIRIES_DATA': 'handleReceiving'
};
EnquiryStore.storeName = 'EnquiryStore';
If there is a better way please tell me! 如果有更好的方法,请告诉我! Hope this helps others.
希望这对其他人有帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.