开发者

Extjs - Single Json store possible?

开发者 https://www.devze.com 2023-02-28 00:35 出处:网络
I am using ExtJS with rails 3.0.6. I have a f开发者_StackOverflow中文版orm and a grid panel. For instance, form has customer details and grid contains the product detail purchased by the particular c

I am using ExtJS with rails 3.0.6. I have a f开发者_StackOverflow中文版orm and a grid panel. For instance, form has customer details and grid contains the product detail purchased by the particular customer ( One - to - many).

Can anyone suggest how to send the entire data ( form + grid) as a single json store instead of 2 json stores?

Thanks in advance !


The new Ext JS 4 has Model associations which will solve the problem (http://docs.sencha.com/ext-js/4-0/guide/data)

// each User hasMany Orders
Ext.define('User', {
    extend: 'Ext.data.Model',
    fields: ['id', 'name', 'email'],
    proxy : {
        type: 'rest',
        url : '/users',
        reader: 'json'
    },

    hasMany: 'Orders'
});

// each Order belongsTo a User, and hasMany OrderItems
Ext.define('Order', {
    extend: 'Ext.data.Model',
    fields: ['id', 'user_id', 'status'],
    belongsTo: 'User',
    hasMany: 'OrderItems'
});

// each OrderItem belongsTo an Order
Ext.define('OrderItem', {
    extend: 'Ext.data.Model',
    fields: ['id', 'order_id', 'name', 'description', 'price', 'quantity'],
    belongsTo: 'Order'
});

Calling user.orders() returns a Store configured with the Orders model, because the User model defined an association of hasMany: 'Orders'.

User.load(123, {
    success: function(user) {
        //we can iterate over the orders easily using the Associations API
        user.orders().each(function(order) {
            console.log(order.get('status'));

            //we can even iterate over each Order's OrderItems:
            order.orderItems().each(function(orderItem) {
                console.log(orderItem.get('title'));
            });
        });
    }
});
0

精彩评论

暂无评论...
验证码 换一张
取 消