开发者

Implementing ManyToMany association in Ext Js 4

开发者 https://www.devze.com 2023-03-28 23:12 出处:网络
I\'ve looked around everywhere on the web but I can\'t seem to find a way to implement many to many association in Est Js 4 models. Take the prototypical example.开发者_如何学编程 I have an applicatio

I've looked around everywhere on the web but I can't seem to find a way to implement many to many association in Est Js 4 models. Take the prototypical example.开发者_如何学编程 I have an application with "Posts" and "Tags". How do I represent this in Ext Js 4 models and filter tag by posts and posts by tags?


ManyToMany means that there is some additional table (or array) to contains the links between A and B. So the only way to implement this would be by using an additional store.

In the case there are many items, each can belong to many categories, here's the model definitions:

Ext.define('BS.model.Item', {
    extend: 'Ext.data.Model',
    fields: [
        {name: 'name'                 , type: 'string'},
    ],

    proxy: {
        type: 'direct',

        api: {
            create: Items.Create,
            read: Items.GetTree,
            update: Items.Update,
            destroy: Items.Delete,
        },
    },

    hasMany: {model: 'BS.model.ItemInCategory', name: 'categories', foreignKey: 'itemId'},
});

Ext.define('BS.model.ItemCategory', {
    extend: 'Ext.data.Model',
    fields: [
        {name: 'name'     , type: 'string'},
    ],

    proxy: {
        type: 'direct',

        api: {
            create: ItemCategories.Create,
            read: ItemCategories.Read,
            update: ItemCategories.Update,
            destroy: ItemCategories.Delete,
        },
    },

});

Ext.define('BS.model.ItemInCategory', {
    extend: 'Ext.data.Model',
    fields: ['itemId', 'categoryId'],

    // This will allow create operations with multiple records
    clientIdProperty: 'clientId',

    proxy: {
        type: 'direct',

        api: {
            create:  ItemInCategory.Create,
            read:    ItemInCategory.Read,
            destroy: ItemInCategory.Destroy,
        },

        reader: {
            type: 'json',
            root: 'data',
        },        
    },

});

Then to get all the categories an item belongs to:

Item.categories().load({
    scope: this,
    callback: function( aRecords, aOperation, aSuccess) {

        Ext.each( aRecords, function( aAssociation ) {
            // Do something here
        }, this);

    }                
});

Then to add an association:

var iNewRecord = Item.categories().model.create( { categoryId: '16' } );

Item.categories().add( iNewRecord );


For people who find this question while searching, as of ExtJS 5.0, they support many-to-many relations natively:

http://docs.sencha.com/extjs/5.0.0/apidocs/#!/api/Ext.data.schema.ManyToMany


For ExtJS 6, ManyToMany is implemented too.

Minimal implementation:

Ext.define('App.models.Group', {
 extend: 'Ext.data.Model',

 manyToMany: [
     'User'
 ]

});

0

精彩评论

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