You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何从Odoo控制器接收JSON到ExtJS文件及Bryntum Schedule数据交互求助

Hey there! Let’s tackle your Bryntum Schedule + Odoo integration issues step by step. I’ve worked through similar ExtJS/Odoo data sync problems before, so let’s break this down into actionable parts.

1. Fetching JSON from Odoo Controller with an ExtJS/Bryntum Proxy

First, you need to set up an Odoo controller to expose your data as JSON. Here’s a quick example of how that looks in Python:

from odoo import http
from odoo.http import request
import json

class BryntumScheduleController(http.Controller):
    @http.route('/bryntum/schedule/data', type='http', auth='user', methods=['GET'], csrf=True)
    def get_schedule_data(self):
        # Fetch your data from Odoo models (adjust this to your specific model/fields)
        events = request.env['your.schedule.model'].search([]).read(['name', 'start', 'end', 'resource_id', 'description'])
        
        # Return formatted JSON response with correct headers
        return request.make_response(
            json.dumps({'results': events}),
            headers={'Content-Type': 'application/json'}
        )

Next, configure your Bryntum/ExtJS Store with an Ajax Proxy to pull this data. Bryntum’s Schedule uses ExtJS under the hood, so their proxies work almost identically. Don’t forget to include Odoo’s CSRF token in the headers to avoid validation errors:

// Define your Bryntum EventStore (works similarly for ExtJS Stores)
const eventStore = new bryntum.scheduler.EventStore({
    proxy: {
        type: 'ajax',
        url: '/bryntum/schedule/data',
        headers: {
            'X-CSRF-Token': odoo.csrf_token // Odoo exposes this global variable automatically
        },
        reader: {
            type: 'json',
            rootProperty: 'results' // Match this to the key in your Odoo controller's JSON response
        }
    },
    autoLoad: true // Automatically fetch data when the store initializes
});

// Attach the store to your Bryntum Schedule instance
const scheduler = new bryntum.scheduler.Scheduler({
    eventStore: eventStore,
    // ... add your other scheduler configs here (resources, columns, etc.)
});
2. Two-Way Data Sync (Sending Updates Back to Odoo)

For bidirectional sync, you’ll need to extend your Odoo controller to handle write operations, and configure the proxy’s writer to send data in a format Odoo can parse.

First, update your Odoo controller to handle create/update/delete requests:

class BryntumScheduleController(http.Controller):
    # ... existing GET route ...

    @http.route('/bryntum/schedule/save', type='json', auth='user', methods=['POST'], csrf=True)
    def save_schedule_data(self, **kwargs):
        # Get the data sent from ExtJS/Bryntum
        sync_data = request.jsonrequest

        for event in sync_data:
            event_id = event.get('id')
            if event_id:
                # Update existing record
                request.env['your.schedule.model'].browse(event_id).write({
                    'name': event.get('name'),
                    'start': event.get('start'),
                    'end': event.get('end'),
                    'resource_id': event.get('resource_id'),
                    'description': event.get('description')
                })
            else:
                # Create new record
                request.env['your.schedule.model'].create({
                    'name': event.get('name'),
                    'start': event.get('start'),
                    'end': event.get('end'),
                    'resource_id': event.get('resource_id'),
                    'description': event.get('description')
                })
        
        # Return a success response to confirm sync
        return {'success': True, 'message': 'Data synced successfully'}

Then, update your ExtJS/Bryntum proxy to include a writer and define sync endpoints:

const eventStore = new bryntum.scheduler.EventStore({
    proxy: {
        type: 'ajax',
        url: '/bryntum/schedule/data',
        api: {
            create: '/bryntum/schedule/save',
            update: '/bryntum/schedule/save',
            destroy: '/bryntum/schedule/save' // Use a separate route here if you want dedicated delete logic
        },
        headers: {
            'X-CSRF-Token': odoo.csrf_token
        },
        reader: {
            type: 'json',
            rootProperty: 'results'
        },
        writer: {
            type: 'json',
            writeAllFields: true, // Send all fields (adjust to send only modified fields if needed)
            encode: true // Wrap data in a JSON object that Odoo's jsonrequest can parse
        }
    },
    autoLoad: true
});

// Trigger sync whenever changes are made (or set autoSync: true on the store)
eventStore.on('change', () => {
    eventStore.sync({
        success: (response) => {
            console.log('Sync successful:', response.message);
        },
        failure: (error) => {
            console.error('Sync failed:', error);
        }
    });
});
3. Common Pitfalls to Watch For
  • CSRF Token Validation: Always include the X-CSRF-Token header. Odoo blocks requests without it by default, and the global odoo.csrf_token variable is available in any Odoo web page.
  • Data Format Mismatch: Ensure your Odoo controller returns data with a root property (like results) that matches your proxy’s reader.rootProperty. Similarly, the writer’s output should align with what Odoo expects in request.jsonrequest.
  • Date Handling: Odoo uses ISO 8601 dates by default—double-check that Bryntum is parsing/sending dates in the same format (it usually does this automatically, but dates can break sync if mismatched).
  • Permissions: Make sure the logged-in user has read/write access to your Odoo model. Test your controller routes directly with tools like curl or Postman first to rule out permission issues.

内容的提问来源于stack exchange,提问作者Amor.o

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 09:36:38