Results 1 to 5 of 5

Thread: Polymorphic model?

  1. #1
    Sencha User
    Join Date
    Sep 2012
    Posts
    83
    Vote Rating
    6
      0  

    Default Polymorphic model?

    I have a textbook case of polymorphism: my web server is going to return a shape which could be a circle (center point + radius), rectangle (upper left/lower right), polygon (arbitrary list of points), etc. What's the best way to handle this from a model standpoint?

    I'm using an ajax proxy. One solution is to define a model object which is the union of all fields and includes a string that identifies the shape type. It would work ... but I was hoping ExtJS offered a better solution. (I just don't see how to choose a different model based on the server's response.)

  2. #2
    Sencha User Stormseeker's Avatar
    Join Date
    Jan 2010
    Location
    Evanston, Illinois
    Posts
    59
    Vote Rating
    3
      0  

    Default I think something like this should work

    Code:
    Ext.define('Sample.model.Shape', {
        extend        : 'Ext.data.Model',
    
        fields        : [
            {name: 'id',                                 type: 'int'},
            {name: 'name',                             type: 'string'}
        ]
    });
    
    Ext.define('Sample.model.Circle', {
        extend        : 'Sample.model.Shape',
    
        fields        : [
            {name: 'radius',                             type: 'float'}
        ]
    });

  3. #3
    Sencha Premium Member vadimv's Avatar
    Join Date
    Sep 2010
    Location
    Cluj, Romania
    Posts
    687
    Vote Rating
    50
      0  

    Default

    Probably I would try to use extjs's associations in direction of polymorphic associations, by default is nothing implemented, and probably you will have to extend the reader in order to create the right model based on server response.....could be an idea which worths to try.
    I didn't look well, to comment if their implementations are good or not, but you may get some inspirations from this guys:
    http://extjs-tutorials.blogspot.com/...-subclass.html
    https://github.com/opennode/opennode...&-Partitioning
    Github https://github.com/vadimpopa
    Sencha tips and articles : http://vadimpopa.com
    Twitter: @vadimvpopa

  4. #4
    Sencha User
    Join Date
    Nov 2012
    Location
    uk
    Posts
    7
    Vote Rating
    0
      0  

    Default

    Quote Originally Posted by vadimv View Post
    Probably I would try to use extjs's associations in direction of polymorphic associations, by default is nothing implemented, and probably you will have to extend the reader in order to create the right model based on server response.....could be an idea which worths to try.
    I didn't look well to comment if their implementation are good or not, but you may get some inspirations from this guys:
    http://extjs-tutorials.blogspot.com/...-subclass.html
    https://github.com/opennode/opennode...&-Partitioning
    nice tutorial to read . i liked it very much....
    .

  5. #5
    Sencha User
    Join Date
    Sep 2012
    Posts
    83
    Vote Rating
    6
      0  

    Default

    Thanks vadimv, that's pretty much what I was looking for. It's a shame that ExtJS doesn't offer this functionality natively.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •