0

在我的 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]);

    }


}

我完全不知道如何将 $enquiry 模型传递到我的反应商店:

查询商店.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';

我需要以某种方式将其回显到 JS 变量中吗?我怎样才能让它工作?关键是,当页面加载时,我已经拥有了所有数据,并且 React/Fluxible 不需要再次请求数据。

4

1 回答 1

0

经过一些跟踪和错误后,我得到了它的工作:

在我的 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

然后我的商店:

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';

如果有更好的方法请告诉我!希望这对其他人有帮助。

于 2016-05-19T07:25:13.817 回答